Skip to content

Commit 2052932

Browse files
cdaguerredarkweak
authored andcommitted
feat(eviction): make mapping keys eviction interval configurable
Add `mapping_eviction_interval` configuration option to control how often the mapping keys eviction coroutine runs. Defaults to 1 minute (preserving current behavior). Changes: - Add MappingEvictionInterval field to DefaultCache config - Replace time.Sleep with time.After for channel-based waiting - Move timing logic from EvictMapping to registerMappingKeysEviction Usage: default_cache: mapping_eviction_interval: 6h
1 parent 2a83544 commit 2052932

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

configurationtypes/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ type DefaultCache struct {
279279
DefaultCacheControl string `json:"default_cache_control" yaml:"default_cache_control"`
280280
MaxBodyBytes uint64 `json:"max_cacheable_body_bytes" yaml:"max_cacheable_body_bytes"`
281281
DisableCoalescing bool `json:"disable_coalescing" yaml:"disable_coalescing"`
282+
MappingEvictionInterval Duration `json:"mapping_eviction_interval" yaml:"mapping_eviction_interval"`
282283
}
283284

284285
// GetAllowedHTTPVerbs returns the allowed verbs to cache
@@ -401,6 +402,14 @@ func (d *DefaultCache) IsCoalescingDisable() bool {
401402
return d.DisableCoalescing
402403
}
403404

405+
// GetMappingEvictionInterval returns the interval for mapping eviction
406+
func (d *DefaultCache) GetMappingEvictionInterval() time.Duration {
407+
if d.MappingEvictionInterval.Duration == 0 {
408+
return time.Minute
409+
}
410+
return d.MappingEvictionInterval.Duration
411+
}
412+
404413
// DefaultCacheInterface interface
405414
type DefaultCacheInterface interface {
406415
GetAllowedHTTPVerbs() []string
@@ -427,6 +436,7 @@ type DefaultCacheInterface interface {
427436
GetDefaultCacheControl() string
428437
GetMaxBodyBytes() uint64
429438
IsCoalescingDisable() bool
439+
GetMappingEvictionInterval() time.Duration
430440
}
431441

432442
// APIEndpoint is the minimal structure to define an endpoint

pkg/api/souin.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ func EvictMapping(current types.Storer) {
199199
current.Delete(core.MappingKeyPrefix + k)
200200
}
201201
}
202-
time.Sleep(time.Minute)
203202
}
204203

205204
func (s *SouinAPI) purgeMapping() {

pkg/middleware/middleware.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func tryAcquireEvictionLock(storer types.Storer) bool {
9292
return string(verifyValue) == lockValue
9393
}
9494

95-
func registerMappingKeysEviction(logger core.Logger, storers []types.Storer) {
95+
func registerMappingKeysEviction(logger core.Logger, storers []types.Storer, interval time.Duration) {
9696
for _, storer := range storers {
97-
logger.Debugf("registering mapping eviction for storer %s", storer.Name())
97+
logger.Debugf("registering mapping eviction for storer %s (interval: %s)", storer.Name(), interval)
9898
go func(current types.Storer) {
9999
for {
100100
if !tryAcquireEvictionLock(current) {
@@ -105,6 +105,7 @@ func registerMappingKeysEviction(logger core.Logger, storers []types.Storer) {
105105

106106
logger.Debugf("run mapping eviction for storer %s", current.Name())
107107
api.EvictMapping(current)
108+
<-time.After(interval)
108109
}
109110
}(storer)
110111
}
@@ -198,7 +199,7 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S
198199
}
199200
c.GetLogger().Info("Souin configuration is now loaded.")
200201

201-
registerMappingKeysEviction(c.GetLogger(), storers)
202+
registerMappingKeysEviction(c.GetLogger(), storers, c.GetDefaultCache().GetMappingEvictionInterval())
202203

203204
return &SouinBaseHandler{
204205
Configuration: c,

0 commit comments

Comments
 (0)