add rate limiting for LLMs, added a new api for adding RSS , fixed a bug where the RSS interface was not using the access key.
add rate limiting for LLMs, added a new api for adding RSS , fixed a bug where the RSS interface was not using the access key.
Summary
This PR introduces three major enhancements to improve the functionality, security, and stability of the zenfeed project:
1. LLM Rate Limiting Implementation
- Added RPM (Requests Per Minute) configuration field for LLM settings
- Implemented token bucket rate limiter with comprehensive unit tests
- Integrated rate limiting into OpenAI and Gemini LLM implementations
- Updated documentation to describe the new rate limiting configuration
2. RSS Feed Source Addition Interface
- Added
AddFeedSourceAPI endpoint for dynamically adding new RSS feed sources - Implemented comprehensive validation for feed source parameters
- Added HTTP route
/add_feed_sourcefor REST API access - Created MCP tool
add_feed_sourcefor feed source management through MCP protocol
3. Fixed RSS Interface Access Key Bug
- Added
RSSHubAccessKeyconfiguration field to support RSSHub instances with authentication - Implemented
appendAccessKeyToURLmethod to automatically append access keys to RSSHub API requests - Updated RSSHub query methods to use access keys properly
实现LLM 速率限制,新增rss订阅链接添加接口,修复rss接口没有使用accessKey的bug
摘要
本 PR 引入了三项主要增强功能,以提高 zenfeed 项目的功能性、安全性和稳定性:
1. 实现LLM速率限制
- 为 LLM 设置添加
RPM(每分钟请求数)配置字段 - 实现基于令牌桶算法的速率限制器,包含全面的单元测试
- 将速率限制集成到 OpenAI 和 Gemini LLM 实现中
- 更新文档,描述新的速率限制配置
2. 新增RSS订阅链接添加接口
- 添加
AddFeedSourceAPI 端点,用于动态添加新的 RSS 源 - 实现对 RSS 源参数的全面验证
- 添加 HTTP 路由
/add_feed_source用于 REST API 访问 - 创建 MCP 工具
add_feed_source,通过 MCP 协议管理 RSS 源
3. 修复RSS接口没有使用accessKey的bug
- 添加
RSSHubAccessKey配置字段,支持需要身份验证的 RSSHub 实例 - 实现
appendAccessKeyToURL方法,自动将访问密钥附加到 RSSHub API 请求 - 更新 RSSHub 查询方法,正确使用访问密钥
Summary by Sourcery
添加可配置的 LLM 每分钟请求次数限流、新的用于添加 RSS 源的 API 接口(包括 HTTP 和 MCP 工具),并修复 RSSHub 和标签处理逻辑,以提高可靠性并支持带身份认证的 RSS。
New Features:
- 引入可配置的「按 LLM 维度」的每分钟请求数(RPM)限流机制,并将其应用于 OpenAI 和 Gemini provider
- 新增 API endpoint、HTTP route 和 MCP tool,以便动态向抓取配置中添加 RSS 源
Bug Fixes:
- 确保 RSSHub 请求在配置中提供可选的 access key 时,将其追加到所有 RSSHub API URL 中
- 修复 JSON label 的序列化处理,避免返回可能会从对象池中被复用的缓冲区
Enhancements:
- 在 LLM 配置中扩展一个 RPM 字段,并在工厂配置中完成传递
- 在 API 配置中暴露 RSSHub access key,以支持需要认证的 RSSHub 实例
Documentation:
- 在英文和中文的配置指南中补充新的 LLM
rpm配置选项说明
Tests:
- 为基于 token-bucket 的 LLM 限流器添加单元测试,覆盖基础行为、速率控制、取消、零/负 RPM,以及并发场景
Original summary in English
Summary by Sourcery
Add configurable LLM requests-per-minute rate limiting, a new API surface for adding RSS feed sources (including HTTP and MCP tooling), and fix RSSHub and label handling to improve reliability and authenticated RSS support.
New Features:
- Introduce configurable per-LLM requests-per-minute rate limiting and apply it to OpenAI and Gemini providers
- Add an API endpoint, HTTP route, and MCP tool to dynamically add RSS feed sources to the scrape configuration
Bug Fixes:
- Ensure RSSHub requests include an optional access key from configuration by appending it to all RSSHub API URLs
- Fix JSON label marshaling to avoid returning a buffer that may be reused from the pool
Enhancements:
- Extend LLM configuration with an RPM field and propagate it through factory configuration
- Expose RSSHub access key in API configuration for authenticated RSSHub instances
Documentation:
- Document the new LLM rpm configuration option in both English and Chinese config guides
Tests:
- Add unit tests for the token-bucket-based LLM rate limiter, including basic, rate, cancellation, zero/negative RPM, and concurrency scenarios
Reviewer's Guide
为 LLM 提供商实现基于令牌桶的每分钟请求数(RPM)限流;新增用于动态添加 RSS 源的 API / HTTP / MCP 接口;修复所有 RSSHub 查询中对 RSSHub 访问密钥的处理;并通过在重用缓冲区前进行拷贝,修正 Labels 中的 JSON 编码(marshaling)问题,同时更新相关配置与文档。
新增 add_feed_source MCP 与 API 流程的时序图
sequenceDiagram
actor User
participant MCPClient
participant MCPServer as MCPServer_server
participant API as API_impl
participant ConfigMgr as ConfigManager
User->>MCPClient: Configure add_feed_source tool call
MCPClient->>MCPServer: callTool add_feed_source(name, url|rsshub_route_path)
MCPServer->>MCPServer: addFeedSource validate arguments
MCPServer->>API: AddFeedSource(AddFeedSourceRequest)
API->>API: Validate Source (name, RSS, URL vs RSSHubRoutePath)
API->>ConfigMgr: AppConfig()
ConfigMgr-->>API: AppConfig with Scrape.Sources
API->>API: Check duplicate source name
API->>ConfigMgr: SaveAppConfig(updated AppConfig)
ConfigMgr-->>API: Save result
API-->>MCPServer: AddFeedSourceResponse
MCPServer-->>MCPClient: CallToolResult "Feed source added successfully"
MCPClient-->>User: Display success message
LLM 限流与提供商集成的类图
classDiagram
class Config {
+string Provider
+string APIKey
+string Model
+string EmbeddingModel
+string TTSModel
+float32 Temperature
+int RPM
}
class RateLimiter {
<<interface>>
+Wait(ctx context.Context) error
+TryAcquire() bool
}
class noopRateLimiter {
+Wait(ctx context.Context) error
+TryAcquire() bool
}
class tokenBucketRateLimiter {
-int rpm
-time.Duration interval
-chan structEmpty tokens
-sync.Mutex mu
-chan structEmpty stopCh
-bool stopped
+Wait(ctx context.Context) error
+TryAcquire() bool
+Stop()
-refillTokens()
}
class openai {
+*component.Base[Config, structEmpty] Base
+text text
+RateLimiter rateLimiter
}
class openaiText {
+*component.Base[Config, structEmpty] Base
+*oai.Client client
+embeddingSpliter embeddingSpliter
+RateLimiter rateLimiter
+String(ctx context.Context, messages []string) (string, error)
+Embedding(ctx context.Context, s string) ([]float32, error)
}
class gemini {
+*component.Base[Config, structEmpty] Base
+text text
+*http.Client hc
+RateLimiter rateLimiter
+embeddingSpliter embeddingSpliter
+WAV(ctx context.Context, text string, speakers []Speaker) (io.ReadCloser, error)
}
class NewRateLimiter {
<<function>>
+NewRateLimiter(rpm int) RateLimiter
}
RateLimiter <|.. noopRateLimiter
RateLimiter <|.. tokenBucketRateLimiter
NewRateLimiter ..> RateLimiter
NewRateLimiter ..> tokenBucketRateLimiter
openai o-- openaiText
openaiText ..> RateLimiter
gemini ..> RateLimiter
Config <.. openai
Config <.. openaiText
Config <.. gemini
API 中 RSSHub 访问密钥与 AddFeedSource 端点的类图
classDiagram
class API {
<<interface>>
+QueryRSSHubCategories(ctx context.Context, req *QueryRSSHubCategoriesRequest) (*QueryRSSHubCategoriesResponse, error)
+QueryRSSHubWebsites(ctx context.Context, req *QueryRSSHubWebsitesRequest) (*QueryRSSHubWebsitesResponse, error)
+QueryRSSHubRoutes(ctx context.Context, req *QueryRSSHubRoutesRequest) (*QueryRSSHubRoutesResponse, error)
+AddFeedSource(ctx context.Context, req *AddFeedSourceRequest) (*AddFeedSourceResponse, error)
}
class api {
+Config() *Config
+Dependencies() *Dependencies
+Reload(app *config.App) error
+QueryRSSHubCategories(ctx context.Context, req *QueryRSSHubCategoriesRequest) (*QueryRSSHubCategoriesResponse, error)
+QueryRSSHubWebsites(ctx context.Context, req *QueryRSSHubWebsitesRequest) (*QueryRSSHubWebsitesResponse, error)
+QueryRSSHubRoutes(ctx context.Context, req *QueryRSSHubRoutesRequest) (*QueryRSSHubRoutesResponse, error)
+AddFeedSource(ctx context.Context, req *AddFeedSourceRequest) (*AddFeedSourceResponse, error)
-appendAccessKeyToURL(url string) string
}
API <|.. api
class Config {
+string RSSHubEndpoint
+string RSSHubAccessKey
+string LLM
+Validate() error
+From(app *config.App) *Config
}
class AddFeedSourceRequest {
+config.ScrapeSource Source
}
class AddFeedSourceResponse {
}
class ScrapeSource {
+string Name
+*ScrapeSourceRSS RSS
}
class ScrapeSourceRSS {
+string URL
+string RSSHubRoutePath
}
class ConfigManager {
+AppConfig() *config.App
+SaveAppConfig(app *config.App) error
}
class Dependencies {
+ConfigManager ConfigManager
}
api ..> Config
api ..> Dependencies
api ..> AddFeedSourceRequest
AddFeedSourceRequest o-- ScrapeSource
ScrapeSource o-- ScrapeSourceRSS
Dependencies o-- ConfigManager
class HTTPServer {
+new(instance string, app *config.App, dependencies Dependencies) (Server, error)
}
HTTPServer ..> API
class MCPServer {
+registerFeedSourceTools()
+addFeedSource(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
}
MCPServer ..> API
MCPServer ..> AddFeedSourceRequest
File-Level Changes
| Change | Details | Files |
|---|---|---|
| 为每个 LLM 引入可配置的 RPM 限流,并将其集成到 OpenAI 和 Gemini 调用中。 |
|
pkg/llm/llm.gopkg/config/config.godocs/config.mddocs/config-zh.mdpkg/llm/ratelimiter.gopkg/llm/ratelimiter_test.gopkg/llm/openai.gopkg/llm/gemini.go |
| 增加动态创建 RSS 源的 API,并通过 HTTP JSON-RPC 和 MCP 工具对外暴露。 |
|
pkg/api/api.gopkg/api/http/http.gopkg/api/mcp/mcp.go |
| 修复 RSSHub 调用,确保在所有 API 请求中正确携带已配置的访问密钥。 |
|
pkg/api/api.go |
| 防止在 Labels.MarshalJSON 中重用内部 bytes 缓冲区导致输出损坏。 |
|
pkg/model/model.go |
Tips and commands
与 Sourcery 交互
- 触发一次新的审查: 在 Pull Request 中评论
@sourcery-ai review。 - 继续讨论: 直接回复 Sourcery 的审查评论。
- 从审查评论生成 GitHub Issue: 在某条审查评论下回复,请求 Sourcery 从该评论创建一个 issue。你也可以直接回复
@sourcery-ai issue来从该评论创建 issue。 - 生成 Pull Request 标题: 在 Pull Request 标题中任意位置写
@sourcery-ai即可随时生成标题。也可以在 Pull Request 中评论@sourcery-ai title来(重新)生成标题。 - 生成 Pull Request 摘要: 在 Pull Request 正文任意位置写
@sourcery-ai summary,即可在该位置生成 PR 摘要。也可以评论@sourcery-ai summary来随时(重新)生成摘要。 - 生成审查者指南: 在 Pull Request 中评论
@sourcery-ai guide,即可随时(重新)生成审查者指南。 - 一次性解决所有 Sourcery 评论: 在 Pull Request 中评论
@sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不想再看到它们,这会很有用。 - 忽略所有 Sourcery 审查: 在 Pull Request 中评论
@sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审查。特别适合希望基于新一轮审查重新开始的场景——别忘了再评论@sourcery-ai review来触发新的审查!
自定义你的使用体验
前往你的 控制台 以:
- 启用或禁用审查特性,例如 Sourcery 生成的 Pull Request 摘要、审查者指南等。
- 更改审查语言。
- 添加、移除或编辑自定义审查说明。
- 调整其他审查设置。
获取帮助
Original review guide in English
Reviewer's Guide
Implements token-bucket RPM rate limiting for LLM providers, adds a new API/HTTP/MCP interface for dynamically adding RSS feed sources, fixes RSSHub access-key handling in all RSSHub queries, and corrects a JSON marshaling bug in Labels by copying the buffer before reuse, along with corresponding config and docs updates.
Sequence diagram for the new add_feed_source MCP and API flow
sequenceDiagram
actor User
participant MCPClient
participant MCPServer as MCPServer_server
participant API as API_impl
participant ConfigMgr as ConfigManager
User->>MCPClient: Configure add_feed_source tool call
MCPClient->>MCPServer: callTool add_feed_source(name, url|rsshub_route_path)
MCPServer->>MCPServer: addFeedSource validate arguments
MCPServer->>API: AddFeedSource(AddFeedSourceRequest)
API->>API: Validate Source (name, RSS, URL vs RSSHubRoutePath)
API->>ConfigMgr: AppConfig()
ConfigMgr-->>API: AppConfig with Scrape.Sources
API->>API: Check duplicate source name
API->>ConfigMgr: SaveAppConfig(updated AppConfig)
ConfigMgr-->>API: Save result
API-->>MCPServer: AddFeedSourceResponse
MCPServer-->>MCPClient: CallToolResult "Feed source added successfully"
MCPClient-->>User: Display success message
Class diagram for LLM rate limiting and provider integration
classDiagram
class Config {
+string Provider
+string APIKey
+string Model
+string EmbeddingModel
+string TTSModel
+float32 Temperature
+int RPM
}
class RateLimiter {
<<interface>>
+Wait(ctx context.Context) error
+TryAcquire() bool
}
class noopRateLimiter {
+Wait(ctx context.Context) error
+TryAcquire() bool
}
class tokenBucketRateLimiter {
-int rpm
-time.Duration interval
-chan structEmpty tokens
-sync.Mutex mu
-chan structEmpty stopCh
-bool stopped
+Wait(ctx context.Context) error
+TryAcquire() bool
+Stop()
-refillTokens()
}
class openai {
+*component.Base[Config, structEmpty] Base
+text text
+RateLimiter rateLimiter
}
class openaiText {
+*component.Base[Config, structEmpty] Base
+*oai.Client client
+embeddingSpliter embeddingSpliter
+RateLimiter rateLimiter
+String(ctx context.Context, messages []string) (string, error)
+Embedding(ctx context.Context, s string) ([]float32, error)
}
class gemini {
+*component.Base[Config, structEmpty] Base
+text text
+*http.Client hc
+RateLimiter rateLimiter
+embeddingSpliter embeddingSpliter
+WAV(ctx context.Context, text string, speakers []Speaker) (io.ReadCloser, error)
}
class NewRateLimiter {
<<function>>
+NewRateLimiter(rpm int) RateLimiter
}
RateLimiter <|.. noopRateLimiter
RateLimiter <|.. tokenBucketRateLimiter
NewRateLimiter ..> RateLimiter
NewRateLimiter ..> tokenBucketRateLimiter
openai o-- openaiText
openaiText ..> RateLimiter
gemini ..> RateLimiter
Config <.. openai
Config <.. openaiText
Config <.. gemini
Class diagram for API RSSHub access key and AddFeedSource endpoint
classDiagram
class API {
<<interface>>
+QueryRSSHubCategories(ctx context.Context, req *QueryRSSHubCategoriesRequest) (*QueryRSSHubCategoriesResponse, error)
+QueryRSSHubWebsites(ctx context.Context, req *QueryRSSHubWebsitesRequest) (*QueryRSSHubWebsitesResponse, error)
+QueryRSSHubRoutes(ctx context.Context, req *QueryRSSHubRoutesRequest) (*QueryRSSHubRoutesResponse, error)
+AddFeedSource(ctx context.Context, req *AddFeedSourceRequest) (*AddFeedSourceResponse, error)
}
class api {
+Config() *Config
+Dependencies() *Dependencies
+Reload(app *config.App) error
+QueryRSSHubCategories(ctx context.Context, req *QueryRSSHubCategoriesRequest) (*QueryRSSHubCategoriesResponse, error)
+QueryRSSHubWebsites(ctx context.Context, req *QueryRSSHubWebsitesRequest) (*QueryRSSHubWebsitesResponse, error)
+QueryRSSHubRoutes(ctx context.Context, req *QueryRSSHubRoutesRequest) (*QueryRSSHubRoutesResponse, error)
+AddFeedSource(ctx context.Context, req *AddFeedSourceRequest) (*AddFeedSourceResponse, error)
-appendAccessKeyToURL(url string) string
}
API <|.. api
class Config {
+string RSSHubEndpoint
+string RSSHubAccessKey
+string LLM
+Validate() error
+From(app *config.App) *Config
}
class AddFeedSourceRequest {
+config.ScrapeSource Source
}
class AddFeedSourceResponse {
}
class ScrapeSource {
+string Name
+*ScrapeSourceRSS RSS
}
class ScrapeSourceRSS {
+string URL
+string RSSHubRoutePath
}
class ConfigManager {
+AppConfig() *config.App
+SaveAppConfig(app *config.App) error
}
class Dependencies {
+ConfigManager ConfigManager
}
api ..> Config
api ..> Dependencies
api ..> AddFeedSourceRequest
AddFeedSourceRequest o-- ScrapeSource
ScrapeSource o-- ScrapeSourceRSS
Dependencies o-- ConfigManager
class HTTPServer {
+new(instance string, app *config.App, dependencies Dependencies) (Server, error)
}
HTTPServer ..> API
class MCPServer {
+registerFeedSourceTools()
+addFeedSource(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error)
}
MCPServer ..> API
MCPServer ..> AddFeedSourceRequest
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Introduce configurable per-LLM RPM rate limiting and integrate it into OpenAI and Gemini calls. |
|
pkg/llm/llm.gopkg/config/config.godocs/config.mddocs/config-zh.mdpkg/llm/ratelimiter.gopkg/llm/ratelimiter_test.gopkg/llm/openai.gopkg/llm/gemini.go |
| Add a dynamic RSS feed source creation API and expose it via HTTP JSON-RPC and MCP tool. |
|
pkg/api/api.gopkg/api/http/http.gopkg/api/mcp/mcp.go |
| Fix RSSHub calls to correctly include the configured access key on all API requests. |
|
pkg/api/api.go |
| Prevent reuse of an internal bytes buffer in Labels.MarshalJSON that could lead to corrupted output. |
|
pkg/model/model.go |
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
- Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with
@sourcery-ai issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore. - Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
- Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
关于客户端限流的‘必要性’,有什么具体场景吗?
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
Regarding the ‘necessity’ of client-side current limiting, are there any specific scenarios?
关于客户端限流的‘必要性’,有什么具体场景吗?
之前用阿里云百炼的 deepseek-v3.2 不到10分钟干完了免费的 一百万token 。
然后我换成用薄荷大佬的公益api,他的RPM只有3,但是免费 (狗头
对于公益大模型来说,RPM限制还是挺有用的,如果一直请求对方服务器,像是ddos对方服务器一样。 或者对于各种免费的 2api 来说,并发太高会有封号风险。
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
Regarding the ‘necessity’ of client-side current limiting, are there any specific scenarios?
Previously, I used Alibaba Cloud Bailian's deepseek-v3.2 to complete the free one million tokens in less than 10 minutes.
Then I switched to using the public welfare API of Mint Boss. His RPM is only 3, but it is free.
For large public welfare models, RPM limits are quite useful. If you keep requesting the other party's server, it is like ddos the other party's server. Or for various free APIs, if the concurrency is too high, there is a risk of account closure.