mirror of
https://github.com/SagerNet/sing-box.git
synced 2026-04-12 01:57:18 +10:00
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.
35 lines
865 B
Go
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
|
|
}
|