@@ -10,6 +10,7 @@ import (
10
10
"github.com/sagernet/sing/common/logger"
11
11
M "github.com/sagernet/sing/common/metadata"
12
12
N "github.com/sagernet/sing/common/network"
13
+ "github.com/sagernet/sing/common/x/list"
13
14
"github.com/sagernet/sing/service"
14
15
"github.com/sagernet/sing/service/pause"
15
16
)
@@ -40,9 +41,11 @@ type Service struct {
40
41
server M.Socksaddr
41
42
writeToSystem bool
42
43
ticker * time.Ticker
44
+ interval time.Duration
43
45
timeout time.Duration
44
46
clockOffset time.Duration
45
47
pause pause.Manager
48
+ pauseCallback * list.Element [pause.Callback ]
46
49
}
47
50
48
51
func NewService (options Options ) * Service {
@@ -82,7 +85,7 @@ func NewService(options Options) *Service {
82
85
logger : options .Logger ,
83
86
writeToSystem : options .WriteToSystem ,
84
87
server : destination ,
85
- ticker : time . NewTicker ( interval ) ,
88
+ interval : interval ,
86
89
timeout : options .Timeout ,
87
90
pause : service.FromContext [pause.Manager ](ctx ),
88
91
}
@@ -95,7 +98,11 @@ func (s *Service) Start() error {
95
98
} else {
96
99
s .logger .Info ("updated time: " , s .TimeFunc ()().Local ().Format (TimeLayout ))
97
100
}
101
+ s .ticker = time .NewTicker (s .interval )
98
102
go s .loopUpdate ()
103
+ if s .pause != nil {
104
+ s .pauseCallback = pause .RegisterTicker (s .pause , s .ticker , s .interval , s .updateOnce )
105
+ }
99
106
return nil
100
107
}
101
108
@@ -118,20 +125,16 @@ func (s *Service) loopUpdate() {
118
125
return
119
126
case <- s .ticker .C :
120
127
}
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 )
135
138
}
136
139
}
137
140
0 commit comments