add stop pxy.StopCheck

This commit is contained in:
baixl 2023-07-05 17:10:38 +08:00
parent 4f12a4f308
commit 95434a3ebd
3 changed files with 51 additions and 29 deletions

View File

@ -68,6 +68,7 @@ func main() {
// enable checkers // enable checkers
pxy.Check() pxy.Check()
defer pxy.StopCheck()
// run proxy servers // run proxy servers
for _, listen := range config.Listens { for _, listen := range config.Listens {

View File

@ -33,6 +33,7 @@ type FwdrGroup struct {
index uint32 index uint32
priority uint32 priority uint32
next func(addr string) *Forwarder next func(addr string) *Forwarder
stopChan chan bool
} }
// NewFwdrGroup returns a new forward group. // NewFwdrGroup returns a new forward group.
@ -235,6 +236,11 @@ func (p *FwdrGroup) check(fwdr *Forwarder, checker Checker) {
intval := time.Duration(p.config.CheckInterval) * time.Second intval := time.Duration(p.config.CheckInterval) * time.Second
for { for {
select {
case <-p.stopChan:
log.F("[check] %s: stop checking", p.name)
return
default:
time.Sleep(intval * time.Duration(wait)) time.Sleep(intval * time.Duration(wait))
// check all forwarders at least one time // check all forwarders at least one time
@ -272,6 +278,7 @@ func (p *FwdrGroup) check(fwdr *Forwarder, checker Checker) {
fwdr.Enable() fwdr.Enable()
} }
} }
}
func (p *FwdrGroup) setLatency(fwdr *Forwarder, elapsed time.Duration) { func (p *FwdrGroup) setLatency(fwdr *Forwarder, elapsed time.Duration) {
newLatency := int64(elapsed) newLatency := int64(elapsed)
@ -316,3 +323,8 @@ func (p *FwdrGroup) scheduleDH(dstAddr string) *Forwarder {
fnv1a.Write([]byte(dstAddr)) fnv1a.Write([]byte(dstAddr))
return p.avail[fnv1a.Sum32()%uint32(len(p.avail))] return p.avail[fnv1a.Sum32()%uint32(len(p.avail))]
} }
// Stop Check
func (p *FwdrGroup) StopCheck() {
p.stopChan <- true
}

View File

@ -154,3 +154,12 @@ func (p *Proxy) Check() {
fwdrGroup.Check() fwdrGroup.Check()
} }
} }
// Stop All checkers.
func (p *Proxy) StopCheck() {
p.main.StopCheck()
for _, fwdrGroup := range p.all {
fwdrGroup.StopCheck()
}
}