-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move everything common between cla and mesh cache in a common cache i…
…mplementation Signed-off-by: Charly Molter <charly@koyeb.com>
- Loading branch information
Charly Molter
committed
Apr 28, 2021
1 parent
18baef9
commit e45e156
Showing
10 changed files
with
278 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package once | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/kumahq/kuma/pkg/metrics" | ||
) | ||
|
||
type Cache struct { | ||
cache *cache.Cache | ||
onceMap *omap | ||
metrics *prometheus.GaugeVec | ||
} | ||
|
||
func New(expirationTime time.Duration, name string, metrics metrics.Metrics) (*Cache, error) { | ||
metric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: name, | ||
Help: "Summary of " + name, | ||
}, []string{"operation", "result"}) | ||
if err := metrics.Register(metric); err != nil { | ||
return nil, err | ||
} | ||
return &Cache{ | ||
cache: cache.New(expirationTime, time.Duration(int64(float64(expirationTime)*0.9))), | ||
onceMap: newMap(), | ||
metrics: metric, | ||
}, nil | ||
} | ||
|
||
func (c *Cache) Get(ctx context.Context, key string, fn func(context.Context, string) (interface{}, error)) (interface{}, error) { | ||
v, found := c.cache.Get(key) | ||
if found { | ||
c.metrics.WithLabelValues("get", "hit").Inc() | ||
return v, nil | ||
} | ||
o := c.onceMap.Get(key) | ||
c.metrics.WithLabelValues("get", "hit-wait").Inc() | ||
o.Do(func() (interface{}, error) { | ||
defer c.onceMap.Delete(key) | ||
c.metrics.WithLabelValues("get", "hit-wait").Dec() | ||
c.metrics.WithLabelValues("get", "miss").Inc() | ||
res, err := fn(ctx, key) | ||
if err != nil { | ||
c.metrics.WithLabelValues("get", "error").Inc() | ||
return nil, err | ||
} | ||
c.cache.SetDefault(key, res) | ||
return res, nil | ||
}) | ||
if o.Err != nil { | ||
return "", o.Err | ||
} | ||
return o.Value, nil | ||
} |
Oops, something went wrong.