@@ -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
4851func 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
0 commit comments