@@ -224,7 +224,6 @@ import Simplex.RemoteControl.Client
224224import Simplex.RemoteControl.Invitation
225225import Simplex.RemoteControl.Types
226226import System.Mem.Weak (deRefWeak )
227- import UnliftIO.Async (mapConcurrently )
228227import UnliftIO.Concurrent (forkFinally , forkIO , killThread , mkWeakThreadId , threadDelay )
229228import qualified UnliftIO.Exception as E
230229import UnliftIO.STM
@@ -440,7 +439,7 @@ subscribeConnections c = withAgentEnv c . subscribeConnections' c
440439{-# INLINE subscribeConnections #-}
441440
442441-- | Get messages for connections (GET commands)
443- getConnectionMessages :: AgentClient -> NonEmpty ConnMsgReq -> IO (NonEmpty (Maybe SMPMsgMeta ))
442+ getConnectionMessages :: AgentClient -> NonEmpty ConnMsgReq -> IO (NonEmpty (Either AgentErrorType ( Maybe SMPMsgMeta ) ))
444443getConnectionMessages c = withAgentEnv' c . getConnectionMessages' c
445444{-# INLINE getConnectionMessages #-}
446445
@@ -1277,26 +1276,26 @@ resubscribeConnections' c connIds = do
12771276 -- union is left-biased, so results returned by subscribeConnections' take precedence
12781277 (`M.union` r) <$> subscribeConnections' c connIds'
12791278
1280- getConnectionMessages' :: AgentClient -> NonEmpty ConnMsgReq -> AM' (NonEmpty (Maybe SMPMsgMeta ))
1281- getConnectionMessages' c =
1282- mapConcurrently $ \ cmr ->
1283- getConnectionMessage cmr `catchAgentError'` \ e -> do
1284- logError $ " Error loading message: " <> tshow e
1285- pure Nothing
1279+ -- requesting messages sequentially, to reduce memory usage
1280+ getConnectionMessages' :: AgentClient -> NonEmpty ConnMsgReq -> AM' (NonEmpty (Either AgentErrorType (Maybe SMPMsgMeta )))
1281+ getConnectionMessages' c = mapM $ tryAgentError' . getConnectionMessage
12861282 where
12871283 getConnectionMessage :: ConnMsgReq -> AM (Maybe SMPMsgMeta )
12881284 getConnectionMessage (ConnMsgReq connId dbQueueId msgTs_) = do
12891285 whenM (atomically $ hasActiveSubscription c connId) . throwE $ CMD PROHIBITED " getConnectionMessage: subscribed"
12901286 SomeConn _ conn <- withStore c (`getConn` connId)
1291- msg_ <- case conn of
1292- DuplexConnection _ (rq :| _) _ -> getQueueMessage c rq
1293- RcvConnection _ rq -> getQueueMessage c rq
1294- ContactConnection _ rq -> getQueueMessage c rq
1287+ rq <- case conn of
1288+ DuplexConnection _ (rq :| _) _ -> pure rq
1289+ RcvConnection _ rq -> pure rq
1290+ ContactConnection _ rq -> pure rq
12951291 SndConnection _ _ -> throwE $ CONN SIMPLEX
12961292 NewConnection _ -> throwE $ CMD PROHIBITED " getConnectionMessage: NewConnection"
1297- when (isNothing msg_) $
1293+ msg_ <- getQueueMessage c rq `catchAgentError` \ e -> atomically (releaseGetLock c rq) >> throwError e
1294+ when (isNothing msg_) $ do
1295+ atomically $ releaseGetLock c rq
12981296 forM_ msgTs_ $ \ msgTs -> withStore' c $ \ db -> setLastBrokerTs db connId (DBQueueId dbQueueId) msgTs
12991297 pure msg_
1298+ {-# INLINE getConnectionMessages' #-}
13001299
13011300getNotificationConns' :: AgentClient -> C. CbNonce -> ByteString -> AM (NonEmpty NotificationInfo )
13021301getNotificationConns' c nonce encNtfInfo =
@@ -1330,6 +1329,7 @@ getNotificationConns' c nonce encNtfInfo =
13301329 Just SMP. NMsgMeta {msgTs}
13311330 | maybe True (systemToUTCTime msgTs > ) lastBrokerTs_ -> Just ntfInfo
13321331 _ -> Nothing
1332+ {-# INLINE getNotificationConns' #-}
13331333
13341334-- | Send message to the connection (SEND command) in Reader monad
13351335sendMessage' :: AgentClient -> ConnId -> PQEncryption -> MsgFlags -> MsgBody -> AM (AgentMsgId , PQEncryption )
0 commit comments