mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-27 01:05:15 +00:00
improve maxapi websocket reconnect issue
This commit is contained in:
parent
c3c3c47808
commit
9406682944
|
@ -3,6 +3,7 @@ package max
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -160,20 +161,38 @@ func (s *WebSocketService) read(ctx context.Context) {
|
||||||
|
|
||||||
default:
|
default:
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
|
if err := s.conn.SetReadDeadline(time.Now().Add(time.Second * 5)); err != nil {
|
||||||
|
log.WithError(err).Error("can not set read deadline")
|
||||||
|
}
|
||||||
|
|
||||||
mt, msg, err := s.conn.ReadMessage()
|
mt, msg, err := s.conn.ReadMessage()
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
|
// if it's a network timeout error, we should re-connect
|
||||||
s.EmitDisconnect()
|
switch err := err.(type) {
|
||||||
// emit reconnect to start a new connection
|
|
||||||
s.emitReconnect()
|
// if it's a websocket related error
|
||||||
|
case *websocket.CloseError:
|
||||||
|
if err.Code == websocket.CloseNormalClosure {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// for unexpected close error, we should re-connect
|
||||||
|
// emit reconnect to start a new connection
|
||||||
|
s.EmitDisconnect()
|
||||||
|
s.emitReconnect()
|
||||||
|
return
|
||||||
|
|
||||||
log.WithError(err).Error("websocket error")
|
case net.Error:
|
||||||
|
s.EmitDisconnect()
|
||||||
|
s.emitReconnect()
|
||||||
|
return
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.WithError(err).Error("unexpected websocket connection error")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if mt != websocket.TextMessage {
|
if mt != websocket.TextMessage {
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue
Block a user