From 86f224bcf41edc7041aa3db03e64ba68f2f01a11 Mon Sep 17 00:00:00 2001 From: Alberic Corniere Date: Mon, 26 Dec 2022 11:02:53 +0100 Subject: [PATCH] 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. --- server/service_windows.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/service_windows.go b/server/service_windows.go index 2920f74b..a7ab37d7 100644 --- a/server/service_windows.go +++ b/server/service_windows.go @@ -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 }