rate limiter - system design

1 问题

Whenever you expose a web service / api endpoint, you need to implement a rate limiter to prevent abuse of the service (DOS attacks).

Implement a RateLimiter Class with an isAllow method. Every request comes in with a unique clientID, deny a request if that client has made more than 100 requests in the past second.

这是一个流控问题。

2 输入

Request:

client ID和时间戳timestamp

3 输出

true or false

4 思路

保存每个client ID过去1s的所有请求。

数据结构?

 

5 关于guava的RateLimiter

5.1 RateLimiter在下列应用场景中的应用

https://www.cnblogs.com/yeyinfu/p/7316972.html

5.2 RateLimiter用在接口中不会超时吗?

比如限制访问数据库,每秒不超过10,使用RateLimiter的本质是每个请求等待0.1秒。这不会导致请求超时吗?

5.3 抢购场景降级 tryAcquire()

 

6 限流算法:令牌桶算法和漏桶算法

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-05-30 16:24  PhoenixTree(梧桐树)  阅读(182)  评论(0编辑  收藏  举报