Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
创建 RateLimiter 频率限制器类
修改 SunRequests 类集成频率限制
更新 init.py 导出 RateLimiter
实现方案
1. 新建 rate_limiter.py
创建了 RateLimiter 类,采用 滑动窗口算法 实现频率限制:
核心特性:
2. 修改 sunrequests.py
在 SunRequests.request 方法中仅添加一行代码:
3. 更新 init.py
导出 rate_limiter 实例和 RateLimiter 类。
使用方式
from adata.common.utils import rate_limiter
设置默认频率(所有域名,默认30次/分钟)
rate_limiter.set_default_rate(60)
设置特定域名的频率限制
rate_limiter.set_rate('data.eastmoney.com', 100)
rate_limiter.set_rate('q.10jqka.com.cn', 20)
查看某个域名的频率限制
rate = rate_limiter.get_rate('data.eastmoney.com')
清除历史记录(可选)
rate_limiter.clear_history() # 清除所有
rate_limiter.clear_history('data.eastmoney.com') # 清除特定域名