Skip to content

Commit 3b0e049

Browse files
committed
connection-manager: ConnStateId
Also renamed `FreshConnIdSupply` to `ConnStateIdSupply`.
1 parent 63a890d commit 3b0e049

File tree

2 files changed

+44
-30
lines changed
  • ouroboros-network-framework/src/Ouroboros/Network/ConnectionManager

2 files changed

+44
-30
lines changed

ouroboros-network-framework/src/Ouroboros/Network/ConnectionManager/Core.hs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ import Ouroboros.Network.ConnectionId
6060
import Ouroboros.Network.ConnectionManager.InformationChannel
6161
(InformationChannel)
6262
import Ouroboros.Network.ConnectionManager.InformationChannel qualified as InfoChannel
63-
import Ouroboros.Network.ConnectionManager.State (ConnectionManagerState,
64-
ConnectionState (..), FreshIdSupply, MutableConnState (..))
63+
import Ouroboros.Network.ConnectionManager.State (ConnStateIdSupply,
64+
ConnectionManagerState, ConnectionState (..), MutableConnState (..))
6565
import Ouroboros.Network.ConnectionManager.State qualified as State
6666
import Ouroboros.Network.ConnectionManager.Types
6767
import Ouroboros.Network.InboundGovernor.Event (NewConnectionInfo (..))
@@ -402,8 +402,8 @@ with args@Arguments {
402402
classifyHandleError
403403
inboundGovernorInfoChannel
404404
k = do
405-
((freshIdSupply, stateVar, stdGenVar)
406-
:: ( FreshIdSupply m
405+
((connStateIdSupply, stateVar, stdGenVar)
406+
:: ( ConnStateIdSupply m
407407
, StrictTMVar m (ConnectionManagerState peerAddr handle handleError
408408
version m)
409409
, StrictTVar m StdGen
@@ -417,9 +417,9 @@ with args@Arguments {
417417
Just st -> Just <$> traverse (inspectTVar (Proxy :: Proxy m) . toLazyTVar . connVar) st
418418
return (TraceString (show st'))
419419

420-
freshIdSupply <- State.newFreshIdSupply (Proxy :: Proxy m)
420+
connStateIdSupply <- State.newConnStateIdSupply (Proxy :: Proxy m)
421421
stdGenVar <- newTVar (stdGen args)
422-
return (freshIdSupply, v, stdGenVar)
422+
return (connStateIdSupply, v, stdGenVar)
423423

424424
let readState
425425
:: STM m (State.ConnMap peerAddr AbstractState)
@@ -456,7 +456,7 @@ with args@Arguments {
456456
WithInitiatorMode
457457
OutboundConnectionManager {
458458
ocmAcquireConnection =
459-
acquireOutboundConnectionImpl freshIdSupply stateVar
459+
acquireOutboundConnectionImpl connStateIdSupply stateVar
460460
stdGenVar outboundHandler,
461461
ocmReleaseConnection =
462462
releaseOutboundConnectionImpl stateVar stdGenVar
@@ -471,7 +471,7 @@ with args@Arguments {
471471
WithResponderMode
472472
InboundConnectionManager {
473473
icmIncludeConnection =
474-
includeInboundConnectionImpl freshIdSupply stateVar
474+
includeInboundConnectionImpl connStateIdSupply stateVar
475475
inboundHandler,
476476
icmReleaseConnection =
477477
releaseInboundConnectionImpl stateVar,
@@ -492,14 +492,14 @@ with args@Arguments {
492492
WithInitiatorResponderMode
493493
OutboundConnectionManager {
494494
ocmAcquireConnection =
495-
acquireOutboundConnectionImpl freshIdSupply stateVar
495+
acquireOutboundConnectionImpl connStateIdSupply stateVar
496496
stdGenVar outboundHandler,
497497
ocmReleaseConnection =
498498
releaseOutboundConnectionImpl stateVar stdGenVar
499499
}
500500
InboundConnectionManager {
501501
icmIncludeConnection =
502-
includeInboundConnectionImpl freshIdSupply stateVar
502+
includeInboundConnectionImpl connStateIdSupply stateVar
503503
inboundHandler,
504504
icmReleaseConnection =
505505
releaseInboundConnectionImpl stateVar,
@@ -844,7 +844,7 @@ with args@Arguments {
844844

845845
includeInboundConnectionImpl
846846
:: HasCallStack
847-
=> FreshIdSupply m
847+
=> ConnStateIdSupply m
848848
-> StrictTMVar m (ConnectionManagerState peerAddr handle handleError version m)
849849
-> ConnectionHandlerFn handlerTrace socket peerAddr handle handleError version versionData m
850850
-> Word32
@@ -859,7 +859,7 @@ with args@Arguments {
859859
-> ConnectionId peerAddr
860860
-- ^ connection id used as an identifier of the resource
861861
-> m (Connected peerAddr handle handleError)
862-
includeInboundConnectionImpl freshIdSupply
862+
includeInboundConnectionImpl connStateIdSupply
863863
stateVar
864864
handler
865865
hardLimit
@@ -905,7 +905,7 @@ with args@Arguments {
905905
case v0 of
906906
Nothing -> do
907907
-- 'Accepted'
908-
v <- State.newMutableConnState (remoteAddress connId) freshIdSupply connState'
908+
v <- State.newMutableConnState (remoteAddress connId) connStateIdSupply connState'
909909
labelTVar (connVar v) ("conn-state-" ++ show connId)
910910
return (v, Nothing)
911911
Just v -> do
@@ -933,8 +933,8 @@ with args@Arguments {
933933
InboundState {} -> writeTVar (connVar v) connState'
934934
$> assert False v
935935

936-
TerminatingState {} -> State.newMutableConnState (remoteAddress connId) freshIdSupply connState'
937-
TerminatedState {} -> State.newMutableConnState (remoteAddress connId) freshIdSupply connState'
936+
TerminatingState {} -> State.newMutableConnState (remoteAddress connId) connStateIdSupply connState'
937+
TerminatedState {} -> State.newMutableConnState (remoteAddress connId) connStateIdSupply connState'
938938

939939
labelTVar (connVar v') ("conn-state-" ++ show connId)
940940
return (v', Just connState0')
@@ -1318,7 +1318,7 @@ with args@Arguments {
13181318
-> DiffusionMode
13191319
-> peerAddr
13201320
-> m (Connected peerAddr handle handleError)
1321-
acquireOutboundConnectionImpl freshIdSupply stateVar stdGenVar handler diffusionMode peerAddr = do
1321+
acquireOutboundConnectionImpl connStateIdSupply stateVar stdGenVar handler diffusionMode peerAddr = do
13221322
let provenance = Outbound
13231323
traceWith tracer (TrIncludeConnection provenance peerAddr)
13241324
(trace, mutableConnState@MutableConnState { connVar }
@@ -1440,7 +1440,7 @@ with args@Arguments {
14401440
let connState' = ReservedOutboundState
14411441
(mutableConnState :: MutableConnState peerAddr handle handleError
14421442
version m)
1443-
<- State.newMutableConnState peerAddr freshIdSupply connState'
1443+
<- State.newMutableConnState peerAddr freshConnIdSupply connState'
14441444
-- TODO: label `connVar` using 'ConnectionId'
14451445
labelTVar (connVar mutableConnState) ("conn-state-" ++ show peerAddr)
14461446

ouroboros-network-framework/src/Ouroboros/Network/ConnectionManager/State.hs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ module Ouroboros.Network.ConnectionManager.State
1414
, readAbstractStateMap
1515
-- * MutableConnState
1616
, MutableConnState (..)
17-
, FreshIdSupply
18-
, newFreshIdSupply
17+
, ConnStateIdSupply
18+
, ConnStateId (..)
19+
, ConnectionTransitionTrace
20+
, newConnStateIdSupply
1921
, newMutableConnState
2022
-- * ConnectionState
2123
, ConnectionState (..)
@@ -57,6 +59,14 @@ readAbstractStateMap
5759
-> STM m (ConnMap peerAddr AbstractState)
5860
readAbstractStateMap = traverse (fmap (abstractState . Known) . readTVar . connVar)
5961

62+
-- | A unique identifier of a connection.
63+
--
64+
-- It's used even when we don't yet know `ConnectionId`.
65+
--
66+
newtype ConnStateId = ConnStateId Int
67+
deriving stock (Eq, Ord, Show)
68+
deriving newtype Enum
69+
6070
-- | 'MutableConnState', which supplies a unique identifier.
6171
--
6272
-- TODO: We can get away without id, by tracking connections in
@@ -65,7 +75,7 @@ readAbstractStateMap = traverse (fmap (abstractState . Known) . readTVar . connV
6575
data MutableConnState peerAddr handle handleError version m = MutableConnState {
6676
-- | A unique identifier
6777
--
68-
connStateId :: !Int
78+
connStateId :: !ConnStateId
6979

7080
, -- | Mutable state
7181
--
@@ -82,35 +92,39 @@ instance Eq (MutableConnState peerAddr handle handleError version m) where
8292
--
8393
-- We use a fresh ids for 'MutableConnState'.
8494
--
85-
newtype FreshIdSupply m = FreshIdSupply { getFreshId :: STM m Int }
95+
newtype ConnStateIdSupply m = ConnStateIdSupply { getConnStateId :: STM m ConnStateId }
8696

8797

8898
-- | Create a 'FreshIdSupply' inside an 'STM' monad.
8999
--
90-
newFreshIdSupply :: forall m. MonadSTM m
91-
=> Proxy m -> STM m (FreshIdSupply m)
92-
newFreshIdSupply _ = do
93-
(v :: StrictTVar m Int) <- newTVar 0
94-
let getFreshId :: STM m Int
95-
getFreshId = do
100+
newConnStateIdSupply :: forall m. MonadSTM m
101+
=> Proxy m
102+
-> STM m (ConnStateIdSupply m)
103+
newConnStateIdSupply _ = do
104+
(v :: StrictTVar m ConnStateId) <- newTVar (ConnStateId 0)
105+
let getConnStateId :: STM m ConnStateId
106+
getConnStateId = do
96107
c <- readTVar v
97108
writeTVar v (succ c)
98109
return c
99-
return $ FreshIdSupply { getFreshId }
110+
return $ ConnStateIdSupply { getConnStateId }
111+
112+
113+
type ConnectionTransitionTrace peerAddr = WithName peerAddr (AbstractTransitionTrace ConnStateId)
100114

101115

102116
newMutableConnState :: forall peerAddr handle handleError version m.
103117
( MonadTraceSTM m
104118
, Typeable peerAddr
105119
)
106120
=> peerAddr
107-
-> FreshIdSupply m
121+
-> ConnStateIdSupply m
108122
-> ConnectionState peerAddr handle handleError
109123
version m
110124
-> STM m (MutableConnState peerAddr handle handleError
111125
version m)
112126
newMutableConnState peerAddr freshIdSupply connState = do
113-
connStateId <- getFreshId freshIdSupply
127+
connStateId <- getConnStateId freshIdSupply
114128
connVar <- newTVar connState
115129
-- This tracing is a no op in IO.
116130
--

0 commit comments

Comments
 (0)