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

Commit 37786ab

Browse files
committed
[CDEC-356] Flatten sendLoop of ntp client
1 parent 08ffc98 commit 37786ab

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

networking/src/Ntp/Client.hs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ 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-
wait, withAsync)
24+
import Control.Concurrent.Async (async, concurrently_, race)
2625
import Control.Concurrent.STM (TVar, check, modifyTVar', retry)
2726
import Control.Exception (Exception, IOException, catch, handle)
2827
import Control.Monad (forever)
@@ -148,11 +147,6 @@ updateStatus cli = updateStatus' cli fn
148147
, (Wlog.Info, sformat ("Evaluated clock offset "%shown%"mcs") offset)
149148
)
150149

151-
-- |
152-
-- Internal commands raised by the send loop.
153-
data NtpClientCmd = SendRequest
154-
deriving Eq
155-
156150
-- |
157151
-- Every `ntpPollDelay` we send a request to the list of `ntpServers`. Before
158152
-- sending a request, we put `NtpSyncPending` to `ncState`. After sending
@@ -162,32 +156,20 @@ data NtpClientCmd = SendRequest
162156
-- drift.
163157
sendLoop :: NtpClient -> [Addresses] -> IO ()
164158
sendLoop cli addrs = do
165-
166-
167159
let respTimeout = ntpResponseTimeout (ncSettings cli)
168160
let poll = ntpPollDelay (ncSettings cli)
169161

170-
() <- withAsync
171-
(do
172-
-- wait for responses and update status
173-
_ <- timeout respTimeout waitForResponses
174-
updateStatus cli
175-
-- after @'updateStatus'@ @'ntpStatus'@ is guaranteed to be
176-
-- different from @'NtpSyncPending'@, now we can wait until it was
177-
-- changed back to @'NtpSyncPending'@ to force a request.
178-
waitForRequest
179-
)
180-
(\a -> do
181-
-- send packets and wait until end of poll delay
182-
sock <- atomically $ readTVar $ ncSockets cli
183-
pack <- mkNtpPacket
184-
sendPacket sock pack addrs
185-
186-
cmd <- timeout poll (wait a)
187-
case cmd of
188-
Nothing -> cancel a
189-
Just _ -> return ()
190-
)
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
191173

192174
-- reset state & status before next loop
193175
atomically $ writeTVar (ncState cli) []
@@ -209,7 +191,7 @@ sendLoop cli addrs = do
209191
atomically $ do
210192
status <- readTVar $ ncStatus cli
211193
check (status == NtpSyncPending)
212-
return SendRequest
194+
return ()
213195

214196

215197
-- |

0 commit comments

Comments
 (0)