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

feat: add simple ratelimit control #217

Merged
merged 1 commit into from
Nov 7, 2023
Merged

Conversation

hank9999
Copy link
Contributor

@hank9999 hank9999 commented Oct 21, 2023

add simple ratelimit control

默认启用速率控制(剩余数量小于80时启动),如需禁用则需要Bot传入参数ratelimiter=None
RateLimiter 支持传入参数控制什么时候开始速率控制
当剩余数量(X-Rate-Limit-Remaining) 小于 传入参数时 开始速率控制

cert = Cert(token=token)
bot = Bot(cert=cert, ratelimiter=None)  # 不启用速率控制
bot = Bot(cert=cert, ratelimiter=RateLimiter(start=100))  # 小于100的时候开始速率控制

khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated
log.debug(f'{method} {route}: req: {params}') # token is excluded
if self.ratelimit:
bucket = self._ratelimit.get_bucket(route)
delay = self._ratelimit.get_delay(bucket)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_delay中可以内含一个get_bucket,这样API会更好理解

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delay 是由 bucket 获取的, 不是由 route 获取的 感觉分离比较好

khl/requester.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
@hank9999 hank9999 force-pushed the ratelimit branch 3 times, most recently from 3a63292 to faf2c3a Compare November 3, 2023 18:37
khl/bot/bot.py Outdated Show resolved Hide resolved
khl/requester.py Outdated Show resolved Hide resolved
khl/ratelimiter.py Outdated Show resolved Hide resolved
Copy link
Owner

@TWT233 TWT233 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TWT233
Copy link
Owner

TWT233 commented Nov 6, 2023

晚上我测测看

@hank9999
Copy link
Contributor Author

hank9999 commented Nov 6, 2023

晚上我测测看

好的,麻烦老师了,我这边测试的时候能稳定住速率

@TWT233 TWT233 merged commit 3002faf into TWT233:main Nov 7, 2023
2 checks passed
@hank9999 hank9999 deleted the ratelimit branch November 13, 2023 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants