From 4ee133cc5ad6e9f8c13363dbb85d0367953ecf83 Mon Sep 17 00:00:00 2001 From: Dylan Evans <62859381+fin3ss3g0d@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:33:46 -0400 Subject: [PATCH 1/2] Fix client backoff --- client/client.go | 4 ++++ client/client_connect.go | 2 +- main.go | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 49b62e65..53d1da2c 100644 --- a/client/client.go +++ b/client/client.go @@ -35,6 +35,7 @@ type Config struct { Auth string KeepAlive time.Duration MaxRetryCount int + MinRetryInterval time.Duration MaxRetryInterval time.Duration Server string Proxy string @@ -75,6 +76,9 @@ func NewClient(c *Config) (*Client, error) { if !strings.HasPrefix(c.Server, "http") { c.Server = "http://" + c.Server } + if c.MinRetryInterval < time.Second { + c.MinRetryInterval = 100 * time.Millisecond + } if c.MaxRetryInterval < time.Second { c.MaxRetryInterval = 5 * time.Minute } diff --git a/client/client_connect.go b/client/client_connect.go index 884c7647..20671c96 100644 --- a/client/client_connect.go +++ b/client/client_connect.go @@ -19,7 +19,7 @@ import ( func (c *Client) connectionLoop(ctx context.Context) error { //connection loop! - b := &backoff.Backoff{Max: c.config.MaxRetryInterval} + b := &backoff.Backoff{Max: c.config.MaxRetryInterval, Min: c.config.MinRetryInterval} for { connected, err := c.connectionOnce(ctx) //reset backoff after successful connections diff --git a/main.go b/main.go index 01f9ca3b..2702404a 100644 --- a/main.go +++ b/main.go @@ -382,6 +382,9 @@ var clientHelp = ` --max-retry-count, Maximum number of times to retry before exiting. Defaults to unlimited. + --min-retry-interval, Minimum wait time before retrying after a + disconnection. Incremented by 2x for each retry. Defaults to 100ms. + --max-retry-interval, Maximum wait time before retrying after a disconnection. Defaults to 5 minutes. @@ -427,6 +430,7 @@ func client(args []string) { flags.StringVar(&config.Auth, "auth", "", "") flags.DurationVar(&config.KeepAlive, "keepalive", 25*time.Second, "") flags.IntVar(&config.MaxRetryCount, "max-retry-count", -1, "") + flags.DurationVar(&config.MinRetryInterval, "min-retry-interval", 0, "") flags.DurationVar(&config.MaxRetryInterval, "max-retry-interval", 0, "") flags.StringVar(&config.Proxy, "proxy", "", "") flags.StringVar(&config.TLS.CA, "tls-ca", "", "") From 8ed1d39c26ae00acd7d8f03f770d605b1bc0b8d6 Mon Sep 17 00:00:00 2001 From: Dylan Evans <62859381+fin3ss3g0d@users.noreply.github.com> Date: Wed, 6 Nov 2024 19:55:24 -0500 Subject: [PATCH 2/2] add jitter to client backoff --- client/client.go | 1 + client/client_connect.go | 4 +++- main.go | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 53d1da2c..0a23b7ca 100644 --- a/client/client.go +++ b/client/client.go @@ -37,6 +37,7 @@ type Config struct { MaxRetryCount int MinRetryInterval time.Duration MaxRetryInterval time.Duration + RetryJitter bool Server string Proxy string Remotes []string diff --git a/client/client_connect.go b/client/client_connect.go index 20671c96..35f58e92 100644 --- a/client/client_connect.go +++ b/client/client_connect.go @@ -19,7 +19,9 @@ import ( func (c *Client) connectionLoop(ctx context.Context) error { //connection loop! - b := &backoff.Backoff{Max: c.config.MaxRetryInterval, Min: c.config.MinRetryInterval} + b := &backoff.Backoff{Min: c.config.MinRetryInterval, + Max: c.config.MaxRetryInterval, + Jitter: c.config.RetryJitter} for { connected, err := c.connectionOnce(ctx) //reset backoff after successful connections diff --git a/main.go b/main.go index 2702404a..a0f6ea79 100644 --- a/main.go +++ b/main.go @@ -388,6 +388,8 @@ var clientHelp = ` --max-retry-interval, Maximum wait time before retrying after a disconnection. Defaults to 5 minutes. + --retry-jitter, Enable jitter in the retry interval. Defaults to false. + --proxy, An optional HTTP CONNECT or SOCKS5 proxy which will be used to reach the chisel server. Authentication can be specified inside the URL. @@ -432,6 +434,7 @@ func client(args []string) { flags.IntVar(&config.MaxRetryCount, "max-retry-count", -1, "") flags.DurationVar(&config.MinRetryInterval, "min-retry-interval", 0, "") flags.DurationVar(&config.MaxRetryInterval, "max-retry-interval", 0, "") + flags.BoolVar(&config.RetryJitter, "retry-jitter", false, "") flags.StringVar(&config.Proxy, "proxy", "", "") flags.StringVar(&config.TLS.CA, "tls-ca", "", "") flags.BoolVar(&config.TLS.SkipVerify, "tls-skip-verify", false, "")