From 0f5636c077d65ade1fdc8311f8c390a67523bf7e Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 3 Mar 2021 22:13:04 +0300 Subject: [PATCH] Add exponential backoff for client reconnection --- internal/vpn/client.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/vpn/client.go b/internal/vpn/client.go index 1669e5d636..7959a614c3 100644 --- a/internal/vpn/client.go +++ b/internal/vpn/client.go @@ -185,17 +185,24 @@ func (c *Client) Serve() error { fmt.Println("Closed TUN") }() - for { - if err := c.dialServeConn(); err != nil { - fmt.Printf("dialServeConn: %v\n", err) - } - + r := netutil.NewDefaultRetrier(c.log) + err := r.Do(context.Background(), func() error { if c.isClosed() { return nil } - fmt.Println("Connection broke, reconnecting...") + if err := c.dialServeConn(); err != nil { + fmt.Println("Connection broke, reconnecting...") + return fmt.Errorf("dialServeConn: %w", err) + } + + return nil + }) + if err != nil { + return fmt.Errorf("failed to connect to the server: %w", err) } + + return nil } // Close closes client.