Browse Source

BrokerClient: handle timeout in getNotifications

master
Denis Tereshkin 11 months ago
parent
commit
d4e3891145
  1. 34
      src/ATrade/Broker/Client.hs

34
src/ATrade/Broker/Client.hs

@ -278,18 +278,22 @@ bcGetNotifications clientIdentity idCounter notifSqnumRef cmdVar lastKnownNotifi @@ -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"

Loading…
Cancel
Save