From fbed26d94b4a6d3cf88b4dcc6d9d4c6fa2ee89da Mon Sep 17 00:00:00 2001 From: tigerbot Date: Thu, 30 Mar 2017 18:13:27 -0600 Subject: [PATCH] changed client to retry after disconnect --- rvpn/client/client.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/rvpn/client/client.go b/rvpn/client/client.go index a0821f5..e98660e 100644 --- a/rvpn/client/client.go +++ b/rvpn/client/client.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "fmt" "net/url" + "time" "github.com/gorilla/websocket" ) @@ -37,11 +38,22 @@ func Run(ctx context.Context, config *Config) error { handler := NewWsHandler(config.Services) - conn, _, err := dialer.Dial(serverURL.String(), nil) - if err != nil { - return fmt.Errorf("First connection to server failed - check auth: %v", err) - } + authenticated := false + for { + if conn, _, err := dialer.Dial(serverURL.String(), nil); err == nil { + authenticated = true + handler.HandleConn(ctx, conn) + } else if !authenticated { + return fmt.Errorf("First connection to server failed - check auth: %v", err) + } + loginfo.Println("disconnected from remote server") - handler.HandleConn(ctx, conn) - return nil + // Sleep for a few seconds before trying again, but only if the context is still active + select { + case <-ctx.Done(): + return nil + case <-time.After(5 * time.Second): + } + loginfo.Println("attempting reconnect to remote server") + } }