From 04bbcfe77c0cd2f0dee0ba13f3cf7d80d06f4833 Mon Sep 17 00:00:00 2001 From: Tyler Gillson Date: Tue, 31 Oct 2023 09:17:51 -0400 Subject: [PATCH] feat: add ReconnectWaitSeconds; tidy connect loop Signed-off-by: Tyler Gillson --- cfg/config.go | 44 +++++++++++++++++++++++--------------------- config.toml | 6 ++++-- stream/nats.go | 4 +--- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/cfg/config.go b/cfg/config.go index 9db18e9..de5ec9a 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -67,18 +67,19 @@ type SnapshotConfiguration struct { } type NATSConfiguration struct { - URLs []string `toml:"urls"` - SubjectPrefix string `toml:"subject_prefix"` - StreamPrefix string `toml:"stream_prefix"` - ServerConfigFile string `toml:"server_config"` - SeedFile string `toml:"seed_file"` - CredsUser string `toml:"user_name"` - CredsPassword string `toml:"user_password"` - CAFile string `toml:"ca_file"` - CertFile string `toml:"cert_file"` - KeyFile string `toml:"key_file"` - BindAddress string `toml:"bind_address"` - ConnectRetries int `toml:"connect_retries"` + URLs []string `toml:"urls"` + SubjectPrefix string `toml:"subject_prefix"` + StreamPrefix string `toml:"stream_prefix"` + ServerConfigFile string `toml:"server_config"` + SeedFile string `toml:"seed_file"` + CredsUser string `toml:"user_name"` + CredsPassword string `toml:"user_password"` + CAFile string `toml:"ca_file"` + CertFile string `toml:"cert_file"` + KeyFile string `toml:"key_file"` + BindAddress string `toml:"bind_address"` + ConnectRetries int `toml:"connect_retries"` + ReconnectWaitSeconds int `toml:"reconnect_wait_seconds"` } type LoggingConfiguration struct { @@ -143,15 +144,16 @@ var Config = &Configuration{ }, NATS: NATSConfiguration{ - URLs: []string{}, - SubjectPrefix: "marmot-change-log", - StreamPrefix: "marmot-changes", - ServerConfigFile: "", - SeedFile: "", - CredsPassword: "", - CredsUser: "", - BindAddress: "0.0.0.0:4222", - ConnectRetries: 5, + URLs: []string{}, + SubjectPrefix: "marmot-change-log", + StreamPrefix: "marmot-changes", + ServerConfigFile: "", + SeedFile: "", + CredsPassword: "", + CredsUser: "", + BindAddress: "0.0.0.0:4222", + ConnectRetries: 5, + ReconnectWaitSeconds: 2, }, Logging: LoggingConfiguration{ diff --git a/config.toml b/config.toml index bd6c18b..9169606 100644 --- a/config.toml +++ b/config.toml @@ -120,7 +120,7 @@ urls=[ ] # Embedded server bind address bind_address="0.0.0.0:4222" -# Embedded server config file (will be only used if URLs array is empty) +# Embedded server config file (will only be used if URLs array is empty) server_config="" # Subject prefix used when publishing log entries, it's usually suffixed by shard number # to get the full subject name @@ -137,8 +137,10 @@ seed_file="" # User credentials used for plain user password authentication user_name="" user_password="" -# Number of retries when establishing the NATS server connection (will be only used if URLs array is not empty) +# Number of retries when establishing the NATS server connection (will only be used if URLs array is not empty) connect_retries=5 +# Wait time between NATS reconnect attempts (will only be used if URLs array is not empty) +reconnect_wait_seconds=2 # Console STDOUT configurations diff --git a/stream/nats.go b/stream/nats.go index 7829aea..afc8734 100644 --- a/stream/nats.go +++ b/stream/nats.go @@ -48,8 +48,6 @@ func Connect() (*nats.Conn, error) { Int("attempt_limit", cfg.Config.NATS.ConnectRetries). Str("status", conn.Status().String()). Msg("NATS connection failed") - - continue } return conn, err @@ -95,7 +93,7 @@ func setupConnOptions() []nats.Option { return []nats.Option{ nats.Name(cfg.Config.NodeName()), nats.RetryOnFailedConnect(true), - nats.ReconnectWait(time.Second), + nats.ReconnectWait(time.Duration(cfg.Config.NATS.ReconnectWaitSeconds) * time.Second), nats.MaxReconnects(cfg.Config.NATS.ConnectRetries), nats.ClosedHandler(func(nc *nats.Conn) { log.Fatal().