Skip to content

Commit f79f801

Browse files
committed
fix: 统一使用EchoTimeOut常量替代硬编码超时时间,方法名规范为Get
1 parent 79e82b9 commit f79f801

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

driver/ws.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"github.com/tidwall/gjson"
2222
)
2323

24+
// ws onebot await echo message time out
25+
var EchoTimeOut = 5 * time.Second
26+
2427
type echoStore struct {
2528
mu sync.RWMutex
2629
echos map[int64]chan Response[json.RawMessage]
@@ -41,7 +44,7 @@ func (e *echoStore) Receive(selfId int64, data []byte) error {
4144
return nil
4245
}
4346

44-
func (e *echoStore) GetEcho(selfId int64) chan Response[json.RawMessage] {
47+
func (e *echoStore) Get(selfId int64) chan Response[json.RawMessage] {
4548
e.mu.Lock()
4649
defer e.mu.Unlock()
4750
echoChan, ok := e.echos[selfId]
@@ -127,7 +130,7 @@ func (ws *WSClient) Listen(ctx context.Context, eventChan chan<- event.Event) er
127130
return
128131
}
129132

130-
emitter := NewEmitterWS(botevent.SelfId, c, ws.echoStore.GetEcho(botevent.SelfId))
133+
emitter := NewEmitterWS(botevent.SelfId, c, ws.echoStore.Get(botevent.SelfId))
131134

132135
if slices.Contains(botevent.Types, event.EVENT_META) {
133136
connSelfId = botevent.SelfId
@@ -232,7 +235,7 @@ func (ws *WServer) Listen(ctx context.Context, eventChan chan<- event.Event) err
232235
return
233236
}
234237

235-
emitter := NewEmitterWS(botevent.SelfId, c, ws.echoStore.GetEcho(botevent.SelfId))
238+
emitter := NewEmitterWS(botevent.SelfId, c, ws.echoStore.Get(botevent.SelfId))
236239

237240
if slices.Contains(botevent.Types, event.EVENT_META) {
238241
connSelfId = botevent.SelfId
@@ -518,7 +521,7 @@ func (e *EmitterWS) Raw(ctx context.Context, action Action, params any) ([]byte,
518521
return nil, err
519522
}
520523
e.mu.Unlock()
521-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
524+
ctx, cancel := context.WithTimeout(ctx, EchoTimeOut)
522525
defer cancel()
523526
for {
524527
select {
@@ -544,7 +547,7 @@ func wsAction[P any](conn *websocket.Conn, action string, params P) (string, err
544547
}
545548

546549
func wsWait[R any](ctx context.Context, echoId string, echoChan chan Response[json.RawMessage]) (*R, error) {
547-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
550+
ctx, cancel := context.WithTimeout(ctx, EchoTimeOut)
548551
defer cancel()
549552
for {
550553
select {

0 commit comments

Comments
 (0)