Skip to content

Commit

Permalink
fix(plugins): traefik surrogate-keys (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Feb 9, 2024
1 parent 3acd7f2 commit 8bbbd77
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
17 changes: 13 additions & 4 deletions plugins/traefik/override/storage/cacheProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ type Cache struct {
stale time.Duration
}

var sharedCache *Cache

// CacheConnectionFactory function create new Cache instance
func CacheConnectionFactory(c t.AbstractConfigurationInterface) (types.Storer, error) {
provider := cache.New(1 * time.Second)
return &Cache{Cache: provider, stale: c.GetDefaultCache().GetStale()}, nil

if sharedCache == nil {
sharedCache = &Cache{Cache: provider, stale: c.GetDefaultCache().GetStale()}
}

return sharedCache, nil
}

// Name returns the storer name
Expand All @@ -44,10 +51,12 @@ func (provider *Cache) ListKeys() []string {

// MapKeys method returns the map of existing keys
func (provider *Cache) MapKeys(prefix string) map[string]string {
var keys map[string]string
keys := map[string]string{}
provider.Cache.Range(func(key, value interface{}) bool {
k, _ := strings.CutPrefix(key.(string), prefix)
keys[k] = value.(string)
if strings.HasPrefix(key.(string), prefix) {
k, _ := strings.CutPrefix(key.(string), prefix)
keys[k] = string(value.([]byte))
}
return true
})

Expand Down
29 changes: 18 additions & 11 deletions plugins/traefik/override/surrogate/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"sync"
"time"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/storage"
Expand All @@ -15,6 +16,7 @@ import (

const (
cdnCacheControl = "CDN-Cache-Control"
cacheGroupKey = "Cache-Groups"
surrogateKey = "Surrogate-Key"
surrogateControl = "Surrogate-Control"
cacheControl = "Cache-Control"
Expand All @@ -26,9 +28,14 @@ const (
cacheTags = "Cache-Tags"
cacheTag = "Cache-Tag"

stalePrefix = "STALE_"
stalePrefix = "STALE_"
surrogatePrefix = "SURROGATE_"
)

var storageToInfiniteTTLMap = map[string]time.Duration{
"CACHE": 365 * 24 * time.Hour,
}

func (s *baseStorage) ParseHeaders(value string) []string {
return regexp.MustCompile(s.parent.getHeaderSeparator()+" *").Split(value, -1)
}
Expand Down Expand Up @@ -73,6 +80,7 @@ type baseStorage struct {
dynamic bool
keepStale bool
mu *sync.Mutex
duration time.Duration
}

func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterface) {
Expand Down Expand Up @@ -109,17 +117,16 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf
s.dynamic = config.GetDefaultCache().GetCDN().Dynamic
s.keysRegexp = keysRegexp
s.mu = &sync.Mutex{}
s.duration = storageToInfiniteTTLMap[s.Storage.Name()]
}

func (s *baseStorage) storeTag(tag string, cacheKey string, re *regexp.Regexp) {
defer s.mu.Unlock()
s.mu.Lock()
currentValue := string(s.Storage.Get(tag))
if s.dynamic {
if !re.MatchString(currentValue) {
fmt.Printf("Store the tag %s", tag)
_ = s.Storage.Set("SURROGATE_"+tag, []byte(currentValue+souinStorageSeparator+cacheKey), configurationtypes.URL{}, -1)
}
currentValue := string(s.Storage.Get(surrogatePrefix + tag))
if !re.MatchString(currentValue) {
fmt.Printf("Store the tag %s", tag)
_ = s.Storage.Set(surrogatePrefix+tag, []byte(currentValue+souinStorageSeparator+cacheKey), configurationtypes.URL{}, -1)
}
}

Expand Down Expand Up @@ -172,11 +179,11 @@ func (s *baseStorage) getSurrogateKey(header http.Header) string {
func (s *baseStorage) purgeTag(tag string) []string {
toInvalidate := string(s.Storage.Get(tag))
fmt.Printf("Purge the tag %s", tag)
s.Storage.Delete("SURROGATE_" + tag)
s.Storage.Delete(surrogatePrefix + tag)
if !s.keepStale {
toInvalidate = toInvalidate + "," + string(s.Storage.Get(stalePrefix+tag))
fmt.Printf("Purge the tag %s", stalePrefix+tag)
s.Storage.Delete("SURROGATE_" + stalePrefix + tag)
s.Storage.Delete(surrogatePrefix + stalePrefix + tag)
}
return strings.Split(toInvalidate, souinStorageSeparator)
}
Expand Down Expand Up @@ -240,12 +247,12 @@ func (s *baseStorage) Invalidate(method string, headers http.Header) {

// List returns the stored keys associated to resources
func (s *baseStorage) List() map[string]string {
return s.Storage.MapKeys("SURROGATE_")
return s.Storage.MapKeys(surrogatePrefix)
}

// Destruct method will shutdown properly the provider
func (s *baseStorage) Destruct() error {
s.Storage.DeleteMany("SURROGATE_.*")
s.Storage.DeleteMany(surrogatePrefix + ".*")

return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8bbbd77

Please sign in to comment.