diff --git a/relay/relay.go b/relay/relay.go index 749ccc0..42ef772 100644 --- a/relay/relay.go +++ b/relay/relay.go @@ -5,6 +5,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -104,14 +105,39 @@ func (c *Client) Listen(ctx context.Context) { return } + reconnectBackoffSteps := []time.Duration{ + 0, + 100 * time.Millisecond, + 500 * time.Millisecond, + time.Second, + 5 * time.Second, + } + reconnectAttempts := 0 + for { err := c.connect(ctx) + if errors.Is(err, context.Canceled) { + c.close() + return + } if err != nil { color.Red("Failed to connect to Webhook Relay:\n%s\n", err.Error()) c.close() - return + + backoff := reconnectBackoffSteps[len(reconnectBackoffSteps)-1] + if reconnectAttempts < len(reconnectBackoffSteps) { + backoff = reconnectBackoffSteps[reconnectAttempts] + } + color.Yellow("Reattempting connection in %v\n", backoff) + time.Sleep(backoff) + + reconnectAttempts += 1 + continue } + // If the connection was successful reset reconnect counter + reconnectAttempts = 0 + select { case <-ctx.Done(): c.stopRead <- Stop{}