@@ -21,9 +21,8 @@ module Ntp.Client
21
21
import Universum hiding (Last , catch )
22
22
23
23
import Control.Concurrent (threadDelay )
24
- import Control.Concurrent.Async (async , cancel , concurrently_ , race ,
25
- withAsync )
26
- import Control.Concurrent.STM (TVar , modifyTVar' , retry )
24
+ import Control.Concurrent.Async (async , concurrently_ , race )
25
+ import Control.Concurrent.STM (TVar , check , modifyTVar' , retry )
27
26
import Control.Exception (Exception , IOException , catch , handle )
28
27
import Control.Monad (forever )
29
28
import Data.Aeson (FromJSON (.. ), ToJSON (.. ), genericParseJSON ,
@@ -157,30 +156,25 @@ updateStatus cli = updateStatus' cli fn
157
156
-- drift.
158
157
sendLoop :: NtpClient -> [Addresses ] -> IO ()
159
158
sendLoop cli addrs = do
160
-
161
-
162
159
let respTimeout = ntpResponseTimeout (ncSettings cli)
163
160
let poll = ntpPollDelay (ncSettings cli)
164
161
165
- () <- withAsync
166
- (do
167
- -- wait for responses and update status
168
- _ <- timeout respTimeout waitForResponses
169
- updateStatus cli
170
- )
171
- (\ a -> do
172
- -- send packets and wait until end of poll delay
173
- sock <- atomically $ readTVar $ ncSockets cli
174
- pack <- mkNtpPacket
175
- sendPacket sock pack addrs
176
-
177
- threadDelay $ fromIntegral poll
178
- cancel a
179
- )
162
+ -- send packets and wait until end of poll delay
163
+ sock <- atomically $ readTVar $ ncSockets cli
164
+ pack <- mkNtpPacket
165
+ sendPacket sock pack addrs
166
+
167
+ _ <- timeout respTimeout waitForResponses
168
+ updateStatus cli
169
+ -- after @'updateStatus'@ @'ntpStatus'@ is guaranteed to be
170
+ -- different from @'NtpSyncPending'@, now we can wait until it was
171
+ -- changed back to @'NtpSyncPending'@ to force a request.
172
+ _ <- timeout poll waitForRequest
180
173
181
174
-- reset state & status before next loop
182
175
atomically $ writeTVar (ncState cli) []
183
176
atomically $ writeTVar (ncStatus cli) NtpSyncPending
177
+
184
178
sendLoop cli addrs
185
179
186
180
where
@@ -192,6 +186,14 @@ sendLoop cli addrs = do
192
186
retry
193
187
logDebug " collected all responses"
194
188
189
+ -- Wait for a request to force an ntp check.
190
+ waitForRequest =
191
+ atomically $ do
192
+ status <- readTVar $ ncStatus cli
193
+ check (status == NtpSyncPending )
194
+ return ()
195
+
196
+
195
197
-- |
196
198
-- Start listening for responses on the socket @'ncSockets'@
197
199
startReceive :: NtpClient -> IO ()
0 commit comments