From f1ed637e3decb3b794c1034210f002839b0cd303 Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Fri, 15 Sep 2023 12:16:43 +0200 Subject: [PATCH] feat: cache OPL when loading from HTTP(S) --- .../config/opl_config_namespace_watcher.go | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/driver/config/opl_config_namespace_watcher.go b/internal/driver/config/opl_config_namespace_watcher.go index f07e21344..4cc584501 100644 --- a/internal/driver/config/opl_config_namespace_watcher.go +++ b/internal/driver/config/opl_config_namespace_watcher.go @@ -4,11 +4,14 @@ package config import ( + "bytes" "context" "fmt" "io" "sync" + "time" + "github.com/dgraph-io/ristretto" "github.com/ory/x/logrusx" "github.com/ory/x/urlx" "github.com/ory/x/watcherx" @@ -33,7 +36,14 @@ type ( } ) -var _ namespace.Manager = (*oplConfigWatcher)(nil) +var ( + _ namespace.Manager = (*oplConfigWatcher)(nil) + cache, _ = ristretto.NewCache(&ristretto.Config{ + MaxCost: 20_000_000, // 20 MB max size, each item ca. 10 KB => max 2000 items + NumCounters: 20_000, // max 2000 items => 20000 counters + BufferItems: 64, + }) +) func newOPLConfigWatcher(ctx context.Context, c *Config, target string) (*oplConfigWatcher, error) { nw := &oplConfigWatcher{ @@ -51,7 +61,7 @@ func newOPLConfigWatcher(ctx context.Context, c *Config, target string) (*oplCon switch targetUrl.Scheme { case "file", "": return nw, watchTarget(ctx, target, nw, c.l) - case "http", "https", "base64": + case "base64": file, err := c.Fetcher().FetchContext(ctx, target) if err != nil { return nil, err @@ -59,6 +69,20 @@ func newOPLConfigWatcher(ctx context.Context, c *Config, target string) (*oplCon nw.files.byPath[targetUrl.String()] = file nw.parseFiles() return nw, err + case "http", "https": + var file *bytes.Buffer + if item, ok := cache.Get(target); ok { + file = item.(*bytes.Buffer) + } else { + file, err = c.Fetcher().FetchContext(ctx, target) + if err != nil { + return nil, err + } + cache.SetWithTTL(target, file, int64(file.Cap()), 30*time.Minute) + } + nw.files.byPath[targetUrl.String()] = file + nw.parseFiles() + return nw, err default: return nil, fmt.Errorf("unexpected url scheme: %q", targetUrl.Scheme) }