Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions statefun/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,30 @@ func NewRuntime(config RuntimeConfig) (*Runtime, error) {
shutdown: make(chan struct{}),
}

var err error
natsOpts := nats.GetDefaultOptions()
natsOpts.Url = r.config.natsURL
natsOpts.MaxReconnect = -1 // -1 - infinity attempts
natsOpts.ReconnectedCB = func(nc *nats.Conn) {
lg.GetLogger().Warnf(context.TODO(), "NATS reconnected %d times", nc.Statistics.Reconnects)
}

if r.config.enableTLS {
opts := []nats.Option{
nats.Secure(&tls.Config{InsecureSkipVerify: true}), // for self-assigned certificates
}
r.nc, err = nats.Connect(config.natsURL, opts...)
} else {
r.nc, err = nats.Connect(config.natsURL)
natsOpts.Secure = true
natsOpts.TLSConfig = &tls.Config{InsecureSkipVerify: true} // for self-assigned certificates
}

var err error
maxAttempts := system.GetEnvMustProceed("RETRIES_NATS_CONNECT", 10)
for attempt := 1; attempt <= maxAttempts; attempt++ {
r.nc, err = natsOpts.Connect()
if err != nil && attempt < maxAttempts {
lg.GetLogger().Errorf(context.TODO(), "Can't connect to NATS at attempt %d/%d: %v", attempt, maxAttempts, err)
// Linear backoff: increase delay by 1 second on each attempt
time.Sleep(time.Duration(1+attempt) * time.Second)
} else {
break
}
}
if err != nil {
return nil, err
}
Expand Down
Loading