mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-15 03:57:30 +08:00
fix bug
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"clawgo/pkg/logger"
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MessageBus struct {
|
||||
@@ -12,6 +14,8 @@ type MessageBus struct {
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
const queueWriteTimeout = 2 * time.Second
|
||||
|
||||
func NewMessageBus() *MessageBus {
|
||||
return &MessageBus{
|
||||
inbound: make(chan InboundMessage, 100),
|
||||
@@ -21,7 +25,15 @@ func NewMessageBus() *MessageBus {
|
||||
}
|
||||
|
||||
func (mb *MessageBus) PublishInbound(msg InboundMessage) {
|
||||
mb.inbound <- msg
|
||||
select {
|
||||
case mb.inbound <- msg:
|
||||
case <-time.After(queueWriteTimeout):
|
||||
logger.ErrorCF("bus", "PublishInbound timeout (queue full)", map[string]interface{}{
|
||||
"channel": msg.Channel,
|
||||
"chat_id": msg.ChatID,
|
||||
"session_key": msg.SessionKey,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (mb *MessageBus) ConsumeInbound(ctx context.Context) (InboundMessage, bool) {
|
||||
@@ -34,7 +46,14 @@ func (mb *MessageBus) ConsumeInbound(ctx context.Context) (InboundMessage, bool)
|
||||
}
|
||||
|
||||
func (mb *MessageBus) PublishOutbound(msg OutboundMessage) {
|
||||
mb.outbound <- msg
|
||||
select {
|
||||
case mb.outbound <- msg:
|
||||
case <-time.After(queueWriteTimeout):
|
||||
logger.ErrorCF("bus", "PublishOutbound timeout (queue full)", map[string]interface{}{
|
||||
"channel": msg.Channel,
|
||||
"chat_id": msg.ChatID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (mb *MessageBus) SubscribeOutbound(ctx context.Context) (OutboundMessage, bool) {
|
||||
|
||||
Reference in New Issue
Block a user