From 44752ad006b2368ad3a8798f136c33da090f1cc1 Mon Sep 17 00:00:00 2001 From: Andrii Stelmashenko Date: Thu, 11 Jan 2024 14:33:19 +0200 Subject: [PATCH] reconnect jitter configurable (#467) * reconnect jitter configurable * updated docs: reconnect jitter configurable * Update pkg/common/nats/conn.go Co-authored-by: Dan <5727701+dan-j@users.noreply.github.com> --------- Co-authored-by: Dan <5727701+dan-j@users.noreply.github.com> --- docs/jetstream.md | 8 +++++--- examples/config-nats.yaml | 3 ++- pkg/common/config/eventingnatsconfig.go | 4 ++++ pkg/common/nats/conn.go | 17 ++++++++++++++++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/docs/jetstream.md b/docs/jetstream.md index bf386eb50..5d217624a 100644 --- a/docs/jetstream.md +++ b/docs/jetstream.md @@ -106,9 +106,11 @@ data: secret: name: "" # a secret containing a `ca.crt` entry. connOpts: - retryOnFailedConnect: true # should it reconnect on failed connection - maxReconnects: 50 # max reconnect attempts - reconnectWait: 2000 # delay between reconnect attempts + retryOnFailedConnect: true # should it reconnect on failed connection + maxReconnects: 50 # max reconnect attempts + reconnectWait: 2000 # delay between reconnect attempts + reconnectJitterMilliseconds: 100 # upper bound of a random delay added ReconnectWait + reconnectJitterTLSMilliseconds: 1000 # upper bound of a random delay added ReconnectWait ``` ## JetStream integration diff --git a/examples/config-nats.yaml b/examples/config-nats.yaml index 00ea7d030..e503376ce 100644 --- a/examples/config-nats.yaml +++ b/examples/config-nats.yaml @@ -10,4 +10,5 @@ data: retryOnFailedConnect: true maxReconnects: 5 reconnectWaitMilliseconds: 2000 - + reconnectJitterMilliseconds: 100 + reconnectJitterTLSMilliseconds: 1000 diff --git a/pkg/common/config/eventingnatsconfig.go b/pkg/common/config/eventingnatsconfig.go index 3e7a662aa..34faf6a28 100644 --- a/pkg/common/config/eventingnatsconfig.go +++ b/pkg/common/config/eventingnatsconfig.go @@ -71,4 +71,8 @@ type ConnOpts struct { RetryOnFailedConnect bool `json:"retryOnFailedConnect,omitempty"` // ReconnectWaitMilliseconds time between reconnects in milliseconds ReconnectWaitMilliseconds int `json:"reconnectWaitMilliseconds,omitempty"` + // ReconnectJitterMilliseconds Option to set the upper bound of a random delay added ReconnectWait + ReconnectJitterMilliseconds int `json:"reconnectJitterMilliseconds,omitempty"` + // ReconnectJitterTLSMilliseconds Option to set the upper bound of a random delay added ReconnectWait + ReconnectJitterTLSMilliseconds int `json:"reconnectJitterTLSMilliseconds,omitempty"` } diff --git a/pkg/common/nats/conn.go b/pkg/common/nats/conn.go index 441223334..d1c5f27e6 100644 --- a/pkg/common/nats/conn.go +++ b/pkg/common/nats/conn.go @@ -38,6 +38,11 @@ import ( "knative.dev/eventing-natss/pkg/common/constants" ) +const ( + defaultReconnectJitter = time.Duration(100) * time.Millisecond + defaultReconnectJitterTLS = time.Duration(1000) * time.Millisecond +) + var ( ErrBadCredentialFileOption = errors.New("bad auth.credentialFile option") ErrBadMTLSOption = errors.New("bad auth.tls option") @@ -82,6 +87,9 @@ func NewNatsConn(ctx context.Context, config commonconfig.EventingNatsConfig) (* // reconnection options if config.ConnOpts != nil && config.ConnOpts.RetryOnFailedConnect { reconnectWait := time.Duration(config.ConnOpts.ReconnectWaitMilliseconds) * time.Millisecond + reconnectJitter := defaultJitterIfEmpty(config.ConnOpts.ReconnectJitterMilliseconds, defaultReconnectJitter) + reconnectJitterTLS := defaultJitterIfEmpty(config.ConnOpts.ReconnectJitterTLSMilliseconds, defaultReconnectJitterTLS) + logger.Infof("Configuring retries: %#v", config.ConnOpts) opts = append(opts, nats.RetryOnFailedConnect(config.ConnOpts.RetryOnFailedConnect)) opts = append(opts, nats.ReconnectWait(reconnectWait)) @@ -93,7 +101,7 @@ func NewNatsConn(ctx context.Context, config commonconfig.EventingNatsConfig) (* logger.Debugf("Reconnect attempts left: %d", config.ConnOpts.MaxReconnects-attempts) return reconnectWait })) - opts = append(opts, nats.ReconnectJitter(1000, time.Millisecond)) + opts = append(opts, nats.ReconnectJitter(reconnectJitter, reconnectJitterTLS)) opts = append(opts, nats.DisconnectErrHandler(func(conn *nats.Conn, err error) { logger.Warnf("Disconnected from JSM: err=%v", err) logger.Warnf("Disconnected from JSM: will attempt reconnects for %d", config.ConnOpts.MaxReconnects) @@ -108,6 +116,13 @@ func NewNatsConn(ctx context.Context, config commonconfig.EventingNatsConfig) (* return nats.Connect(url, opts...) } +func defaultJitterIfEmpty(jitter int, defaultValue time.Duration) time.Duration { + if jitter == 0 { + return defaultValue + } + return time.Duration(jitter) * time.Millisecond +} + func buildAuthOption(ctx context.Context, config commonconfig.ENConfigAuth, secrets clientsetcorev1.SecretInterface) ([]nats.Option, error) { opts := make([]nats.Option, 0, 2) if config.CredentialFile != nil {