Set stratup time with env NATS_STARTUP_DELAY

The startup delay when starting as a service under windows was set to 10
seconds.

This value was fixed and hardcoded since before JetStream. On (some)
Windows systems, this leads to service startup failures, as the store
dir sorting may be hindered by an important load, or slowed down by
increased accesses times, typically from security software influence.

This commit allows overriding the startup delay using an environment
variable NATS_STARTUP_DELAY. This variable is checked outside init() to
allow reporting into the service log.

Any value can be used (even lower than the initial 10s). Bare NATS
start can be far faster than 10s. Ops may want to setup this env var to
lower values in order to trigger an error on stricter constraints.
This commit is contained in:
Alberic Corniere
2022-12-26 11:02:53 +01:00
parent d44362f696
commit 86f224bcf4

View File

@@ -42,6 +42,7 @@ type winServiceWrapper struct {
}
var dockerized = false
var startupDelay = 10 * time.Second
func init() {
if v, exists := os.LookupEnv("NATS_DOCKERIZED"); exists && v == "1" {
@@ -66,8 +67,16 @@ func (w *winServiceWrapper) Execute(args []string, changes <-chan svc.ChangeRequ
status <- svc.Status{State: svc.StartPending}
go w.server.Start()
if v, exists := os.LookupEnv("NATS_STARTUP_DELAY"); exists {
delay, err := time.ParseDuration(v)
if err == nil {
startupDelay = delay
} else {
w.server.Errorf("Failed to parse \"%v\" as a duration for startup: %s", v, err)
}
}
// Wait for accept loop(s) to be started
if !w.server.ReadyForConnections(10 * time.Second) {
if !w.server.ReadyForConnections(startupDelay) {
// Failed to start.
return false, 1
}