VictorFrWu / bybit.go.api

Bybit Official Open API SDK
MIT License
8 stars 20 forks source link

[bug] panic: concurrent write to websocket connection #31

Closed mateusfmello closed 3 months ago

mateusfmello commented 5 months ago

Hello, I am getting this error after hours of connecting to testnet websocket server.

Failed to send ping: write tcp 10.0.0.15:42664->13.227.126.13:443: write: broken pipe
panic: concurrent write to websocket connection

goroutine 16 [running]:
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc000160f30, 0x1, {0x0?, 0x0?, 0x0?})
        /home/mateus/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:632 +0x4b8
github.com/gorilla/websocket.(*messageWriter).Close(0x30?)
        /home/mateus/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:746 +0x35
github.com/gorilla/websocket.(*Conn).beginMessage(0xc000386420, 0xc0004d4360, 0x1)
        /home/mateus/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:493 +0x47
github.com/gorilla/websocket.(*Conn).NextWriter(0xc000386420, 0x1)
        /home/mateus/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:535 +0x3f
github.com/gorilla/websocket.(*Conn).WriteMessage(0xc00019a140?, 0xc00002a300?, {0xc00002a330, 0x23, 0x30})
        /home/mateus/go/pkg/mod/github.com/gorilla/websocket@v1.5.1/conn.go:788 +0x138
github.com/wuhewuhe/bybit%2ego%2eapi.(*WebSocket).Send(...)
        /home/mateus/go/pkg/mod/github.com/wuhewuhe/bybit.go.api@v1.0.2/bybit_websocket.go:147
github.com/wuhewuhe/bybit%2ego%2eapi.(*WebSocket).SendAsJson(0xc0001c21c0, {0x6cf760?, 0xc0004d4300?})
        /home/mateus/go/pkg/mod/github.com/wuhewuhe/bybit.go.api@v1.0.2/bybit_websocket.go:194 +0x85
github.com/XXXXXXXXXX/bybit.(*BybitWebSocket).verificaConexaoEReinicia(0xc0001c0540)
        /home/mateus/XXXXXXXXXXX/bybit/web_socket.go:96 +0xff
created by github.com/XXXXXXXXXX/bybit.(*BybitWebSocket).Inicia in goroutine 1
        /home/mateus/XXXXXXXXXXX/bybit/web_socket.go:59 +0x65
exit status 2

How do I create the instance

bybitWebSocket.publicWS = bybit.NewBybitPublicWebSocket(
    "wss://stream-testnet.bybit.com/v5/public/spot",
    20,
    bybitWebSocket.processaMensagens,
)

bybitWebSocket.publicWS.Connect([]string{"tickers.BTCUSDT", "kline.1.BTCUSDT"})

I create a goroutine to check if it is able to communicate with the server

for {
    select {
    case <-bws.ctx.Done():
        return
    default:
    }

    time.Sleep(time.Second * 30)

    err = bybitWebSocket.publicWS.SendAsJson(map[string]string{"op": "ping", "req_id": "public_WS"})
    if err != nil {
        slog.Error("[BYBIT::WS] Erro ao enviar Ping para o servidor público", "erro", err)
        _ = bybitWebSocket.publicWS.Disconnect()
        bybitWebSocket.publicWS.Connect([]string{"tickers.BTCUSDT", "kline.1.BTCUSDT"})
    }
}
VictorFrWu commented 3 months ago
ws := bybit.NewBybitPublicWebSocket("wss://stream.bybit.com/v5/public/spot", func(message string) error {
    fmt.Println("Received:", message)
    return nil
})
_ = ws.Connect([]string{"orderbook.1.BTCUSDT"})
select {}
VictorFrWu commented 3 months ago

refactor websocket and release next version 1.0.3