Skip to content

Commit 9c07ddf

Browse files
authored
agent: allow to use existing connId for getConnShortLinkAsync (#1752)
1 parent 50b71d3 commit 9c07ddf

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/Simplex/Messaging/Agent.hs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,9 @@ setConnShortLinkAsync :: AgentClient -> ACorrId -> ConnId -> UserConnLinkData 'C
354354
setConnShortLinkAsync c = withAgentEnv c .:: setConnShortLinkAsync' c
355355
{-# INLINE setConnShortLinkAsync #-}
356356

357-
-- | Get and verify data from short link (LGET/LKEY command) asynchronously, synchronous response is new connection id
358-
getConnShortLinkAsync :: AgentClient -> UserId -> ACorrId -> ConnShortLink 'CMContact -> AE ConnId
359-
getConnShortLinkAsync c = withAgentEnv c .:. getConnShortLinkAsync' c
357+
-- | Get and verify data from short link (LGET/LKEY command) asynchronously, synchronous response is new/passed connection id
358+
getConnShortLinkAsync :: AgentClient -> UserId -> ACorrId -> Maybe ConnId -> ConnShortLink 'CMContact -> AE ConnId
359+
getConnShortLinkAsync c = withAgentEnv c .:: getConnShortLinkAsync' c
360360
{-# INLINE getConnShortLinkAsync #-}
361361

362362
-- | Join SMP agent connection (JOIN command) asynchronously, synchronous response is new connection id.
@@ -1001,14 +1001,22 @@ setConnShortLinkAsync' c corrId connId userLinkData clientData =
10011001
_ -> throwE $ CMD PROHIBITED "setConnShortLinkAsync: invalid connection or mode"
10021002
enqueueCommand c corrId connId (Just srv) $ AClientCommand $ LSET userLinkData clientData
10031003

1004-
getConnShortLinkAsync' :: AgentClient -> UserId -> ACorrId -> ConnShortLink 'CMContact -> AM ConnId
1005-
getConnShortLinkAsync' c userId corrId shortLink@(CSLContact _ _ srv _) = do
1006-
g <- asks random
1007-
connId <- withStore c $ \db -> do
1008-
-- server is created so the command is processed in server queue,
1009-
-- not blocking other "no server" commands
1010-
void $ createServer db srv
1011-
prepareNewConn db g
1004+
getConnShortLinkAsync' :: AgentClient -> UserId -> ACorrId -> Maybe ConnId -> ConnShortLink 'CMContact -> AM ConnId
1005+
getConnShortLinkAsync' c userId corrId connId_ shortLink@(CSLContact _ _ srv _) = do
1006+
connId <- case connId_ of
1007+
Just existingConnId -> do
1008+
-- connId and srv can be unrelated: connId is used as "mailbox" for LDATA delivery,
1009+
-- while srv is the short link's server for the LGET request.
1010+
-- E.g., owner's relay connection (connId, on server A) fetches relay's group link data (srv = server B).
1011+
-- This works because enqueueCommand stores (connId, srv) independently in the commands table,
1012+
-- the network request targets srv, and event delivery uses connId via corrId correlation.
1013+
withStore' c $ \db -> void $ createServer db srv
1014+
pure existingConnId
1015+
Nothing -> do
1016+
g <- asks random
1017+
withStore c $ \db -> do
1018+
void $ createServer db srv
1019+
prepareNewConn db g
10121020
enqueueCommand c corrId connId (Just srv) $ AClientCommand $ LGET shortLink
10131021
pure connId
10141022
where

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,7 @@ testGetConnShortLinkAsync ps = withAgentClients2 $ \alice bob ->
27362736
newLinkData = UserContactLinkData userCtData
27372737
(_, (CCLink qInfo (Just shortLink), _)) <- A.createConnection alice NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing IKPQOn SMSubscribe
27382738
-- get link data async - creates new connection for bob
2739-
newId <- getConnShortLinkAsync bob 1 "1" shortLink
2739+
newId <- getConnShortLinkAsync bob 1 "1" Nothing shortLink
27402740
("1", newId', LDATA FixedLinkData {linkConnReq = qInfo'} (ContactLinkData _ userCtData')) <- get bob
27412741
liftIO $ newId' `shouldBe` newId
27422742
liftIO $ qInfo' `shouldBe` qInfo

0 commit comments

Comments
 (0)