Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to re-run websocket if it down #384

Closed
Icarus9913 opened this issue Jun 18, 2022 · 8 comments
Closed

How to re-run websocket if it down #384

Icarus9913 opened this issue Jun 18, 2022 · 8 comments

Comments

@Icarus9913
Copy link

I found you use for loop to traverse. But once it meet err the whole websokcetTick will down.
In this situation, how can we run it gain?

for {
_, message, err := c.ReadMessage()
if err != nil {
if !silent {
errHandler(err)
}
return
}
handler(message)
}

@Icarus9913
Copy link
Author

cc @adshao

@Icarus9913
Copy link
Author

Can we use continue instead of return in line 60?

@Icarus9913
Copy link
Author

This is my err log:

websokcet get price err(this is custom): websocket: close 1006 (abnormal closure): unexpected EOF

@Icarus9913
Copy link
Author

In another way, we could add one boolean arg to check whether it should continue when it occurs an error.

@adshao
Copy link
Owner

adshao commented Jun 27, 2022

Can we use continue instead of return in line 60?

It's impossible to break the loop if we use continue. Maybe you can just reconnect websocket from the first place to retry.

@gurkanguray
Copy link

gurkanguray commented Jul 15, 2022

@Icarus9913 your errHandler function should "handle" the error. If you want to reconnect immediately, just reconnect it while handling error. Here is an example function:

func errorHandler(symbol string, klineInterval string) func(err error) {
	return func(err error) { // handler function that handles error
		fmt.Println("Error: ", err.Error())
		_, _, err = binance.WsKlineServe(symbol, klineInterval, klineHandler(klineInterval), errorHandler(symbol, klineInterval)) // start the kline websocket connection again with the same interval
		if err != nil {                                                                                                            // if there is an error on reconnecting (I suggest you to check your internet connection first and binance api/server status here)
			fmt.Println(err.Error())
			return
		}
	}
}

@rodrigo-brito
Copy link
Contributor

rodrigo-brito commented Aug 8, 2022

I used it with a channel wrapper, when it disconnects I use a backoff retry:
https://github.com/rodrigo-brito/ninjabot/blob/main/exchange/binance.go#L470-L514

func (b *Binance) CandlesSubscription(ctx context.Context, pair, period string) (chan model.Candle, chan error) {
	ccandle := make(chan model.Candle)
	cerr := make(chan error)

	go func() {
		ba := &backoff.Backoff{
			Min: 100 * time.Millisecond,
			Max: 1 * time.Second,
		}

		for {
			done, _, err := binance.WsKlineServe(pair, period, func(event *binance.WsKlineEvent) {
				ba.Reset()
				candle := CandleFromWsKline(pair, event.Kline)
				ccandle <- candle

			}, func(err error) {
				cerr <- err
			})
			if err != nil {
				cerr <- err
				close(cerr)
				close(ccandle)
				return
			}

			select {
			case <-ctx.Done():
				close(cerr)
				close(ccandle)
				return
			case <-done:
				time.Sleep(ba.Duration())
			}
		}
	}()

	return ccandle, cerr
}

The final usage will looks like:

candles, _ := binance.CandlesSubscription(ctx, "BTCUSDT", "1h")
for candle := range candles {
// your logic here
}

@xyq-c-cpp
Copy link
Collaborator

No response for a long time, close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants