diff --git a/app/api/api.go b/app/api/api.go index a1062800..7a1f39fc 100644 --- a/app/api/api.go +++ b/app/api/api.go @@ -6,6 +6,7 @@ import ( "fmt" "io" golog "log" + "math" gonet "net" gohttp "net/http" "net/url" @@ -1119,6 +1120,12 @@ func (a *api) start() error { }(ctx) } + if cfg.Debug.MemoryLimit > 0 { + debug.SetMemoryLimit(cfg.Debug.MemoryLimit * 1024 * 1024) + } else { + debug.SetMemoryLimit(math.MaxInt64) + } + // Start the restream processes restream.Start() diff --git a/config/config.go b/config/config.go index 1d1d6732..b8a5028e 100644 --- a/config/config.go +++ b/config/config.go @@ -232,6 +232,7 @@ func (d *Config) init() { // Debug d.vars.Register(value.NewBool(&d.Debug.Profiling, false), "debug.profiling", "CORE_DEBUG_PROFILING", nil, "Enable profiling endpoint on /profiling", false, false) d.vars.Register(value.NewInt(&d.Debug.ForceGC, 0), "debug.force_gc", "CORE_DEBUG_FORCEGC", nil, "Number of seconds between forcing GC to return memory to the OS", false, false) + d.vars.Register(value.NewInt64(&d.Debug.MemoryLimit, 0), "debug.memory_limit_mbytes", "CORE_DEBUG_MEMORY_LIMIT_MBYTES", nil, "Impose a soft memory limit for the core, in megabytes", false, false) // Metrics d.vars.Register(value.NewBool(&d.Metrics.Enable, false), "metrics.enable", "CORE_METRICS_ENABLE", nil, "Enable collecting historic metrics data", false, false) diff --git a/config/data.go b/config/data.go index 012c964b..bb836b3f 100644 --- a/config/data.go +++ b/config/data.go @@ -135,8 +135,9 @@ type Data struct { MaxPort int `json:"max_port"` } `json:"playout"` Debug struct { - Profiling bool `json:"profiling"` - ForceGC int `json:"force_gc"` + Profiling bool `json:"profiling"` + ForceGC int `json:"force_gc"` + MemoryLimit int64 `json:"memory_limit_mbytes"` } `json:"debug"` Metrics struct { Enable bool `json:"enable"` @@ -189,7 +190,6 @@ func MergeV2toV3(data *Data, d *v2.Data) (*Data, error) { data.SRT = d.SRT data.FFmpeg = d.FFmpeg data.Playout = d.Playout - data.Debug = d.Debug data.Metrics = d.Metrics data.Sessions = d.Sessions data.Service = d.Service @@ -228,6 +228,10 @@ func MergeV2toV3(data *Data, d *v2.Data) (*Data, error) { data.Storage.Memory = d.Storage.Memory // Actual changes + data.Debug.Profiling = d.Debug.Profiling + data.Debug.ForceGC = d.Debug.ForceGC + data.Debug.MemoryLimit = 0 + data.TLS.Enable = d.TLS.Enable data.TLS.Address = d.TLS.Address data.TLS.Auto = d.TLS.Auto @@ -267,7 +271,6 @@ func DowngradeV3toV2(d *Data) (*v2.Data, error) { data.SRT = d.SRT data.FFmpeg = d.FFmpeg data.Playout = d.Playout - data.Debug = d.Debug data.Metrics = d.Metrics data.Sessions = d.Sessions data.Service = d.Service @@ -299,6 +302,9 @@ func DowngradeV3toV2(d *Data) (*v2.Data, error) { data.Router.Routes = copy.StringMap(d.Router.Routes) // Actual changes + data.Debug.Profiling = d.Debug.Profiling + data.Debug.ForceGC = d.Debug.ForceGC + data.TLS.Enable = d.TLS.Enable data.TLS.Address = d.TLS.Address data.TLS.Auto = d.TLS.Auto