mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-04-13 20:28:32 +10:00
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.
This commit is contained in:
34
route/process_cache.go
Normal file
34
route/process_cache.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user