From 59ecfa7b4e76e9eaf4c276897516f38f0d77d74e Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 18 Mar 2025 12:52:59 +0100 Subject: [PATCH] feat: prevent burst of proxy/layers update --- internal/command/server/admin/run.go | 4 --- internal/proxy/director/director.go | 44 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/internal/command/server/admin/run.go b/internal/command/server/admin/run.go index a6e8fd8..334ac5a 100644 --- a/internal/command/server/admin/run.go +++ b/internal/command/server/admin/run.go @@ -12,10 +12,6 @@ import ( "gitlab.com/wpetit/goweb/logger" ) -const ( - flagPrintDefaultToken = "print-default-token" -) - func RunCommand() *cli.Command { flags := common.Flags() diff --git a/internal/proxy/director/director.go b/internal/proxy/director/director.go index c334082..fe3cf5d 100644 --- a/internal/proxy/director/director.go +++ b/internal/proxy/director/director.go @@ -4,6 +4,7 @@ import ( "context" "net/http" "sort" + "sync" "forge.cadoles.com/Cadoles/go-proxy" "forge.cadoles.com/Cadoles/go-proxy/wildcard" @@ -23,6 +24,9 @@ type Director struct { proxyCache cache.Cache[string, []*store.Proxy] layerCache cache.Cache[string, []*store.Layer] + proxyCacheLock sync.RWMutex + layerCacheLock sync.RWMutex + handleError HandleErrorFunc } @@ -103,6 +107,26 @@ func (d *Director) getProxies(ctx context.Context) ([]*store.Proxy, error) { return proxies, nil } + locked := d.proxyCacheLock.TryLock() + if !locked { + d.proxyCacheLock.RLock() + + proxies, exists := d.proxyCache.Get(proxiesCacheKey) + if exists { + d.proxyCacheLock.RUnlock() + logger.Debug(ctx, "using cached proxies") + return proxies, nil + } + + d.proxyCacheLock.RUnlock() + } + + if !locked { + d.proxyCacheLock.Lock() + } + + defer d.proxyCacheLock.Unlock() + logger.Debug(ctx, "querying fresh proxies") headers, err := d.proxyRepository.QueryProxy(ctx, store.WithProxyQueryEnabled(true)) @@ -141,6 +165,26 @@ func (d *Director) getLayers(ctx context.Context, proxyName store.ProxyName) ([] return layers, nil } + locked := d.layerCacheLock.TryLock() + if !locked { + d.layerCacheLock.RLock() + + layers, exists := d.layerCache.Get(cacheKey) + if exists { + d.layerCacheLock.RUnlock() + logger.Debug(ctx, "using cached layers") + return layers, nil + } + + d.layerCacheLock.RUnlock() + } + + if !locked { + d.layerCacheLock.Lock() + } + + defer d.layerCacheLock.Unlock() + logger.Debug(ctx, "querying fresh layers") headers, err := d.layerRepository.QueryLayers(ctx, proxyName, store.WithLayerQueryEnabled(true))