Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Q033】如何对接口进行限流 #34

Open
shfshanyue opened this issue Nov 15, 2019 · 1 comment
Open

【Q033】如何对接口进行限流 #34

shfshanyue opened this issue Nov 15, 2019 · 1 comment
Labels

Comments

@shfshanyue
Copy link
Owner

No description provided.

@shfshanyue
Copy link
Owner Author

shfshanyue commented Nov 16, 2019

一般采用漏桶算法:

  1. 漏桶初始为空
  2. API 调用是在往漏桶里注水
  3. 漏桶会以一定速率出水
  4. 水满时 API 拒绝调用

漏桶算法

可以使用 redis 的计数器实现

  1. 计数器初始为空
  2. API 调用计数器增加
  3. 给计数器设置过期时间,隔段时间清零,视为一定速率出水
  4. 计数器达到上限时,拒绝调用

当然,这只是大致思路,这时会有两个问题要注意

  1. 最坏情况下的限流是额定限流速率的2倍
  2. 条件竞争问题

不过实际实现时注意以下就好了(话说一般也是调用现成的三方库做限流...),可以参考我以前的文章 https://shanyue.tech/post/rate-limit/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant