From d4e3891145e5aa421bdc87ed4bf09574eb21a321 Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Tue, 21 Jan 2025 21:16:02 +0700 Subject: [PATCH] BrokerClient: handle timeout in getNotifications --- src/ATrade/Broker/Client.hs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/ATrade/Broker/Client.hs b/src/ATrade/Broker/Client.hs index 689629a..e868b38 100644 --- a/src/ATrade/Broker/Client.hs +++ b/src/ATrade/Broker/Client.hs @@ -278,18 +278,22 @@ bcGetNotifications clientIdentity idCounter notifSqnumRef cmdVar lastKnownNotifi sqnum <- nextId idCounter notifSqnum <- nextSqnum <$> readIORef notifSqnumRef now <- getCurrentTime - putMVar cmdVar (RequestNotifications (RequestId sqnum) clientIdentity notifSqnum, respVar, now) - resp <- takeMVar respVar - case resp of - (ResponseNotifications (RequestId requestId) ns) -> - if (requestId == sqnum) - then do - case lastMay ns of - Just n -> atomicWriteIORef notifSqnumRef (getNotificationSqnum n) - Nothing -> readIORef lastKnownNotification >>= atomicWriteIORef notifSqnumRef - return $ Right ns - else do - logWith logger Warning "Broker.Client" "GetNotifications: requestId mismatch" - return $ Left "requestId mismatch" - (ResponseError (RequestId requestId) msg) -> return $ Left msg - _ -> return $ Left "Unknown error" + result <- timeout 3000000 $ do + putMVar cmdVar (RequestNotifications (RequestId sqnum) clientIdentity notifSqnum, respVar, now) + resp <- takeMVar respVar + case resp of + (ResponseNotifications (RequestId requestId) ns) -> + if (requestId == sqnum) + then do + case lastMay ns of + Just n -> atomicWriteIORef notifSqnumRef (getNotificationSqnum n) + Nothing -> readIORef lastKnownNotification >>= atomicWriteIORef notifSqnumRef + return $ Right ns + else do + logWith logger Warning "Broker.Client" "GetNotifications: requestId mismatch" + return $ Left "requestId mismatch" + (ResponseError (RequestId requestId) msg) -> return $ Left msg + _ -> return $ Left "Unknown error" + case result of + Just r -> pure $ r + _ -> pure $ Left "Request timeout"