Files
sing-box/route/process_cache.go
世界 b8e5a71450 Add process information cache to avoid duplicate lookups
PreMatch and full match phases each created a fresh InboundContext,
causing process search (expensive OS syscalls) to run twice per
connection. Use a freelru ShardedLRU cache with 200ms TTL to serve
the second lookup from cache.
2026-03-23 14:26:45 +08:00

35 lines
865 B
Go

package route
import (
"context"
"net/netip"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/process"
)
type processCacheKey struct {
Network string
Source netip.AddrPort
Destination netip.AddrPort
}
type processCacheEntry struct {
result *adapter.ConnectionOwner
err error
}
func (r *Router) findProcessInfoCached(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
key := processCacheKey{
Network: network,
Source: source,
Destination: destination,
}
if entry, ok := r.processCache.Get(key); ok {
return entry.result, entry.err
}
result, err := process.FindProcessInfo(r.processSearcher, ctx, network, source, destination)
r.processCache.Add(key, processCacheEntry{result: result, err: err})
return result, err
}