adshao / go-binance

A Go SDK for Binance API
MIT License
1.55k stars 677 forks source link

Websocket timeout #29

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi adshao, first of all thanks for your work. Really appreaciated. I just wanted to ask you if there's a connection timeout or ping/pong mechanism to ensure connection stability?

adshao commented 6 years ago

hi @davidklodner , thanks for the issue. for connection timeout, do you mean readdealine as in https://godoc.org/github.com/gorilla/websocket#Conn.SetReadDeadline , I think we can add it as a websocket config; while for ping/pong handler, I cannot find any info about it from binance api doc, and I tested it with https://godoc.org/github.com/gorilla/websocket#Conn.SetPingHandler and https://godoc.org/github.com/gorilla/websocket#Conn.SetPongHandler, there is nothing sent from binance server, I think it's fine to ignore them. Do you have any better suggestion?

ghost commented 6 years ago

@adshao Try setting a pong handler and sending the server a ping. If the server correctly implements the websocket protocol, then the application should receive a pong. This may not be documented because it's at the Websocket protocol level, not part of the application.

adshao commented 6 years ago

@claudia-jones thanks for the update, you are right, we can periodically send ping message to server, and check pong message for connection stability.

kustov-an commented 6 years ago

Hi! Any updates on this?

adshao commented 6 years ago

hi @kustov-an , will implement it soon.

adshao commented 6 years ago

I don't think we need ping/pong handler here, instead, HandshakeTimeout https://godoc.org/github.com/gorilla/websocket#Dialer and CloseHandler https://godoc.org/github.com/gorilla/websocket#Conn.SetCloseHandler are enough to check connection stability. Since these changes will break the compatibility, we need to refactor the web socket interfaces.

kustov-an commented 6 years ago

Correct me if I wrong, but as I understand 1) HandshakeTimeout is only used when establishing a new connection 2) CloseHandler is executed when the connection is closed by the peer So those changes are not going to help when the connection is lost due to network problems?

adshao commented 6 years ago

hi @kustov-an , if the connection is lost, ReadMessage will return error: https://github.com/adshao/go-binance/blob/master/websocket.go#L45, so we can use errHandler here.

kustov-an commented 6 years ago

Hi @adshao, please take a look at my pull request #50 addressing this issue

adshao commented 6 years ago

close as solved

aleibovici commented 3 years ago

@adshao your project has been a tremendous help. Much appreciated. I recently started seeing many "websocket: close 1008 (policy violation): Pong timeout" messages. I see that this case is closed, but I wonder if this is something you or anyone else is seeing. My code simply restart the a Websocket, but I wonder if I should be doing something diferent.

snawaz commented 2 years ago

@adshao your project has been a tremendous help. Much appreciated. I recently started seeing many "websocket: close 1008 (policy violation): Pong timeout" messages. I see that this case is closed, but I wonder if this is something you or anyone else is seeing. My code simply restart the a Websocket, but I wonder if I should be doing something diferent.

this is because https://github.com/adshao/go-binance/pull/50 does not fix the problem, as it sends ping message whereas the client needs to send pong message. As the per the binance docs, it's the server which sends ping to the client and then if the client does not respond with a pong message within 10 mins, then the server will disconnect the connection.

goriunov commented 6 months ago

For future readers, as per @snawaz looks like if you set custom ping handler it will not automatically send pong based on the Gorilla websocket implementation. You will need to send pong back by yourself from within your custom ping handler.

image

This primarily required for the implementation on the gorilla websocket with custom handler. Note that if no handler is passed or no SetPingHandler called, it will default to the default ping handler

snawaz commented 6 months ago

For future readers, as per @snawaz looks like if you set custom ping handler it will not automatically send pong based on the Gorilla websocket implementation. You will need to send pong back by yourself from within your custom ping handler.

That's correct. I have a custom ping handler which sends websocket.PongMessage, as soon as it receives the PingMessage.

image
anyongjin commented 3 days ago

I also encountered this error when I used gorilla/websocket to subscribe to Binance's websocket data. After a long period of testing, I finally found that it was because I subscribed to too many symbols at the same time. I subscribed to the order book data of all spot trading pairs at one time, a total of 1257. The subscription was successful and there was no error. But after receiving the first ping and sending pong, no more ping messages from binance were received. After another 10 minutes, the connection was disconnected and the output error was:

close 1008 (policy violation): Pong timeout

The Binance document says that a connection supports up to 1024 streams. I tried to subscribe to only one trading pair instead. This time I could receive ping messages every three minutes and the connection was not disconnected again. I suggest check whether it is caused by too many subscribed streams.