Skip to content

Commit

Permalink
Connection table: change logic for whether a new connection should be…
Browse files Browse the repository at this point in the history
… used

When considering making a new outbound connection, the subscription
worker consults the connection table to see if there's an existing
connection to the remote ip:port combo. If there is, no new connection
will be established.

Change this test so that existing inbound connections are not
considered, i.e. we will now look to see if there's any existing
_outbound_ connection already, not any connection.

This should mean that we will try to connect outbound to a peer that is
already connected to us inbound (on the same remote ip:port combo).
  • Loading branch information
dcoutts committed Mar 21, 2023
1 parent 9e3ad42 commit 76a0fb8
Showing 1 changed file with 23 additions and 14 deletions.
Expand Up @@ -218,8 +218,8 @@ removeConnection
-> m ()
removeConnection tbl remoteAddr localAddr = atomically $ removeConnectionSTM tbl remoteAddr localAddr

-- | Try to see if it is possible to reference an existing connection rather
-- than creating a new one to the provied peer.
-- | Try to see if it is possible to reference an existing /outbound/
-- connection rather than creating a new one to the provied peer.
--
refConnectionSTM
:: ( MonadSTM m
Expand All @@ -233,19 +233,28 @@ refConnectionSTM ConnectionTable{ctTable} remoteAddr refVar = do
tbl <- readTVar ctTable
case M.lookup remoteAddr tbl of
Nothing -> return ConnectionTableCreate
Just cte ->
if S.member refVar $ cteRefs cte
then return ConnectionTableDuplicate
else do
-- TODO We look up remoteAddr twice, is it possible
-- to use M.alterF given that we need to be able to return
-- ConnectionTableCreate or ConnectionTableExist?
let refs' = S.insert refVar (cteRefs cte)
mapM_ addValencyCounter $ S.toList refs'
Just cte
-- If all the existing connections are inbound we still
-- want to create an outbound connection.
| all (==ConnectionInbound) (cteLocalAddresses cte) ->
return ConnectionTableCreate

writeTVar ctTable $ M.insert remoteAddr
(cte { cteRefs = refs'}) tbl
return ConnectionTableExist
-- Otherwise we know there's at least one outbound connection
-- but we distinguish the case of an exact duplicate.
| S.member refVar (cteRefs cte) ->
return ConnectionTableDuplicate

| otherwise -> do
-- TODO We look up remoteAddr twice, is it possible
-- to use M.alterF given that we need to be able to return
-- ConnectionTableCreate or ConnectionTableExist?
let refs' = S.insert refVar (cteRefs cte)
mapM_ addValencyCounter $ S.toList refs'

writeTVar ctTable $
M.insert remoteAddr (cte { cteRefs = refs'}) tbl

return ConnectionTableExist

refConnection
:: ( MonadSTM m
Expand Down

0 comments on commit 76a0fb8

Please sign in to comment.