Limit concurrent auto-redirect flow judgements

This commit is contained in:
世界 2026-10-18 12:13:09 +08:00
parent 3454d6d241
commit ef0b632920
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -48,7 +48,7 @@ func NewAutoRedirectService(options []byte, handler AutoRedirectHandler) (AutoRe
autoRedirect, err := tun.NewAutoRedirect(tun.AutoRedirectOptions{
TunOptions: tunOptions,
Context: context.Background(),
Handler: &autoRedirectServiceHandler{handler: handler, logger: autoRedirectLogger},
Handler: &autoRedirectServiceHandler{handler: handler, logger: autoRedirectLogger, judgeSlots: make(chan struct{}, autoRedirectJudgeConcurrency)},
Logger: autoRedirectLogger,
NetworkMonitor: networkMonitor,
InterfaceFinder: control.NewDefaultInterfaceFinder(),
@ -96,13 +96,21 @@ func (s *autoRedirectServiceSession) UpdateRouteAddressSet() error {
return s.autoRedirect.UpdateRouteAddressSet()
}
// Each JudgeFlow call is a synchronous binder transaction into the app process. It occupies
// one thread of the app's binder pool (15 threads by default in libbinder), which also serves
// the system's calls into the app, until the app returns the verdict.
const autoRedirectJudgeConcurrency = 8
type autoRedirectServiceHandler struct {
handler AutoRedirectHandler
logger logger.Logger
handler AutoRedirectHandler
logger logger.Logger
judgeSlots chan struct{}
}
func (h *autoRedirectServiceHandler) JudgeFlow(network uint8, source netip.AddrPort, destination netip.AddrPort, firstPacket []byte) tun.FlowVerdict {
h.judgeSlots <- struct{}{}
action, err := h.handler.JudgeFlow(int32(network), source.Addr().String(), int32(source.Port()), destination.Addr().String(), int32(destination.Port()), firstPacket)
<-h.judgeSlots
if err != nil {
h.logger.Error("judge flow ", source, " -> ", destination, ": ", err)
return tun.FlowVerdict{Action: tun.ActionAccept}