From 2dfba3d2ccfea1e65ed7ceecfd289b6cf29d2ed7 Mon Sep 17 00:00:00 2001 From: Andrey Bobelev Date: Fri, 29 Aug 2025 14:20:07 +0200 Subject: [PATCH] chore: make reg cache expiry tunable Mostly for the tests, opts: - tuning.register_cache_expiration - tuning.register_cache_cleanup --- hscontrol/state/state.go | 14 ++++++++++++-- hscontrol/types/config.go | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/hscontrol/state/state.go b/hscontrol/state/state.go index 15597706..b4baf7b5 100644 --- a/hscontrol/state/state.go +++ b/hscontrol/state/state.go @@ -74,9 +74,19 @@ type State struct { // NewState creates and initializes a new State instance, setting up the database, // IP allocator, DERP map, policy manager, and loading existing users and nodes. func NewState(cfg *types.Config) (*State, error) { + cacheExpiration := registerCacheExpiration + if cfg.Tuning.RegisterCacheExpiration != 0 { + cacheExpiration = cfg.Tuning.RegisterCacheExpiration + } + + cacheCleanup := registerCacheCleanup + if cfg.Tuning.RegisterCacheCleanup != 0 { + cacheCleanup = cfg.Tuning.RegisterCacheCleanup + } + registrationCache := zcache.New[types.RegistrationID, types.RegisterNode]( - registerCacheExpiration, - registerCacheCleanup, + cacheExpiration, + cacheCleanup, ) db, err := hsdb.NewHeadscaleDatabase( diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 4a0a366e..d4a7d662 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -235,6 +235,8 @@ type Tuning struct { BatchChangeDelay time.Duration NodeMapSessionBufferedChanSize int BatcherWorkers int + RegisterCacheCleanup time.Duration + RegisterCacheExpiration time.Duration } func validatePKCEMethod(method string) error { @@ -1002,6 +1004,8 @@ func LoadServerConfig() (*Config, error) { } return DefaultBatcherWorkers() }(), + RegisterCacheCleanup: viper.GetDuration("tuning.register_cache_cleanup"), + RegisterCacheExpiration: viper.GetDuration("tuning.register_cache_expiration"), }, }, nil }