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") }).Fatalln("[Reload] Failed to roll back configuration")
} }
newConf = conf newConf = conf
log.Warnln("[Reload] Last reload failed; rolled back configuration") log.Errorln("[Reload] Last reload failed; rolled back configuration")
} else { } else {
log.Warnln("[Reload] Stopped old control plane") log.Warnln("[Reload] Stopped old control plane")
} }

View File

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