From b8155e618216806163abf7c0f50862f49bccbba3 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 8 May 2024 22:38:28 +0700 Subject: [PATCH] cmd/cli: set DNS last when running ctrld service On low resources Windows Server VM, profiling shows the bottle neck when interacting with Windows DNS server to add/remove forwarders using by calling external powershell commands. This happens because ctrld try setting DNS before it runs. However, it would be better if ctrld only sets DNS after all its listeners ready. So it won't block ctrld from receiving requests. With this change, self-check process on dual Core Windows server VM now runs constantly fast, ~2-4 seconds when running multiple times in a row. --- cmd/cli/prog.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/cli/prog.go b/cmd/cli/prog.go index 64728a0..2e74a98 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -194,9 +194,6 @@ func (p *prog) runWait() { } func (p *prog) preRun() { - if !service.Interactive() { - p.setDNS() - } if runtime.GOOS == "darwin" { p.onStopped = append(p.onStopped, func() { if !service.Interactive() { @@ -206,6 +203,12 @@ func (p *prog) preRun() { } } +func (p *prog) postRun() { + if !service.Interactive() { + p.setDNS() + } +} + func (p *prog) setupUpstream(cfg *ctrld.Config) { localUpstreams := make([]string, 0, len(cfg.Upstream)) ptrNameservers := make([]string, 0, len(cfg.Upstream)) @@ -388,6 +391,7 @@ func (p *prog) run(reload bool, reloadCh chan struct{}) { if p.logConn != nil { _ = p.logConn.Close() } + p.postRun() } wg.Wait() }