Skip to content

Commit 7649435

Browse files
authored
feat(core): upstream select it's own storage (#582)
* feat(core): upstream select it's own storage * fix(ci): vendor and use sync.Mutex instead of reference
1 parent 0e1579f commit 7649435

File tree

9 files changed

+58
-34
lines changed

9 files changed

+58
-34
lines changed

pkg/middleware/middleware.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -358,26 +358,51 @@ func (s *SouinBaseHandler) Store(
358358
hn := strings.Split(hname, ":")
359359
vhs.Set(hn[0], rq.Header.Get(hn[0]))
360360
}
361-
for _, storer := range s.Storers {
362-
wg.Add(1)
363-
go func(currentStorer types.Storer, currentRes http.Response) {
364-
defer wg.Done()
365-
if currentStorer.SetMultiLevel(
366-
cachedKey,
367-
variedKey,
368-
response,
369-
vhs,
370-
currentRes.Header.Get("Etag"), ma,
371-
variedKey,
372-
) == nil {
373-
s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name())
374-
currentRes.Request = rq
375-
} else {
376-
mu.Lock()
377-
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
378-
mu.Unlock()
361+
if upstreamStorerTarget := res.Header.Get("X-Souin-Storer"); upstreamStorerTarget != "" {
362+
res.Header.Del("X-Souin-Storer")
363+
364+
var overridedStorer types.Storer
365+
for _, storer := range s.Storers {
366+
if strings.Contains(strings.ToLower(storer.Name()), strings.ToLower(upstreamStorerTarget)) {
367+
overridedStorer = storer
379368
}
380-
}(storer, res)
369+
}
370+
371+
if overridedStorer.SetMultiLevel(
372+
cachedKey,
373+
variedKey,
374+
response,
375+
vhs,
376+
res.Header.Get("Etag"), ma,
377+
variedKey,
378+
) == nil {
379+
s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, overridedStorer.Name())
380+
res.Request = rq
381+
} else {
382+
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", overridedStorer.Name()))
383+
}
384+
} else {
385+
for _, storer := range s.Storers {
386+
wg.Add(1)
387+
go func(currentStorer types.Storer, currentRes http.Response) {
388+
defer wg.Done()
389+
if currentStorer.SetMultiLevel(
390+
cachedKey,
391+
variedKey,
392+
response,
393+
vhs,
394+
currentRes.Header.Get("Etag"), ma,
395+
variedKey,
396+
) == nil {
397+
s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name())
398+
currentRes.Request = rq
399+
} else {
400+
mu.Lock()
401+
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
402+
mu.Unlock()
403+
}
404+
}(storer, res)
405+
}
381406
}
382407

383408
wg.Wait()

pkg/middleware/writer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewCustomWriter(rq *http.Request, rw http.ResponseWriter, b *bytes.Buffer)
2424
Req: rq,
2525
Rw: rw,
2626
Headers: http.Header{},
27-
mutex: &sync.Mutex{},
27+
mutex: sync.Mutex{},
2828
}
2929
}
3030

@@ -35,7 +35,7 @@ type CustomWriter struct {
3535
Req *http.Request
3636
Headers http.Header
3737
headersSent bool
38-
mutex *sync.Mutex
38+
mutex sync.Mutex
3939
statusCode int
4040
}
4141

pkg/surrogate/providers/common.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type baseStorage struct {
104104
dynamic bool
105105
keepStale bool
106106
logger core.Logger
107-
mu *sync.Mutex
107+
mu sync.Mutex
108108
duration time.Duration
109109
}
110110

@@ -159,7 +159,6 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf
159159
s.dynamic = config.GetDefaultCache().GetCDN().Dynamic
160160
s.logger = config.GetLogger()
161161
s.keysRegexp = keysRegexp
162-
s.mu = &sync.Mutex{}
163162
s.duration = storageToInfiniteTTLMap[s.Storage.Name()]
164163
}
165164

pkg/surrogate/providers/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func mockCommonProvider() *baseStorage {
3030
Keys: make(map[string]configurationtypes.SurrogateKeys),
3131
keysRegexp: make(map[string]keysRegexpInner),
3232
dynamic: true,
33-
mu: &sync.Mutex{},
33+
mu: sync.Mutex{},
3434
logger: zap.NewNop().Sugar(),
3535
},
3636
}

plugins/traefik/override/middleware/writer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewCustomWriter(rq *http.Request, rw http.ResponseWriter, b *bytes.Buffer)
2323
Req: rq,
2424
Rw: rw,
2525
Headers: http.Header{},
26-
mutex: &sync.Mutex{},
26+
mutex: sync.Mutex{},
2727
}
2828
}
2929

@@ -34,7 +34,7 @@ type CustomWriter struct {
3434
Req *http.Request
3535
Headers http.Header
3636
headersSent bool
37-
mutex *sync.Mutex
37+
mutex sync.Mutex
3838
statusCode int
3939
}
4040

plugins/traefik/override/surrogate/providers/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type baseStorage struct {
7979
keysRegexp map[string]keysRegexpInner
8080
dynamic bool
8181
keepStale bool
82-
mu *sync.Mutex
82+
mu sync.Mutex
8383
duration time.Duration
8484
}
8585

@@ -116,7 +116,7 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf
116116

117117
s.dynamic = config.GetDefaultCache().GetCDN().Dynamic
118118
s.keysRegexp = keysRegexp
119-
s.mu = &sync.Mutex{}
119+
s.mu = sync.Mutex{}
120120
s.duration = storageToInfiniteTTLMap[s.Storage.Name()]
121121
}
122122

plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/writer.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/traefik/vendor/github.com/darkweak/souin/pkg/surrogate/providers/common.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/tyk/override/cache/surrogate/providers/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type baseStorage struct {
7272
dynamic bool
7373
keepStale bool
7474
logger *zap.Logger
75-
mu *sync.Mutex
75+
mu sync.Mutex
7676
}
7777

7878
func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterface) {
@@ -103,7 +103,7 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf
103103
s.dynamic = config.GetDefaultCache().GetCDN().Dynamic
104104
s.logger = config.GetLogger()
105105
s.keysRegexp = keysRegexp
106-
s.mu = &sync.Mutex{}
106+
s.mu = sync.Mutex{}
107107
}
108108

109109
func (s *baseStorage) storeTag(tag string, cacheKey string, re *regexp.Regexp) {

0 commit comments

Comments
 (0)