From e0fec1240505defdc6ff1779047c2188b95de3b3 Mon Sep 17 00:00:00 2001 From: Chris Hager Date: Wed, 20 Nov 2024 14:11:10 +0100 Subject: [PATCH] config file: log_max_entries --- systemapi-config.toml | 3 +++ systemapi/config.go | 17 ++++++++++++----- systemapi/config_test.go | 7 +++++++ systemapi/server.go | 2 +- systemapi/vars.go | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/systemapi-config.toml b/systemapi-config.toml index 1b8b291..ad2fe65 100644 --- a/systemapi-config.toml +++ b/systemapi-config.toml @@ -5,6 +5,9 @@ pprof = true log_json = false log_debug = true +# Maximum number of entries in the log +log_max_entries = 1000 + # HTTP Basic Auth basic_auth_secret_path = "basic-auth-secret.txt" # basic auth is supported if a path is provided basic_auth_secret_salt = "D;%yL9TS:5PalS/d" # use a random string for the salt diff --git a/systemapi/config.go b/systemapi/config.go index cd00cfa..c6dfa56 100644 --- a/systemapi/config.go +++ b/systemapi/config.go @@ -7,11 +7,12 @@ import ( ) type systemAPIConfigGeneral struct { - ListenAddr string `toml:"listen_addr"` - PipeFile string `toml:"pipe_file"` - LogJSON bool `toml:"log_json"` - LogDebug bool `toml:"log_debug"` - EnablePprof bool `toml:"pprof"` // Enables pprof endpoints + ListenAddr string `toml:"listen_addr"` + PipeFile string `toml:"pipe_file"` + LogJSON bool `toml:"log_json"` + LogDebug bool `toml:"log_debug"` + EnablePprof bool `toml:"pprof"` // Enables pprof endpoints + LogMaxEntries int `toml:"log_max_entries"` // Maximum number of log entries BasicAuthSecretPath string `toml:"basic_auth_secret_path"` BasicAuthSecretSalt string `toml:"basic_auth_secret_salt"` @@ -41,6 +42,12 @@ func LoadConfig(content []byte) (*SystemAPIConfig, error) { if err != nil { return nil, err } + + // Apply default + if cfg.General.LogMaxEntries == 0 { + cfg.General.LogMaxEntries = DefaultLogMaxEntries + } + return cfg, nil } diff --git a/systemapi/config_test.go b/systemapi/config_test.go index 1b0af60..0029d05 100644 --- a/systemapi/config_test.go +++ b/systemapi/config_test.go @@ -14,3 +14,10 @@ func TestLoadConfig(t *testing.T) { require.NotEmpty(t, cfg.Actions) require.Equal(t, "echo test", cfg.Actions["echo_test"]) } + +func TestEmptyConfig(t *testing.T) { + cfg, err := LoadConfig([]byte{}) + require.NoError(t, err) + require.NotNil(t, cfg) + require.Equal(t, DefaultLogMaxEntries, cfg.General.LogMaxEntries) +} diff --git a/systemapi/server.go b/systemapi/server.go index eeb28ce..c4d5ccf 100644 --- a/systemapi/server.go +++ b/systemapi/server.go @@ -206,7 +206,7 @@ func (s *Server) addEvent(event Event) { // Add event to the list and prune if necessary s.eventsLock.Lock() s.events = append(s.events, event) - if len(s.events) > MaxEvents { + if len(s.events) > s.cfg.General.LogMaxEntries { s.events = s.events[1:] } s.eventsLock.Unlock() diff --git a/systemapi/vars.go b/systemapi/vars.go index 2439cb4..59732b8 100644 --- a/systemapi/vars.go +++ b/systemapi/vars.go @@ -2,4 +2,4 @@ package systemapi import "github.com/flashbots/system-api/common" -var MaxEvents = common.GetEnvInt("MAX_EVENTS", 1000) +var DefaultLogMaxEntries = common.GetEnvInt("MAX_EVENTS", 1000)