Skip to content

Commit 2bd2071

Browse files
committed
fix(configuration): load mapping_eviction_interval properly
1 parent 2052932 commit 2bd2071

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

pkg/middleware/middleware.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,20 @@ func tryAcquireEvictionLock(storer types.Storer) bool {
9595
func registerMappingKeysEviction(logger core.Logger, storers []types.Storer, interval time.Duration) {
9696
for _, storer := range storers {
9797
logger.Debugf("registering mapping eviction for storer %s (interval: %s)", storer.Name(), interval)
98-
go func(current types.Storer) {
98+
go func(current types.Storer, currentInterval time.Duration) {
9999
for {
100100
if !tryAcquireEvictionLock(current) {
101101
logger.Debugf("skipping mapping eviction for storer %s, another instance holds the lock", current.Name())
102-
time.Sleep(time.Minute)
102+
time.Sleep(currentInterval)
103+
103104
continue
104105
}
105106

106107
logger.Debugf("run mapping eviction for storer %s", current.Name())
107108
api.EvictMapping(current)
108-
<-time.After(interval)
109+
<-time.After(currentInterval)
109110
}
110-
}(storer)
111+
}(storer, interval)
111112
}
112113
}
113114

plugins/caddy/admin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
7070
TTL: configurationtypes.Duration{
7171
Duration: 120 * time.Second,
7272
},
73+
MappingEvictionInterval: configurationtypes.Duration{
74+
Duration: time.Hour,
75+
},
7376
},
7477
}
7578
a.InternalEndpointHandlers = api.GenerateHandlerMap(&config, currentApp.Storers, item.SurrogateStorage)

plugins/caddy/configuration.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type DefaultCache struct {
6060
Stale configurationtypes.Duration `json:"stale"`
6161
// Disable the coalescing system.
6262
DisableCoalescing bool `json:"disable_coalescing"`
63+
// MappingEvictionInterval interval between eviction
64+
MappingEvictionInterval configurationtypes.Duration `json:"mapping_eviction_interval"`
6365
}
6466

6567
// GetAllowedHTTPVerbs returns the allowed verbs to cache
@@ -107,6 +109,11 @@ func (d *DefaultCache) GetEtcd() configurationtypes.CacheProvider {
107109
return d.Etcd
108110
}
109111

112+
// GetMappingEvictionInterval returns the interval between eviction
113+
func (d *DefaultCache) GetMappingEvictionInterval() time.Duration {
114+
return d.MappingEvictionInterval.Duration
115+
}
116+
110117
// GetMode returns mdoe configuration
111118
func (d *DefaultCache) GetMode() string {
112119
return d.Mode
@@ -763,6 +770,12 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
763770
}
764771
case "disable_coalescing":
765772
cfg.DefaultCache.DisableCoalescing = true
773+
case "mapping_eviction_interval":
774+
args := h.RemainingArgs()
775+
interval, err := time.ParseDuration(args[0])
776+
if err == nil {
777+
cfg.DefaultCache.MappingEvictionInterval.Duration = interval
778+
}
766779
case "disable_surrogate_key":
767780
cfg.SurrogateKeyDisabled = true
768781
default:

plugins/caddy/httpcache.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
142142
}
143143
}
144144

145+
if app.DefaultCache.GetMappingEvictionInterval() == 0 {
146+
if s.Configuration.DefaultCache.GetMappingEvictionInterval() == 0 {
147+
app.DefaultCache.TTL = configurationtypes.Duration{Duration: time.Hour}
148+
}
149+
}
150+
145151
if s.Configuration.GetDefaultCache() == nil {
146152
s.Configuration.DefaultCache = DefaultCache{
147153
AllowedHTTPVerbs: app.DefaultCache.AllowedHTTPVerbs,
@@ -151,6 +157,7 @@ func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
151157
TTL: app.TTL,
152158
Stale: app.Stale,
153159
DefaultCacheControl: app.DefaultCacheControl,
160+
MappingEvictionInterval: app.MappingEvictionInterval,
154161
CacheName: app.CacheName,
155162
Timeout: app.Timeout,
156163
}
@@ -189,6 +196,9 @@ func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
189196
if dc.TTL.Duration == 0 {
190197
s.Configuration.DefaultCache.TTL = appDc.TTL
191198
}
199+
if dc.MappingEvictionInterval.Duration == 0 {
200+
s.Configuration.DefaultCache.MappingEvictionInterval = appDc.MappingEvictionInterval
201+
}
192202
if dc.Stale.Duration == 0 {
193203
s.Configuration.DefaultCache.Stale = appDc.Stale
194204
}
@@ -296,8 +306,9 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser, _ interface{}) (interfac
296306
TTL: configurationtypes.Duration{
297307
Duration: 120 * time.Second,
298308
},
299-
DefaultCacheControl: "",
300-
CacheName: "",
309+
MappingEvictionInterval: configurationtypes.Duration{Duration: time.Hour},
310+
DefaultCacheControl: "",
311+
CacheName: "",
301312
},
302313
URLs: make(map[string]configurationtypes.URL),
303314
}

plugins/souin/agnostic/configuration_parser.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ func parseDefaultCache(dcConfiguration map[string]interface{}) *configurationtyp
104104
Path: "",
105105
Configuration: nil,
106106
},
107-
Regex: configurationtypes.Regex{},
108-
TTL: configurationtypes.Duration{},
109-
DefaultCacheControl: "",
107+
Regex: configurationtypes.Regex{},
108+
TTL: configurationtypes.Duration{},
109+
MappingEvictionInterval: configurationtypes.Duration{Duration: time.Hour},
110+
DefaultCacheControl: "",
110111
}
111112
for defaultCacheK, defaultCacheV := range dcConfiguration {
112113
switch defaultCacheK {
@@ -195,6 +196,11 @@ func parseDefaultCache(dcConfiguration map[string]interface{}) *configurationtyp
195196
}
196197
}
197198
}
199+
case "mapping_eviction_interval":
200+
eviction, err := time.ParseDuration(defaultCacheV.(string))
201+
if err == nil {
202+
dc.MappingEvictionInterval = configurationtypes.Duration{Duration: eviction}
203+
}
198204
case "mode":
199205
dc.Mode, _ = defaultCacheV.(string)
200206
case "nats":

0 commit comments

Comments
 (0)