more optimisation
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-01-14 21:30:10 +11:00
parent 877b65f10b
commit 434c7136e9
10 changed files with 457 additions and 19 deletions

View File

@@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"strconv"
"time"
)
@@ -70,3 +71,29 @@ func SleepWithContext(ctx context.Context, d time.Duration) {
case <-timer.C:
}
}
// EnvInt parses an environment variable into an int; returns (value, true) when set and valid.
func EnvInt(key string) (int, bool) {
val := os.Getenv(key)
if val == "" {
return 0, false
}
parsed, err := strconv.Atoi(val)
if err != nil {
return 0, false
}
return parsed, true
}
// DurationFromEnv parses an environment variable representing seconds into a duration, defaulting when unset/invalid.
func DurationFromEnv(key string, fallback time.Duration) time.Duration {
val := os.Getenv(key)
if val == "" {
return fallback
}
seconds, err := strconv.ParseInt(val, 10, 64)
if err != nil || seconds <= 0 {
return fallback
}
return time.Duration(seconds) * time.Second
}