Compare commits
2 Commits
v2025.3.10
...
v2025.3.18
Author | SHA1 | Date | |
---|---|---|---|
1af7248a6f | |||
8b132dddd4 |
1
internal/cache/cache.go
vendored
1
internal/cache/cache.go
vendored
@ -3,4 +3,5 @@ package cache
|
||||
type Cache[K comparable, V any] interface {
|
||||
Get(key K) (V, bool)
|
||||
Set(key K, value V)
|
||||
Clear()
|
||||
}
|
||||
|
4
internal/cache/memory/cache.go
vendored
4
internal/cache/memory/cache.go
vendored
@ -25,6 +25,10 @@ func (c *Cache[K, V]) Set(key K, value V) {
|
||||
c.store.Store(key, value)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Clear() {
|
||||
c.store.Clear()
|
||||
}
|
||||
|
||||
func NewCache[K comparable, V any]() *Cache[K, V] {
|
||||
return &Cache[K, V]{
|
||||
store: new(sync.Map),
|
||||
|
5
internal/cache/ttl/cache.go
vendored
5
internal/cache/ttl/cache.go
vendored
@ -28,6 +28,11 @@ func (c *Cache[K, V]) Set(key K, value V) {
|
||||
c.values.Set(key, value)
|
||||
}
|
||||
|
||||
func (c *Cache[K, V]) Clear() {
|
||||
c.timestamps.Clear()
|
||||
c.values.Clear()
|
||||
}
|
||||
|
||||
func NewCache[K comparable, V any](values cache.Cache[K, V], timestamps cache.Cache[K, time.Time], ttl time.Duration) *Cache[K, V] {
|
||||
return &Cache[K, V]{
|
||||
values: values,
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
)
|
||||
@ -17,6 +18,7 @@ const (
|
||||
contextKeyLayers contextKey = "layers"
|
||||
contextKeyOriginalURL contextKey = "originalURL"
|
||||
contextKeyHandleError contextKey = "handleError"
|
||||
contextKeySentryScope contextKey = "sentryScope"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -82,3 +84,16 @@ func HandleError(ctx context.Context, w http.ResponseWriter, r *http.Request, st
|
||||
|
||||
fn(w, r, status, err)
|
||||
}
|
||||
|
||||
func withSentryScope(ctx context.Context, scope *sentry.Scope) context.Context {
|
||||
return context.WithValue(ctx, contextKeySentryScope, scope)
|
||||
}
|
||||
|
||||
func SentryScope(ctx context.Context) (*sentry.Scope, error) {
|
||||
scope, err := ctxValue[*sentry.Scope](ctx, contextKeySentryScope)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return scope, nil
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"gitlab.com/wpetit/goweb/logger"
|
||||
@ -76,6 +77,13 @@ func (d *Director) rewriteRequest(r *http.Request) (*http.Request, error) {
|
||||
proxyCtx = withLayers(proxyCtx, layers)
|
||||
r = r.WithContext(proxyCtx)
|
||||
|
||||
if sentryScope, _ := SentryScope(ctx); sentryScope != nil {
|
||||
sentryScope.SetContext("bouncer", sentry.Context{
|
||||
"proxy_name": p.Name,
|
||||
"proxy_target": r.URL.String(),
|
||||
})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
@ -91,9 +99,12 @@ const proxiesCacheKey = "proxies"
|
||||
func (d *Director) getProxies(ctx context.Context) ([]*store.Proxy, error) {
|
||||
proxies, exists := d.proxyCache.Get(proxiesCacheKey)
|
||||
if exists {
|
||||
logger.Debug(ctx, "using cached proxies")
|
||||
return proxies, nil
|
||||
}
|
||||
|
||||
logger.Debug(ctx, "querying fresh proxies")
|
||||
|
||||
headers, err := d.proxyRepository.QueryProxy(ctx, store.WithProxyQueryEnabled(true))
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
@ -126,9 +137,12 @@ func (d *Director) getLayers(ctx context.Context, proxyName store.ProxyName) ([]
|
||||
|
||||
layers, exists := d.layerCache.Get(cacheKey)
|
||||
if exists {
|
||||
logger.Debug(ctx, "using cached layers")
|
||||
return layers, nil
|
||||
}
|
||||
|
||||
logger.Debug(ctx, "querying fresh layers")
|
||||
|
||||
headers, err := d.layerRepository.QueryLayers(ctx, proxyName, store.WithLayerQueryEnabled(true))
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
@ -216,7 +230,9 @@ func (d *Director) ResponseTransformer() proxy.ResponseTransformer {
|
||||
func (d *Director) Middleware() proxy.Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
sentry.ConfigureScope(func(scope *sentry.Scope) {
|
||||
ctx := withHandleError(r.Context(), d.handleError)
|
||||
ctx = withSentryScope(ctx, scope)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
r, err := d.rewriteRequest(r)
|
||||
@ -250,6 +266,7 @@ func (d *Director) Middleware() proxy.Middleware {
|
||||
handler := createMiddlewareChain(next, httpMiddlewares)
|
||||
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
|
@ -13,8 +13,11 @@ import (
|
||||
"net/http/httputil"
|
||||
"net/http/pprof"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Cadoles/go-proxy"
|
||||
@ -94,24 +97,15 @@ 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(
|
||||
ttl.NewCache(
|
||||
memory.NewCache[string, []*store.Layer](),
|
||||
memory.NewCache[string, time.Time](),
|
||||
s.directorCacheTTL,
|
||||
),
|
||||
),
|
||||
director.WithProxyCache(
|
||||
ttl.NewCache(
|
||||
memory.NewCache[string, []*store.Proxy](),
|
||||
memory.NewCache[string, time.Time](),
|
||||
s.directorCacheTTL,
|
||||
),
|
||||
),
|
||||
director.WithLayerCache(layerCache),
|
||||
director.WithProxyCache(proxyCache),
|
||||
director.WithHandleErrorFunc(s.handleError),
|
||||
)
|
||||
|
||||
@ -205,6 +199,44 @@ 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)
|
||||
|
||||
|
@ -131,6 +131,12 @@ proxy:
|
||||
# Les proxys/layers sont mis en cache local pour une durée de 30s
|
||||
# par défaut. Si les modifications sont rares, vous pouvez augmenter
|
||||
# cette valeur pour réduire la "pression" sur le serveur Redis.
|
||||
# Il est possible de forcer la réinitialisation du cache en envoyant
|
||||
# le signal SIGUSR2 au processus Bouncer.
|
||||
#
|
||||
# Exemple
|
||||
#
|
||||
# kill -s USR2 $(pgrep bouncer)
|
||||
ttl: 30s
|
||||
|
||||
# Configuration du transport HTTP(S)
|
||||
|
Reference in New Issue
Block a user