router handler: add cache for sd service
This commit is contained in:
@@ -35,12 +35,12 @@ import (
|
||||
xio "github.com/go-gost/x/internal/io"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
xhttp "github.com/go-gost/x/internal/net/http"
|
||||
limiter_util "github.com/go-gost/x/internal/util/limiter"
|
||||
"github.com/go-gost/x/internal/util/sniffing"
|
||||
stats_util "github.com/go-gost/x/internal/util/stats"
|
||||
tls_util "github.com/go-gost/x/internal/util/tls"
|
||||
ws_util "github.com/go-gost/x/internal/util/ws"
|
||||
rate_limiter "github.com/go-gost/x/limiter/rate"
|
||||
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
|
||||
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
xstats "github.com/go-gost/x/observer/stats"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
@@ -90,10 +90,10 @@ func (h *httpHandler) Init(md md.Metadata) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@ import (
|
||||
xio "github.com/go-gost/x/internal/io"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
xhttp "github.com/go-gost/x/internal/net/http"
|
||||
limiter_util "github.com/go-gost/x/internal/util/limiter"
|
||||
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"
|
||||
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
@@ -78,10 +78,10 @@ func (h *http2Handler) Init(md md.Metadata) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,10 @@ import (
|
||||
"github.com/go-gost/core/recorder"
|
||||
"github.com/go-gost/relay"
|
||||
ctxvalue "github.com/go-gost/x/ctx"
|
||||
limiter_util "github.com/go-gost/x/internal/util/limiter"
|
||||
stats_util "github.com/go-gost/x/internal/util/stats"
|
||||
tls_util "github.com/go-gost/x/internal/util/tls"
|
||||
rate_limiter "github.com/go-gost/x/limiter/rate"
|
||||
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
|
||||
xstats "github.com/go-gost/x/observer/stats"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
@@ -73,10 +73,10 @@ func (h *relayHandler) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+28
-19
@@ -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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -20,11 +20,11 @@ import (
|
||||
"github.com/go-gost/gosocks4"
|
||||
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/sniffing"
|
||||
stats_util "github.com/go-gost/x/internal/util/stats"
|
||||
tls_util "github.com/go-gost/x/internal/util/tls"
|
||||
rate_limiter "github.com/go-gost/x/limiter/rate"
|
||||
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
|
||||
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
xstats "github.com/go-gost/x/observer/stats"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
@@ -77,10 +77,10 @@ func (h *socks4Handler) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ import (
|
||||
"github.com/go-gost/core/recorder"
|
||||
"github.com/go-gost/gosocks5"
|
||||
ctxvalue "github.com/go-gost/x/ctx"
|
||||
limiter_util "github.com/go-gost/x/internal/util/limiter"
|
||||
"github.com/go-gost/x/internal/util/socks"
|
||||
stats_util "github.com/go-gost/x/internal/util/stats"
|
||||
tls_util "github.com/go-gost/x/internal/util/tls"
|
||||
rate_limiter "github.com/go-gost/x/limiter/rate"
|
||||
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
|
||||
xstats "github.com/go-gost/x/observer/stats"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
@@ -78,10 +78,10 @@ func (h *socks5Handler) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ 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"
|
||||
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"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
"github.com/go-gost/x/registry"
|
||||
xservice "github.com/go-gost/x/service"
|
||||
@@ -123,10 +123,10 @@ func (h *tunnelHandler) 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),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user