Skip to content

Commit 2c27120

Browse files
committed
agent: refactor cleanup if no pending subs
1 parent 7a3713f commit 2c27120

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Simplex/Messaging/Client/Agent.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import Crypto.Random (ChaChaDRG)
4747
import Data.ByteString.Char8 (ByteString)
4848
import qualified Data.ByteString.Char8 as B
4949
import Data.Constraint (Dict (..))
50+
import Data.Functor (($>))
5051
import Data.List.NonEmpty (NonEmpty)
5152
import qualified Data.List.NonEmpty as L
5253
import Data.Map.Strict (Map)
@@ -328,11 +329,12 @@ reconnectClient ca@SMPClientAgent {active, agentCfg, smpSubWorkers, workerSeq} s
328329
atomically $ putTMVar (sessionVar v) a
329330
runSubWorker v =
330331
withRetryInterval (reconnectInterval agentCfg) $ \_ loop -> do
331-
subs <- atomically $ do
332-
s <- getPending TM.lookup readTVar
333-
when (noPending s) $ cleanup v
334-
pure s
335-
unless (noPending subs) $ whenM (readTVarIO active) $ do
332+
subs_ <- atomically $ do
333+
s <- getPending TM.lookup readTVar
334+
if noPending s
335+
then cleanup v $> Nothing
336+
else pure $ Just s
337+
forM_ subs_ $ \subs -> whenM (readTVarIO active) $ do
336338
void $ netTimeoutInt tcpConnectTimeout NRMBackground `timeout` runExceptT (reconnectSMPClient ca srv subs)
337339
loop
338340
ProtocolClientConfig {networkConfig = NetworkConfig {tcpConnectTimeout}} = smpCfg agentCfg

0 commit comments

Comments
 (0)