feat: add rate limiting and improve event handling
- Introduced rate limiting configuration in config.toml with options for enabling, requests per second (RPS), and burst capacity. - Enhanced event handling in the OneBot11 adapter to ignore messages sent by the bot itself. - Updated the dispatcher to register rate limit middleware based on configuration settings. - Refactored WebSocket message handling to support flexible JSON parsing and improved event type detection. - Removed deprecated echo plugin and associated tests to streamline the codebase.
This commit is contained in:
@@ -206,6 +206,11 @@ func (a *Adapter) ParseMessage(raw []byte) (protocol.Event, error) {
|
||||
return nil, fmt.Errorf("failed to unmarshal raw event: %w", err)
|
||||
}
|
||||
|
||||
// 忽略机器人自己发送的消息
|
||||
if rawEvent.PostType == "message_sent" {
|
||||
return nil, fmt.Errorf("ignoring message_sent event")
|
||||
}
|
||||
|
||||
return a.convertToEvent(&rawEvent)
|
||||
}
|
||||
|
||||
@@ -419,17 +424,24 @@ func (a *Adapter) handleWebSocketMessages() {
|
||||
zap.Int("size", len(message)),
|
||||
zap.String("preview", string(message[:min(len(message), 200)])))
|
||||
|
||||
// 尝试解析为响应
|
||||
var resp OB11Response
|
||||
if err := sonic.Unmarshal(message, &resp); err == nil {
|
||||
// 如果有echo字段,说明是API响应
|
||||
if resp.Echo != "" {
|
||||
a.logger.Debug("Received API response",
|
||||
zap.String("echo", resp.Echo),
|
||||
zap.String("status", resp.Status),
|
||||
zap.Int("retcode", resp.RetCode))
|
||||
a.wsWaiter.Notify(&resp)
|
||||
continue
|
||||
// 尝试解析为响应(先检查是否有 echo 字段)
|
||||
var tempMap map[string]interface{}
|
||||
if err := sonic.Unmarshal(message, &tempMap); err == nil {
|
||||
if echo, ok := tempMap["echo"].(string); ok && echo != "" {
|
||||
// 有 echo 字段,说明是API响应
|
||||
var resp OB11Response
|
||||
if err := sonic.Unmarshal(message, &resp); err == nil {
|
||||
a.logger.Debug("Received API response",
|
||||
zap.String("echo", resp.Echo),
|
||||
zap.String("status", resp.Status),
|
||||
zap.Int("retcode", resp.RetCode))
|
||||
a.wsWaiter.Notify(&resp)
|
||||
continue
|
||||
} else {
|
||||
a.logger.Warn("Failed to parse API response",
|
||||
zap.Error(err),
|
||||
zap.String("echo", echo))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,9 +453,14 @@ func (a *Adapter) handleWebSocketMessages() {
|
||||
a.logger.Info("Parsing OneBot event...")
|
||||
event, err := a.ParseMessage(message)
|
||||
if err != nil {
|
||||
a.logger.Error("Failed to parse event",
|
||||
zap.Error(err),
|
||||
zap.ByteString("raw_message", message))
|
||||
// 如果是忽略的事件(如 message_sent),只记录 debug 日志
|
||||
if err.Error() == "ignoring message_sent event" {
|
||||
a.logger.Debug("Ignoring message_sent event")
|
||||
} else {
|
||||
a.logger.Error("Failed to parse event",
|
||||
zap.Error(err),
|
||||
zap.ByteString("raw_message", message))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user