refactor: remove redis direct references from proxy/admin servers
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
Cadoles/bouncer/pipeline/pr-master This commit looks good

This commit is contained in:
2025-08-13 16:54:47 +02:00
parent a50f926463
commit ad4f334bc2
16 changed files with 199 additions and 235 deletions

View File

@ -13,14 +13,12 @@ import (
"net/http/httputil"
"net/http/pprof"
"net/url"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"
"forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/cadoles/bouncer/internal/cache"
"forge.cadoles.com/cadoles/bouncer/internal/cache/memory"
"forge.cadoles.com/cadoles/bouncer/internal/cache/ttl"
bouncerChi "forge.cadoles.com/cadoles/bouncer/internal/chi"
@ -38,12 +36,14 @@ import (
)
type Server struct {
serverConfig config.ProxyServerConfig
redisConfig config.RedisConfig
directorLayers []director.Layer
directorCacheTTL time.Duration
proxyRepository store.ProxyRepository
layerRepository store.LayerRepository
serverConfig config.ProxyServerConfig
directorLayers []director.Layer
proxyRepository store.ProxyRepository
layerRepository store.LayerRepository
layerCache cache.Cache[string, []*store.Layer]
proxyCache cache.Cache[string, []*store.Proxy]
}
func (s *Server) Start(ctx context.Context) (<-chan net.Addr, <-chan error) {
@ -55,6 +55,11 @@ func (s *Server) Start(ctx context.Context) (<-chan net.Addr, <-chan error) {
return addrs, errs
}
func (s *Server) ClearCache() {
s.layerCache.Clear()
s.proxyCache.Clear()
}
func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan error) {
defer func() {
close(errs)
@ -64,12 +69,6 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
if err := s.initRepositories(ctx); err != nil {
errs <- errors.WithStack(err)
return
}
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.serverConfig.HTTP.Host, s.serverConfig.HTTP.Port))
if err != nil {
errs <- errors.WithStack(err)
@ -97,15 +96,12 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
logger.Info(ctx, "http server listening")
layerCache, proxyCache, cancel := s.createDirectorCaches(ctx)
defer cancel()
director := director.New(
s.proxyRepository,
s.layerRepository,
director.WithLayers(s.directorLayers...),
director.WithLayerCache(layerCache),
director.WithProxyCache(proxyCache),
director.WithLayerCache(s.layerCache),
director.WithProxyCache(s.proxyCache),
director.WithHandleErrorFunc(s.handleError),
)
@ -199,44 +195,6 @@ func (s *Server) run(parentCtx context.Context, addrs chan net.Addr, errs chan e
logger.Info(ctx, "http server exiting")
}
func (s *Server) createDirectorCaches(ctx context.Context) (*ttl.Cache[string, []*store.Layer], *ttl.Cache[string, []*store.Proxy], func()) {
layerCache := ttl.NewCache(
memory.NewCache[string, []*store.Layer](),
memory.NewCache[string, time.Time](),
s.directorCacheTTL,
)
proxyCache := ttl.NewCache(
memory.NewCache[string, []*store.Proxy](),
memory.NewCache[string, time.Time](),
s.directorCacheTTL,
)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGUSR2)
go func() {
for {
_, ok := <-sig
if !ok {
return
}
logger.Info(ctx, "received sigusr2 signal, clearing proxies and layers cache")
layerCache.Clear()
proxyCache.Clear()
}
}()
cancel := func() {
close(sig)
}
return layerCache, proxyCache, cancel
}
func (s *Server) createReverseProxy(ctx context.Context, target *url.URL) *httputil.ReverseProxy {
reverseProxy := httputil.NewSingleHostReverseProxy(target)
@ -345,9 +303,21 @@ func NewServer(funcs ...OptionFunc) *Server {
}
return &Server{
serverConfig: opt.ServerConfig,
redisConfig: opt.RedisConfig,
directorLayers: opt.DirectorLayers,
directorCacheTTL: opt.DirectorCacheTTL,
serverConfig: opt.ServerConfig,
directorLayers: opt.DirectorLayers,
proxyRepository: opt.ProxyRepository,
layerRepository: opt.LayerRepository,
layerCache: ttl.NewCache(
memory.NewCache[string, []*store.Layer](),
memory.NewCache[string, time.Time](),
opt.DirectorCacheTTL,
),
proxyCache: ttl.NewCache(
memory.NewCache[string, []*store.Proxy](),
memory.NewCache[string, time.Time](),
opt.DirectorCacheTTL,
),
}
}