Pause recurring tasks when no network

This commit is contained in:
世界
2023-08-07 17:46:51 +08:00
parent 2d0aa96cbe
commit 5acc40d72f
11 changed files with 52 additions and 84 deletions

View File

@@ -8,7 +8,6 @@ import (
"time"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/sleep"
"github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
@@ -20,6 +19,7 @@ import (
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
"github.com/sagernet/sing/service/pause"
)
var (
@@ -153,7 +153,7 @@ type URLTestGroup struct {
tolerance uint16
history *urltest.HistoryStorage
checking atomic.Bool
sleepManager *sleep.Manager
pauseManager pause.Manager
access sync.Mutex
ticker *time.Ticker
@@ -184,7 +184,7 @@ func NewURLTestGroup(ctx context.Context, router adapter.Router, logger log.Logg
tolerance: tolerance,
history: history,
close: make(chan struct{}),
sleepManager: service.PtrFromContext[sleep.Manager](ctx),
pauseManager: pause.ManagerFromContext(ctx),
}
}
@@ -266,9 +266,7 @@ func (g *URLTestGroup) Fallback(used adapter.Outbound) []adapter.Outbound {
func (g *URLTestGroup) loopCheck() {
go g.CheckOutbounds(true)
for {
if g.sleepManager != nil {
<-g.sleepManager.Active()
}
g.pauseManager.WaitActive()
select {
case <-g.close:
return

View File

@@ -166,7 +166,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
if err != nil {
return nil, E.Cause(err, "create WireGuard device")
}
wgDevice := device.NewDevice(wireTunDevice, outbound.bind, &device.Logger{
wgDevice := device.NewDevice(ctx, wireTunDevice, outbound.bind, &device.Logger{
Verbosef: func(format string, args ...interface{}) {
logger.Debug(fmt.Sprintf(strings.ToLower(format), args...))
},