diff --git a/dns/dial.go b/dns/dial.go deleted file mode 100644 index ce2b7b8580..0000000000 --- a/dns/dial.go +++ /dev/null @@ -1,15 +0,0 @@ -package dns - -import ( - "context" - "net" - - "github.com/metacubex/mihomo/component/inner_dialer" - C "github.com/metacubex/mihomo/constant" -) - -var remoteDialer = inner_dialer.NewDialer(C.DNS) - -func remoteDial(network, addr string) (net.Conn, error) { - return remoteDialer.DialContext(context.Background(), network, addr) -} diff --git a/hub/executor/concurrent_load_limit.go b/hub/executor/concurrent_load_limit.go new file mode 100644 index 0000000000..45f5ce9f27 --- /dev/null +++ b/hub/executor/concurrent_load_limit.go @@ -0,0 +1,5 @@ +//go:build !386 && !amd64 && !arm64 && !arm64be && !mipsle && !mips + +package executor + +const concurrentCount = 5 diff --git a/hub/executor/concurrent_load_single.go b/hub/executor/concurrent_load_single.go new file mode 100644 index 0000000000..603d38f160 --- /dev/null +++ b/hub/executor/concurrent_load_single.go @@ -0,0 +1,5 @@ +//go:build mips || mipsle + +package executor + +const concurrentCount = 1 diff --git a/hub/executor/concurrent_load_unlimit.go b/hub/executor/concurrent_load_unlimit.go new file mode 100644 index 0000000000..fded7e5649 --- /dev/null +++ b/hub/executor/concurrent_load_unlimit.go @@ -0,0 +1,7 @@ +//go:build 386 || amd64 || arm64 || arm64be + +package executor + +import "math" + +const concurrentCount = math.MaxInt diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 1ed342aab7..68426959cd 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -4,6 +4,7 @@ import ( "fmt" "net/netip" "os" + "runtime" "strconv" "sync" @@ -93,8 +94,11 @@ func ApplyConfig(cfg *config.Config, force bool) { updateListeners(cfg.Listeners) updateDNS(cfg.DNS, cfg.RuleProviders) updateTun(cfg.General) // tun should not care "force" - loadProvider(cfg.RuleProviders, cfg.Providers) + loadProvider(cfg.Providers) + loadProvider(cfg.RuleProviders) updateTunnels(cfg.Tunnels) + + runtime.GC() } func GetGeneral() *config.General { @@ -142,8 +146,8 @@ func GetGeneral() *config.General { return general } -func loadProvider(ruleProviders map[string]providerTypes.RuleProvider, proxyProviders map[string]providerTypes.ProxyProvider) { - load := func(pv providerTypes.Provider) { +func loadProvider[P providerTypes.Provider](providers map[string]P) { + load := func(pv P) { if pv.VehicleType() == providerTypes.Compatible { log.Infoln("Start initial compatible provider %s", pv.Name()) } else { @@ -165,13 +169,20 @@ func loadProvider(ruleProviders map[string]providerTypes.RuleProvider, proxyProv } } - for _, proxyProvider := range proxyProviders { - load(proxyProvider) + // limit concurrent size + wg := sync.WaitGroup{} + ch := make(chan struct{}, concurrentCount) + for _, _provider := range providers { + _provider := _provider + wg.Add(1) + ch <- struct{}{} + go func() { + defer func() { <-ch; wg.Done() }() + load(_provider) + }() } - for _, ruleProvider := range ruleProviders { - load(ruleProvider) - } + wg.Wait() } func updateListeners(listeners map[string]C.InboundListener) { diff --git a/tunnel/dns_dialer.go b/tunnel/dns_dialer.go index 1839869b4a..52b0374d69 100644 --- a/tunnel/dns_dialer.go +++ b/tunnel/dns_dialer.go @@ -36,7 +36,7 @@ func (d *DNSDialer) DialContext(ctx context.Context, network, addr string) (net. var rule C.Rule metadata := &C.Metadata{ NetWork: C.TCP, - Type: C.INNER, + Type: C.DNS, } err := metadata.SetRemoteAddress(addr) // tcp can resolve host by remote if err != nil { @@ -134,7 +134,7 @@ func (d *DNSDialer) ListenPacket(ctx context.Context, network, addr string) (net opts := d.opts metadata := &C.Metadata{ NetWork: C.UDP, - Type: C.INNER, + Type: C.DNS, } err := metadata.SetRemoteAddress(addr) if err != nil {