router handler: add cache for sd service

This commit is contained in:
ginuerzh
2025-02-09 16:16:44 +08:00
parent 08ad260606
commit fc12e33786
13 changed files with 98 additions and 74 deletions
+28 -19
View File
@@ -19,6 +19,7 @@ import (
"github.com/go-gost/core/sd"
"github.com/go-gost/relay"
xip "github.com/go-gost/x/internal/net/ip"
"github.com/go-gost/x/internal/util/cache"
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
"github.com/go-gost/x/registry"
@@ -189,25 +190,7 @@ func (h *routerHandler) handlePacket(ctx context.Context, data []byte, routerID
return nil
}
if h.md.sd == nil {
return nil
}
clientID := fmt.Sprintf("%s@%s", route.Gateway, routerID)
ss, _ := h.md.sd.Get(ctx, clientID)
var service *sd.Service
for _, s := range ss {
if s.Node != h.id {
service = s
break
}
}
if service == nil {
return nil
}
raddr, _ := net.ResolveUDPAddr("udp", service.Address)
raddr := h.getAddrforRoute(ctx, rid, route.Gateway)
if raddr == nil {
return nil
}
@@ -235,6 +218,32 @@ func (h *routerHandler) handlePacket(ctx context.Context, data []byte, routerID
return nil
}
func (h *routerHandler) getAddrforRoute(ctx context.Context, routerID, gateway string) net.Addr {
if h.md.sd == nil {
return nil
}
clientID := fmt.Sprintf("%s@%s", gateway, routerID)
if item := h.sdCache.Get(clientID); item != nil && !item.Expired() {
addr, _ := item.Value().(net.Addr)
return addr
}
ss, _ := h.md.sd.Get(ctx, clientID)
service := &sd.Service{}
for _, s := range ss {
if s.Node != h.id {
service = s
break
}
}
raddr, _ := net.ResolveUDPAddr("udp", service.Address)
h.sdCache.Set(clientID, cache.NewItem(raddr, h.md.sdCacheExpiration))
return raddr
}
type packetConn struct {
net.Conn
}
+8 -5
View File
@@ -17,9 +17,10 @@ import (
"github.com/go-gost/relay"
ctxvalue "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
limiter_util "github.com/go-gost/x/internal/util/limiter"
"github.com/go-gost/x/internal/util/cache"
stats_util "github.com/go-gost/x/internal/util/stats"
rate_limiter "github.com/go-gost/x/limiter/rate"
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
"github.com/go-gost/x/registry"
"github.com/google/uuid"
)
@@ -45,6 +46,7 @@ type routerHandler struct {
stats *stats_util.HandlerStats
limiter traffic.TrafficLimiter
cancel context.CancelFunc
sdCache *cache.Cache
}
func NewHandler(opts ...handler.Option) handler.Handler {
@@ -55,6 +57,7 @@ func NewHandler(opts ...handler.Option) handler.Handler {
return &routerHandler{
options: options,
sdCache: cache.NewCache(time.Minute),
}
}
@@ -87,10 +90,10 @@ func (h *routerHandler) Init(md md.Metadata) (err error) {
}
if h.options.Limiter != nil {
h.limiter = limiter_util.NewCachedTrafficLimiter(h.options.Limiter,
limiter_util.RefreshIntervalOption(h.md.limiterRefreshInterval),
limiter_util.CleanupIntervalOption(h.md.limiterCleanupInterval),
limiter_util.ScopeOption(limiter.ScopeClient),
h.limiter = cache_limiter.NewCachedTrafficLimiter(h.options.Limiter,
cache_limiter.RefreshIntervalOption(h.md.limiterRefreshInterval),
cache_limiter.CleanupIntervalOption(h.md.limiterCleanupInterval),
cache_limiter.ScopeOption(limiter.ScopeClient),
)
}
+14 -6
View File
@@ -12,18 +12,20 @@ import (
)
const (
defaultTTL = 15 * time.Second
defaultBufferSize = 4096
defaultTTL = 15 * time.Second
defaultBufferSize = 4096
defaultCacheExpiration = time.Second
)
type metadata struct {
readTimeout time.Duration
bufferSize int
entryPoint string
ingress ingress.Ingress
sd sd.SD
router router.Router
entryPoint string
ingress ingress.Ingress
sd sd.SD
sdCacheExpiration time.Duration
router router.Router
observerPeriod time.Duration
observerResetTraffic bool
@@ -41,7 +43,13 @@ func (h *routerHandler) parseMetadata(md mdata.Metadata) (err error) {
h.md.entryPoint = mdutil.GetString(md, "entrypoint")
h.md.ingress = registry.IngressRegistry().Get(mdutil.GetString(md, "ingress"))
h.md.sd = registry.SDRegistry().Get(mdutil.GetString(md, "sd"))
h.md.sdCacheExpiration = mdutil.GetDuration(md, "sd.cacheExpiration")
if h.md.sdCacheExpiration <= 0 {
h.md.sdCacheExpiration = defaultCacheExpiration
}
h.md.router = registry.RouterRegistry().Get(mdutil.GetString(md, "router"))
h.md.observerPeriod = mdutil.GetDuration(md, "observePeriod", "observer.period", "observer.observePeriod")