Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 3a6e31a

Browse files
authored
Merge pull request #3323 from input-output-hk/coot/cdec-356
[CDEC-356] Forcable ntp check
2 parents 17a809e + 2a4266d commit 3a6e31a

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

networking/src/Ntp/Client.hs

+22-20
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ module Ntp.Client
2121
import Universum hiding (Last, catch)
2222

2323
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)
2726
import Control.Exception (Exception, IOException, catch, handle)
2827
import Control.Monad (forever)
2928
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON,
@@ -157,30 +156,25 @@ updateStatus cli = updateStatus' cli fn
157156
-- drift.
158157
sendLoop :: NtpClient -> [Addresses] -> IO ()
159158
sendLoop cli addrs = do
160-
161-
162159
let respTimeout = ntpResponseTimeout (ncSettings cli)
163160
let poll = ntpPollDelay (ncSettings cli)
164161

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
180173

181174
-- reset state & status before next loop
182175
atomically $ writeTVar (ncState cli) []
183176
atomically $ writeTVar (ncStatus cli) NtpSyncPending
177+
184178
sendLoop cli addrs
185179

186180
where
@@ -192,6 +186,14 @@ sendLoop cli addrs = do
192186
retry
193187
logDebug "collected all responses"
194188

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+
195197
-- |
196198
-- Start listening for responses on the socket @'ncSockets'@
197199
startReceive :: NtpClient -> IO ()

0 commit comments

Comments
 (0)