mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* fn: introduce agent config and minor ghostreader tweak TODO: move all constants/tweaks in agent to agent config. * fn: json convention
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type AgentConfig struct {
|
|
MinDockerVersion string `json:"min_docker_version"`
|
|
FreezeIdleMsecs time.Duration `json:"freeze_idle_msecs"`
|
|
EjectIdleMsecs time.Duration `json:"eject_idle_msecs"`
|
|
}
|
|
|
|
func NewAgentConfig() (*AgentConfig, error) {
|
|
|
|
var err error
|
|
|
|
cfg := &AgentConfig{
|
|
MinDockerVersion: "17.06.0-ce",
|
|
}
|
|
|
|
cfg.FreezeIdleMsecs, err = getEnvMsecs("FN_FREEZE_IDLE_MSECS", 50*time.Millisecond)
|
|
if err != nil {
|
|
return cfg, errors.New("error initializing freeze idle delay")
|
|
}
|
|
|
|
cfg.EjectIdleMsecs, err = getEnvMsecs("FN_EJECT_IDLE_MSECS", 1000*time.Millisecond)
|
|
if err != nil {
|
|
return cfg, errors.New("error initializing eject idle delay")
|
|
}
|
|
|
|
if cfg.EjectIdleMsecs == time.Duration(0) {
|
|
return cfg, errors.New("error eject idle delay cannot be zero")
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func getEnvMsecs(name string, defaultVal time.Duration) (time.Duration, error) {
|
|
|
|
delay := defaultVal
|
|
|
|
if dur := os.Getenv(name); dur != "" {
|
|
durInt, err := strconv.ParseInt(dur, 10, 64)
|
|
if err != nil {
|
|
return defaultVal, err
|
|
}
|
|
// disable if negative or set to msecs specified.
|
|
if durInt < 0 || time.Duration(durInt) >= math.MaxInt64/time.Millisecond {
|
|
delay = math.MaxInt64
|
|
} else {
|
|
delay = time.Duration(durInt) * time.Millisecond
|
|
}
|
|
}
|
|
|
|
return delay, nil
|
|
}
|