Skip to content

Commit 26365fb

Browse files
committed
Update pause usage
1 parent 159e489 commit 26365fb

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

common/ntp/service.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/sagernet/sing/common/logger"
1111
M "github.com/sagernet/sing/common/metadata"
1212
N "github.com/sagernet/sing/common/network"
13+
"github.com/sagernet/sing/common/x/list"
1314
"github.com/sagernet/sing/service"
1415
"github.com/sagernet/sing/service/pause"
1516
)
@@ -40,9 +41,11 @@ type Service struct {
4041
server M.Socksaddr
4142
writeToSystem bool
4243
ticker *time.Ticker
44+
interval time.Duration
4345
timeout time.Duration
4446
clockOffset time.Duration
4547
pause pause.Manager
48+
pauseCallback *list.Element[pause.Callback]
4649
}
4750

4851
func NewService(options Options) *Service {
@@ -82,7 +85,7 @@ func NewService(options Options) *Service {
8285
logger: options.Logger,
8386
writeToSystem: options.WriteToSystem,
8487
server: destination,
85-
ticker: time.NewTicker(interval),
88+
interval: interval,
8689
timeout: options.Timeout,
8790
pause: service.FromContext[pause.Manager](ctx),
8891
}
@@ -95,7 +98,11 @@ func (s *Service) Start() error {
9598
} else {
9699
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
97100
}
101+
s.ticker = time.NewTicker(s.interval)
98102
go s.loopUpdate()
103+
if s.pause != nil {
104+
s.pauseCallback = pause.RegisterTicker(s.pause, s.ticker, s.interval, s.updateOnce)
105+
}
99106
return nil
100107
}
101108

@@ -118,20 +125,16 @@ func (s *Service) loopUpdate() {
118125
return
119126
case <-s.ticker.C:
120127
}
121-
if s.pause != nil {
122-
s.pause.WaitActive()
123-
select {
124-
case <-s.ctx.Done():
125-
return
126-
default:
127-
}
128-
}
129-
err := s.update()
130-
if err == nil {
131-
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
132-
} else {
133-
s.logger.Error("update time: ", err)
134-
}
128+
s.updateOnce()
129+
}
130+
}
131+
132+
func (s *Service) updateOnce() {
133+
err := s.update()
134+
if err == nil {
135+
s.logger.Info("updated time: ", s.TimeFunc()().Local().Format(TimeLayout))
136+
} else {
137+
s.logger.Error("update time: ", err)
135138
}
136139
}
137140

service/pause/timer.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pause
2+
3+
import (
4+
"time"
5+
6+
"github.com/sagernet/sing/common/x/list"
7+
)
8+
9+
func RegisterTicker(manager Manager, ticker *time.Ticker, duration time.Duration, resume func()) *list.Element[Callback] {
10+
if manager.IsPaused() {
11+
ticker.Stop()
12+
}
13+
return manager.RegisterCallback(func(event int) {
14+
switch event {
15+
case EventDevicePaused, EventNetworkPause:
16+
ticker.Stop()
17+
case EventDeviceWake, EventNetworkWake:
18+
if resume != nil {
19+
resume()
20+
}
21+
ticker.Reset(duration)
22+
}
23+
})
24+
}

0 commit comments

Comments
 (0)