Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cache to hydrator #418

Merged
merged 11 commits into from
May 1, 2020
Prev Previous commit
Next Next commit
add cache_ttl config for hydrator
pike1212 committed Apr 24, 2020
commit 9aee5d1ae26a27365fd785d50607e3530792e713
6 changes: 6 additions & 0 deletions .schema/config.schema.json
Original file line number Diff line number Diff line change
@@ -843,6 +843,12 @@
"$ref": "#/definitions/retry"
}
}
},
"cache_ttl": {
pike1212 marked this conversation as resolved.
Show resolved Hide resolved
"type": "string",
"pattern": "^[0-9]+(ns|us|ms|s|m|h)$",
"title": "Cache Time to Live",
"description": "How long to cache hydrate calls"
}
},
"required": [
42 changes: 19 additions & 23 deletions pipeline/mutate/mutator_hydrator.go
Original file line number Diff line number Diff line change
@@ -60,8 +60,7 @@ type MutatorHydrator struct {
client *http.Client
d mutatorHydratorDependencies

hydrateCache *ristretto.Cache
hydrateCacheEnabled bool
hydrateCache *ristretto.Cache
}

type BasicAuth struct {
@@ -85,24 +84,21 @@ type externalAPIConfig struct {
}

type MutatorHydratorConfig struct {
Api externalAPIConfig `json:"api"`
Api externalAPIConfig `json:"api"`
CacheTtl string `json:"cache_ttl"`
}

type mutatorHydratorDependencies interface {
x.RegistryLogger
}

func (a *MutatorHydrator) SetCaching(cache bool) {
a.hydrateCacheEnabled = cache
}

func NewMutatorHydrator(c configuration.Provider, d mutatorHydratorDependencies) *MutatorHydrator {
cache, _ := ristretto.NewCache(&ristretto.Config{
NumCounters: 10000,
MaxCost: 1 << 25,
pike1212 marked this conversation as resolved.
Show resolved Hide resolved
BufferItems: 64,
})
return &MutatorHydrator{c: c, d: d, client: httpx.NewResilientClientLatencyToleranceSmall(nil), hydrateCache: cache, hydrateCacheEnabled: true}
return &MutatorHydrator{c: c, d: d, client: httpx.NewResilientClientLatencyToleranceSmall(nil), hydrateCache: cache}
}

func (a *MutatorHydrator) GetID() string {
@@ -116,10 +112,6 @@ func (a *MutatorHydrator) cacheKey(config *MutatorHydratorConfig, session *authn
}

func (a *MutatorHydrator) hydrateFromCache(config *MutatorHydratorConfig, session *authn.AuthenticationSession) (*authn.AuthenticationSession, bool) {
if !a.hydrateCacheEnabled {
return nil, false
}

key := a.cacheKey(config, session)

item, found := a.hydrateCache.Get(key)
@@ -131,13 +123,9 @@ func (a *MutatorHydrator) hydrateFromCache(config *MutatorHydratorConfig, sessio
return container, true
}

func (a *MutatorHydrator) hydrateToCache(config *MutatorHydratorConfig, session *authn.AuthenticationSession) {
if !a.hydrateCacheEnabled {
return
}

func (a *MutatorHydrator) hydrateToCache(config *MutatorHydratorConfig, session *authn.AuthenticationSession, ttl time.Duration) {
key := a.cacheKey(config, session)
cached := a.hydrateCache.SetWithTTL(key, session, 0, time.Minute*5)
cached := a.hydrateCache.SetWithTTL(key, session, 0, ttl)
if !cached {
a.d.Logger().Warn("Item not added to cache")
}
@@ -154,9 +142,11 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
return errors.WithStack(err)
}

if cacheSession, ok := a.hydrateFromCache(cfg, session); ok {
*session = *cacheSession
return nil
if cfg.CacheTtl != "" {
if cacheSession, ok := a.hydrateFromCache(cfg, session); ok {
*session = *cacheSession
return nil
}
}

if cfg.Api.URL == "" {
@@ -229,8 +219,14 @@ func (a *MutatorHydrator) Mutate(r *http.Request, session *authn.AuthenticationS
}
*session = sessionFromUpstream

// set in cache
a.hydrateToCache(cfg, session)
if cfg.CacheTtl != "" {
d, err := time.ParseDuration(cfg.CacheTtl)
if err != nil {
a.d.Logger().WithError(err).Error("Unable to parse cache_ttl in the Hydrator Mutator.")
return errors.WithStack(err)
}
a.hydrateToCache(cfg, session, d)
}

return nil
}