|
|
@@ -4,7 +4,7 @@ import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
- sync "sync"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/dice"
|
|
|
@@ -21,9 +21,10 @@ type HealthPingSettings struct {
|
|
|
|
|
|
// HealthPing is the health checker for balancers
|
|
|
type HealthPing struct {
|
|
|
- ctx context.Context
|
|
|
- access sync.Mutex
|
|
|
- ticker *time.Ticker
|
|
|
+ ctx context.Context
|
|
|
+ access sync.Mutex
|
|
|
+ ticker *time.Ticker
|
|
|
+ tickerClose chan struct{}
|
|
|
|
|
|
Settings *HealthPingSettings
|
|
|
Results map[string]*HealthPingRTTS
|
|
|
@@ -72,7 +73,9 @@ func (h *HealthPing) StartScheduler(selector func() ([]string, error)) {
|
|
|
}
|
|
|
interval := h.Settings.Interval * time.Duration(h.Settings.SamplingCount)
|
|
|
ticker := time.NewTicker(interval)
|
|
|
+ tickerClose := make(chan struct{})
|
|
|
h.ticker = ticker
|
|
|
+ h.tickerClose = tickerClose
|
|
|
go func() {
|
|
|
for {
|
|
|
go func() {
|
|
|
@@ -84,9 +87,11 @@ func (h *HealthPing) StartScheduler(selector func() ([]string, error)) {
|
|
|
h.doCheck(tags, interval, h.Settings.SamplingCount)
|
|
|
h.Cleanup(tags)
|
|
|
}()
|
|
|
- _, ok := <-ticker.C
|
|
|
- if !ok {
|
|
|
- break
|
|
|
+ select {
|
|
|
+ case <-ticker.C:
|
|
|
+ continue
|
|
|
+ case <-tickerClose:
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
}()
|
|
|
@@ -94,8 +99,13 @@ func (h *HealthPing) StartScheduler(selector func() ([]string, error)) {
|
|
|
|
|
|
// StopScheduler implements the HealthChecker
|
|
|
func (h *HealthPing) StopScheduler() {
|
|
|
+ if h.ticker == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
h.ticker.Stop()
|
|
|
h.ticker = nil
|
|
|
+ close(h.tickerClose)
|
|
|
+ h.tickerClose = nil
|
|
|
}
|
|
|
|
|
|
// Check implements the HealthChecker
|