mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-29 15:59:10 +01:00
[management] Add configurable statement timeout to postgres store (#8381)
This commit is contained in:
@@ -3,7 +3,10 @@ package util
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Duration is used strictly for JSON requests/responses due to duration marshalling issues
|
||||
@@ -35,3 +38,16 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
return errors.New("invalid duration")
|
||||
}
|
||||
}
|
||||
|
||||
func DurationFromEnv(key string, fallback time.Duration) time.Duration {
|
||||
val := os.Getenv(key)
|
||||
if val == "" {
|
||||
return fallback
|
||||
}
|
||||
parsed, err := time.ParseDuration(val)
|
||||
if err != nil || parsed < 0 {
|
||||
log.Warnf("ignoring invalid %s value %q, using %v", key, val, fallback)
|
||||
return fallback
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDurationFromEnv(t *testing.T) {
|
||||
const key = "NB_TEST_DURATION"
|
||||
cases := map[string]time.Duration{
|
||||
"": time.Minute,
|
||||
"30s": 30 * time.Second,
|
||||
"0": 0,
|
||||
"soon": time.Minute,
|
||||
"-1s": time.Minute,
|
||||
}
|
||||
for value, want := range cases {
|
||||
t.Run(value, func(t *testing.T) {
|
||||
t.Setenv(key, value)
|
||||
assert.Equal(t, want, DurationFromEnv(key, time.Minute), "duration for %q", value)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user