Skip to content

Commit

Permalink
fix(plugin): tyk
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Sep 4, 2023
1 parent 5092d91 commit e58dbd1
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions plugins/tyk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,32 @@ func SouinResponseHandler(rw http.ResponseWriter, rs *http.Response, rq *http.Re
if err == nil {
variedHeaders := rfc.HeaderAllCommaSepValues(res.Header)
cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders)
if s.SouinBaseHandler.Storer.Set(cachedKey, response, currentMatchedURL, ma) == nil {
var wg sync.WaitGroup
mu := sync.Mutex{}
fails := []string{}
for _, storer := range s.Storers {
wg.Add(1)
go func(currentStorer storage.Storer) {
defer wg.Done()
if currentStorer.Set(cachedKey, response, currentMatchedURL, ma) == nil {
} else {
mu.Lock()
fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name()))
mu.Unlock()
}
}(storer)
}

wg.Wait()
if len(fails) < len(s.Storers) {
go func(rs http.Response, key string) {
_ = s.SurrogateKeyStorer.Store(&rs, key)
}(res, cachedKey)
status += "; stored"
} else {
status += "; detail=STORAGE-INSERTION-ERROR"
}

if len(fails) > 0 {
status += strings.Join(fails, "")
}
}
} else {
Expand Down Expand Up @@ -157,7 +179,13 @@ func SouinRequestHandler(rw http.ResponseWriter, rq *http.Request) {
defer s.bufPool.Put(bufPool)
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
response := s.SouinBaseHandler.Storer.Prefix(cachedKey, rq, validator)
var response *http.Response
for _, currentStorer := range s.Storers {
response = currentStorer.Prefix(cachedKey, rq, validator)
if response != nil {
break
}
}

if response != nil && rfc.ValidateCacheControl(response, requestCc) {
rfc.SetCacheStatusHeader(response)
Expand All @@ -170,7 +198,12 @@ func SouinRequestHandler(rw http.ResponseWriter, rq *http.Request) {
return
}
} else if response == nil && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
response := s.SouinBaseHandler.Storer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
for _, currentStorer := range s.Storers {
response = currentStorer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
if response != nil {
break
}
}
if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
Expand Down

0 comments on commit e58dbd1

Please sign in to comment.