Skip to content

Commit

Permalink
feat(tyk): ci lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Apr 22, 2024
1 parent 04defb0 commit 6a3aa6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
17 changes: 7 additions & 10 deletions plugins/tyk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/darkweak/souin/context"
"github.com/darkweak/souin/pkg/middleware"
"github.com/darkweak/souin/pkg/rfc"
"github.com/darkweak/souin/pkg/storage"
"github.com/darkweak/souin/pkg/storage/types"
"github.com/pquerna/cachecontrol/cacheobject"
)
Expand Down Expand Up @@ -176,6 +175,7 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {

requestCc, coErr := cacheobject.ParseRequestCacheControl(rq.Header.Get("Cache-Control"))

modeContext := rq.Context().Value(context.Mode).(*context.ModeContext)
if coErr != nil || requestCc == nil {
rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=CACHE-CONTROL-EXTRACTION-ERROR")

Expand All @@ -199,12 +199,13 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {
fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator)

if fresh != nil || stale != nil {
s.Configuration.GetLogger().Sugar().Debugf("Found at least one valid response in the %s storage", currentStorer.Name())
fmt.Printf("Found at least one valid response in the %s storage\n", currentStorer.Name())
break
}
}

if response != nil && rfc.ValidateCacheControl(response, requestCc) {
if fresh != nil && (!modeContext.Strict || rfc.ValidateCacheControl(fresh, requestCc)) {
response := fresh
rfc.SetCacheStatusHeader(response)
if rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
for hn, hv := range response.Header {
Expand All @@ -214,13 +215,9 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {

return
}
} else if response == nil && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
for _, currentStorer := range s.SouinBaseHandler.Storers {
response = currentStorer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
if response != nil {
break
}
}
} else if !requestCc.OnlyIfCached && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
response := stale

if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
Expand Down
26 changes: 15 additions & 11 deletions plugins/tyk/override/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/cespare/xxhash"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/context"
"github.com/darkweak/souin/helpers"
Expand Down Expand Up @@ -272,15 +273,22 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
var response *http.Response
var fresh, stale *http.Response
finalKey := cachedKey
if rq.Context().Value(context.Hashed).(bool) {
finalKey = fmt.Sprint(xxhash.Sum64String(finalKey))
}
for _, currentStorer := range s.Storers {
response = currentStorer.Prefix(cachedKey, rq, validator)
if response != nil {
s.Configuration.GetLogger().Sugar().Debugf("Found response in the %s storage", currentStorer.Name())
fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator)

if fresh != nil || stale != nil {
s.Configuration.GetLogger().Sugar().Debugf("Found at least one valid response in the %s storage", currentStorer.Name())
break
}
}

if response != nil && rfc.ValidateCacheControl(response, requestCc) {
if rfc.ValidateCacheControl(response, requestCc) {
response := fresh
rfc.SetCacheStatusHeader(response)
if rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
customWriter.Headers = response.Header
Expand All @@ -290,13 +298,9 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques

return nil
}
} else if response == nil && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) {
for _, currentStorer := range s.Storers {
response = currentStorer.Prefix(storage.StalePrefix+cachedKey, rq, validator)
if response != nil {
break
}
}
} else if requestCc.MaxStaleSet || requestCc.MaxStale > -1 {
response := stale

if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
Expand Down

0 comments on commit 6a3aa6a

Please sign in to comment.