mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-10-22 11:44:50 -04:00
Fix ticker usage
ticker.Close does not close ticker.C
This commit is contained in:
parent
02c8b04230
commit
ebee459f1f
@ -4,14 +4,11 @@ package command
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
|
|
||||||
grpc "google.golang.org/grpc"
|
|
||||||
|
|
||||||
core "github.com/v2fly/v2ray-core/v5"
|
core "github.com/v2fly/v2ray-core/v5"
|
||||||
"github.com/v2fly/v2ray-core/v5/app/log"
|
"github.com/v2fly/v2ray-core/v5/app/log"
|
||||||
"github.com/v2fly/v2ray-core/v5/common"
|
"github.com/v2fly/v2ray-core/v5/common"
|
||||||
cmlog "github.com/v2fly/v2ray-core/v5/common/log"
|
cmlog "github.com/v2fly/v2ray-core/v5/common/log"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoggerServer is the implemention of LoggerService
|
// LoggerServer is the implemention of LoggerService
|
||||||
@ -44,22 +41,19 @@ func (s *LoggerServer) FollowLog(_ *FollowLogRequest, stream LoggerService_Follo
|
|||||||
if !ok {
|
if !ok {
|
||||||
return newError("logger not support following")
|
return newError("logger not support following")
|
||||||
}
|
}
|
||||||
var err error
|
done := make(chan struct{})
|
||||||
f := func(msg cmlog.Message) {
|
f := func(msg cmlog.Message) {
|
||||||
err = stream.Send(&FollowLogResponse{
|
err := stream.Send(&FollowLogResponse{
|
||||||
Message: msg.String(),
|
Message: msg.String(),
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
close(done)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
follower.AddFollower(f)
|
follower.AddFollower(f)
|
||||||
defer follower.RemoveFollower(f)
|
defer follower.RemoveFollower(f)
|
||||||
ticker := time.NewTicker(time.Second)
|
<-done
|
||||||
for {
|
return nil
|
||||||
<-ticker.C
|
|
||||||
if err != nil {
|
|
||||||
ticker.Stop()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LoggerServer) mustEmbedUnimplementedLoggerServiceServer() {}
|
func (s *LoggerServer) mustEmbedUnimplementedLoggerServiceServer() {}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
sync "sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/v2fly/v2ray-core/v5/common/dice"
|
"github.com/v2fly/v2ray-core/v5/common/dice"
|
||||||
@ -21,9 +21,10 @@ type HealthPingSettings struct {
|
|||||||
|
|
||||||
// HealthPing is the health checker for balancers
|
// HealthPing is the health checker for balancers
|
||||||
type HealthPing struct {
|
type HealthPing struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
|
tickerClose chan struct{}
|
||||||
|
|
||||||
Settings *HealthPingSettings
|
Settings *HealthPingSettings
|
||||||
Results map[string]*HealthPingRTTS
|
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)
|
interval := h.Settings.Interval * time.Duration(h.Settings.SamplingCount)
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
|
tickerClose := make(chan struct{})
|
||||||
h.ticker = ticker
|
h.ticker = ticker
|
||||||
|
h.tickerClose = tickerClose
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
go func() {
|
go func() {
|
||||||
@ -84,9 +87,11 @@ func (h *HealthPing) StartScheduler(selector func() ([]string, error)) {
|
|||||||
h.doCheck(tags, interval, h.Settings.SamplingCount)
|
h.doCheck(tags, interval, h.Settings.SamplingCount)
|
||||||
h.Cleanup(tags)
|
h.Cleanup(tags)
|
||||||
}()
|
}()
|
||||||
_, ok := <-ticker.C
|
select {
|
||||||
if !ok {
|
case <-ticker.C:
|
||||||
break
|
continue
|
||||||
|
case <-tickerClose:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -94,8 +99,13 @@ func (h *HealthPing) StartScheduler(selector func() ([]string, error)) {
|
|||||||
|
|
||||||
// StopScheduler implements the HealthChecker
|
// StopScheduler implements the HealthChecker
|
||||||
func (h *HealthPing) StopScheduler() {
|
func (h *HealthPing) StopScheduler() {
|
||||||
|
if h.ticker == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
h.ticker.Stop()
|
h.ticker.Stop()
|
||||||
h.ticker = nil
|
h.ticker = nil
|
||||||
|
close(h.tickerClose)
|
||||||
|
h.tickerClose = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check implements the HealthChecker
|
// Check implements the HealthChecker
|
||||||
|
Loading…
Reference in New Issue
Block a user