Reconnect on any read error

This commit is contained in:
Elara 2023-01-24 13:19:22 -08:00
parent 4fe5c94d9c
commit ef2e61a1d7

View File

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"strings"
"time" "time"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
@ -58,9 +57,7 @@ func NewWebSocket(baseURL string) (*WSClient, error) {
if err != nil { if err != nil {
out.errCh <- err out.errCh <- err
// gorilla/websocket eats the error type, so I have to check conn.Close()
// the string itself
if strings.HasSuffix(err.Error(), "connection timed out") {
err = backoff.RetryNotify( err = backoff.RetryNotify(
func() error { func() error {
conn, _, err = keepaliveDialer().Dial(u.JoinPath("ws").String(), nil) conn, _, err = keepaliveDialer().Dial(u.JoinPath("ws").String(), nil)
@ -80,7 +77,6 @@ func NewWebSocket(baseURL string) (*WSClient, error) {
if err != nil { if err != nil {
out.errCh <- err out.errCh <- err
} }
}
continue continue
} }
@ -201,7 +197,7 @@ func keepaliveDialer() *websocket.Dialer {
return nil, err return nil, err
} }
err = conn.SetKeepAlivePeriod(time.Second) err = conn.SetKeepAlivePeriod(10 * time.Second)
if err != nil { if err != nil {
return nil, err return nil, err
} }