Skip to content

Commit

Permalink
Merge pull request #709 from Darkren/feature/vpn-redial-backoff
Browse files Browse the repository at this point in the history
Add exponential backoff for VPN client reconnection
  • Loading branch information
jdknives authored Mar 17, 2021
2 parents f136915 + 5a49b91 commit 55f6797
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions internal/vpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,25 @@ func (c *Client) Serve() error {

c.setAppStatus(ClientStatusConnecting)

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
}

c.setAppStatus(ClientStatusReconnecting)
fmt.Println("Connection broke, reconnecting...")
if err := c.dialServeConn(); err != nil {
c.setAppStatus(ClientStatusReconnecting)
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.
Expand Down

0 comments on commit 55f6797

Please sign in to comment.