@@ -1564,12 +1564,11 @@ subscribeAllConnections' :: AgentClient -> Bool -> Maybe UserId -> AM ()
15641564subscribeAllConnections' c onlyNeeded activeUserId_ = handleErr $ do
15651565 userSrvs <- withStore' c (`getSubscriptionServers` onlyNeeded)
15661566 unless (null userSrvs) $ do
1567- maxPending <- asks $ maxPendingSubscriptions . config
1568- currPending <- newTVarIO 0
1567+ batchSize <- asks $ subsBatchSize . config
15691568 let userSrvs' = case activeUserId_ of
15701569 Just activeUserId -> sortOn (\ (uId, _) -> if uId == activeUserId then 0 else 1 :: Int ) userSrvs
15711570 Nothing -> userSrvs
1572- rs <- lift $ mapConcurrently (subscribeUserServer maxPending currPending ) userSrvs'
1571+ rs <- lift $ mapConcurrently (subscribeUserServer batchSize ) userSrvs'
15731572 let (errs, oks) = partitionEithers rs
15741573 logInfo $ " subscribed " <> tshow (sum oks) <> " queues"
15751574 forM_ (L. nonEmpty errs) $ notifySub c . ERRS . L. map (" " ,)
@@ -1578,18 +1577,16 @@ subscribeAllConnections' c onlyNeeded activeUserId_ = handleErr $ do
15781577 resumeAllCommands c
15791578 where
15801579 handleErr = (`catchAllErrors` \ e -> notifySub' c " " (ERR e) >> throwE e)
1581- subscribeUserServer :: Int -> TVar Int -> (UserId , SMPServer ) -> AM' (Either AgentErrorType Int )
1582- subscribeUserServer maxPending currPending (userId, srv) = do
1583- atomically $ whenM ((maxPending <= ) <$> readTVar currPending) retry
1584- tryAllErrors' $ do
1585- qs <- withStore' c $ \ db -> do
1586- qs <- getUserServerRcvQueueSubs db userId srv onlyNeeded
1587- unless (null qs) $ atomically $ modifyTVar' currPending (+ length qs) -- update before leaving transaction
1588- pure qs
1589- let n = length qs
1590- unless (null qs) $ lift $ subscribe qs `E.finally` atomically (modifyTVar' currPending $ subtract n)
1591- pure n
1580+ subscribeUserServer :: Int -> (UserId , SMPServer ) -> AM' (Either AgentErrorType Int )
1581+ subscribeUserServer batchSize (userId, srv) = tryAllErrors' $ loop 0 Nothing
15921582 where
1583+ loop ! n cursor_ = do
1584+ qs <- withStore' c $ \ db -> getUserServerRcvQueueSubs db userId srv onlyNeeded batchSize cursor_
1585+ if null qs then pure n else do
1586+ lift $ subscribe qs
1587+ let n' = n + length qs
1588+ lastRcvId = Just $ queueId $ last qs
1589+ if length qs < batchSize then pure n' else loop n' lastRcvId
15931590 subscribe qs = do
15941591 rs <- subscribeUserServerQueues c userId srv qs
15951592 -- TODO [certs rcv] storeClientServiceAssocs store associations of queues with client service ID
0 commit comments