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,41 +236,47 @@ 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 {
time.Sleep(intval * time.Duration(wait)) select {
case <-p.stopChan:
log.F("[check] %s: stop checking", p.name)
return
default:
time.Sleep(intval * time.Duration(wait))
// check all forwarders at least one time // check all forwarders at least one time
if wait > 0 && (fwdr.Priority() < p.Priority()) { if wait > 0 && (fwdr.Priority() < p.Priority()) {
continue continue
}
if fwdr.Enabled() && p.config.CheckDisabledOnly {
continue
}
elapsed, err := checker.Check(fwdr)
if err != nil {
if errors.Is(err, proxy.ErrNotSupported) {
fwdr.SetMaxFailures(0)
log.F("[check] %s: %s(%d), %s, stop checking", p.name, fwdr.Addr(), fwdr.Priority(), err)
fwdr.Enable()
break
} }
wait++ if fwdr.Enabled() && p.config.CheckDisabledOnly {
if wait > 16 { continue
wait = 16
} }
log.F("[check] %s: %s(%d), FAILED. error: %s", p.name, fwdr.Addr(), fwdr.Priority(), err) elapsed, err := checker.Check(fwdr)
fwdr.Disable() if err != nil {
continue if errors.Is(err, proxy.ErrNotSupported) {
} fwdr.SetMaxFailures(0)
log.F("[check] %s: %s(%d), %s, stop checking", p.name, fwdr.Addr(), fwdr.Priority(), err)
fwdr.Enable()
break
}
wait = 1 wait++
p.setLatency(fwdr, elapsed) if wait > 16 {
log.F("[check] %s: %s(%d), SUCCESS. Elapsed: %dms, Latency: %dms.", wait = 16
p.name, fwdr.Addr(), fwdr.Priority(), elapsed.Milliseconds(), time.Duration(fwdr.Latency()).Milliseconds()) }
fwdr.Enable()
log.F("[check] %s: %s(%d), FAILED. error: %s", p.name, fwdr.Addr(), fwdr.Priority(), err)
fwdr.Disable()
continue
}
wait = 1
p.setLatency(fwdr, elapsed)
log.F("[check] %s: %s(%d), SUCCESS. Elapsed: %dms, Latency: %dms.",
p.name, fwdr.Addr(), fwdr.Priority(), elapsed.Milliseconds(), time.Duration(fwdr.Latency()).Milliseconds())
fwdr.Enable()
}
} }
} }
@ -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()
}
}