Skip to content

Commit

Permalink
feat: add ReconnectWaitSeconds; tidy connect loop
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <[email protected]>
  • Loading branch information
TylerGillson committed Oct 31, 2023
1 parent a913b30 commit 04bbcfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
44 changes: 23 additions & 21 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 4 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions stream/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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().
Expand Down

0 comments on commit 04bbcfe

Please sign in to comment.