process_cache.go 865 B

12345678910111213141516171819202122232425262728293031323334
  1. package route
  2. import (
  3. "context"
  4. "net/netip"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/process"
  7. )
  8. type processCacheKey struct {
  9. Network string
  10. Source netip.AddrPort
  11. Destination netip.AddrPort
  12. }
  13. type processCacheEntry struct {
  14. result *adapter.ConnectionOwner
  15. err error
  16. }
  17. func (r *Router) findProcessInfoCached(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
  18. key := processCacheKey{
  19. Network: network,
  20. Source: source,
  21. Destination: destination,
  22. }
  23. if entry, ok := r.processCache.Get(key); ok {
  24. return entry.result, entry.err
  25. }
  26. result, err := process.FindProcessInfo(r.processSearcher, ctx, network, source, destination)
  27. r.processCache.Add(key, processCacheEntry{result: result, err: err})
  28. return result, err
  29. }