chore: refine reloading code

This commit is contained in:
mzz2017 2023-03-30 23:28:45 +08:00
parent 745a47e952
commit f2dc750dbb
2 changed files with 12 additions and 16 deletions

View File

@ -193,7 +193,7 @@ loop:
}).Fatalln("[Reload] Failed to roll back configuration")
}
newConf = conf
log.Warnln("[Reload] Last reload failed; rolled back configuration")
log.Errorln("[Reload] Last reload failed; rolled back configuration")
} else {
log.Warnln("[Reload] Stopped old control plane")
}

View File

@ -56,7 +56,8 @@ type ControlPlane struct {
routingMatcher *RoutingMatcher
closed chan struct{}
ctx context.Context
cancel context.CancelFunc
ready chan struct{}
muRealDomainSet sync.Mutex
@ -322,6 +323,7 @@ func NewControlPlane(
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
plane := &ControlPlane{
log: log,
core: core,
@ -332,14 +334,15 @@ func NewControlPlane(
onceNetworkReady: sync.Once{},
dialMode: dialMode,
routingMatcher: routingMatcher,
closed: make(chan struct{}),
ctx: ctx,
cancel: cancel,
ready: make(chan struct{}),
muRealDomainSet: sync.Mutex{},
realDomainSet: bloom.NewWithEstimates(2048, 0.001),
}
defer func() {
if err != nil {
close(plane.closed)
cancel()
}
}()
@ -420,7 +423,7 @@ func (c *ControlPlane) CloneDnsCache() map[string]*DnsCache {
func (c *ControlPlane) dnsUpstreamReadyCallback(dnsUpstream *dns.Upstream) (err error) {
// Waiting for ready.
select {
case <-c.closed:
case <-c.ctx.Done():
return nil
case <-c.ready:
}
@ -605,18 +608,12 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
return err
}
ctx, cancel := context.WithCancel(context.Background())
c.deferFuncs = append(c.deferFuncs, func() error {
cancel()
return nil
})
sentReady = true
readyChan <- true
go func() {
defer cancel()
for {
select {
case <-ctx.Done():
case <-c.ctx.Done():
return
default:
}
@ -635,10 +632,9 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}
}()
go func() {
defer cancel()
for {
select {
case <-ctx.Done():
case <-c.ctx.Done():
return
default:
}
@ -684,7 +680,7 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}(newBuf, src)
}
}()
<-ctx.Done()
<-c.ctx.Done()
return nil
}
@ -834,6 +830,6 @@ func (c *ControlPlane) Close() (err error) {
}
}
}
close(c.closed)
c.cancel()
return c.core.Close()
}