简体   繁体   English

如何在环回中制定API速率限制策略

[英]How to make API rate limit policy in loopback

I just want to make an API requests rate limiting per account plan so let's say that we have users and every user have a plan that has some limits of how many API requests per day they can make. 我只想根据帐户计划制定API请求速率限制,因此我们假设我们有用户,并且每个用户都有一个计划,该计划对每天可以进行的API请求数量有一些限制。

So now, How can i make an API limit policy in loopback 3.x. 那么现在,我如何在loopback 3.x中制定API限制策略。

Thanks 谢谢

If you're planning on using Loopback on IBM Bluemix hosting you can use their API Connect service that includes customer plan based policies with API level throttling , monitoring, API billing and many other API management features. 如果您计划在IBM Bluemix托管上使用Loopback,则可以使用其API Connect服务,其中包括基于客户计划的策略以及API级别限制 ,监控,API计费和许多其他API管理功能。

StrongLoop API Microgateway is used by API Connect but is now open sourced (Apr 2017). API Connect使用StrongLoop API Microgateway ,但现在是开源的(2017年4月)。

Since Loopback is just a layer on top of Express, you can alternatively just use an Express lib. 由于Loopback只是Express上的一个层,您可以选择使用Express lib。

For rate limiting on a single standalone Loopback server you can use one of these Express libs: 对于单个独立Loopback服务器的速率限制,您可以使用以下Express库中的一个:

If you plan to use this on a cluster of Loopback servers you'll need to store the API call counts as part of the shared server state of each user or user session. 如果您计划在Loopback服务器群集上使用此功能,则需要将API调用计数存储为每个用户或用户会话的共享服务器状态的一部分。 The weapon of choice for this is Redis since it's a high performance in memory data store that can be scaled. 选择的武器是Redis,因为它可以在内存数据存储中实现高性能扩展。 Rate limiting Express libs that support Redis include: 速率限制支持Redis的Express库包括:

Finally, you could also implement rate limiting on a reverse proxy. 最后,您还可以在反向代理上实现速率限制。 See Nginx Rate Limiting 请参阅Nginx速率限制

such a policy can only* be made with a database, such as redis/memcached. 这样的策略只能用数据库来完成,比如redis / memcached。 For my projects I rely on redback which is based on Redis. 对于我的项目,我靠了Redback是基于Redis的。 It has a built in RateLimit helper (among others) and it takes care of some raceconditions and atomic transactions. 它有一个内置的RateLimit助手(以及其他),它负责一些竞争条件和原子事务。

* if you don't have a database, you could store it in-memory (in a hash or array) and use intervals to flush it, but I'd go with redback :) *如果你没有数据库,你可以将它存储在内存中(在散列或数组中)并使用间隔来刷新它,但我会使用redback :)

This is an access control policy. 这是一种访问控制策略。

You can handle this by custom roles created by role resolver . 您可以通过角色解析程序创建的自定义角色来处理此问题。

By creating a custom role and checking in that resolver callback if the current user exceeded from rate limit or not. 通过创建自定义角色并在当前用户超出速率限制时检入该解析器回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM