Skip to content

Commit 79e974b

Browse files
committed
network-mux: added trace points
Added some new tracepoints which requires the tracers to be passed to `Mux.new` rather than `Mux.run`.
1 parent 9eee029 commit 79e974b

File tree

14 files changed

+210
-140
lines changed

14 files changed

+210
-140
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Breaking
2+
3+
- Tracers are now passed to `Network.Mux.new` rather than `Network.Mux.run`.
4+
- Added new trace points to `Network.Mux.Trace.Trace`:
5+
- `NewMux` which logs all `MiniProtocolInfo`s used to create the new `Mux` interface.
6+
- `MuxStarting` which logs that `Mux` is starting.
7+

network-mux/demo/mux-demo.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ server = do
121121

122122
serverWorker :: Bearer IO -> IO ()
123123
serverWorker bearer = do
124-
mux <- Mx.new ptcls
124+
mux <- Mx.new Mx.nullTracers ptcls
125125

126126
void $ forkIO $ do
127127
awaitResult <-
@@ -133,7 +133,7 @@ serverWorker bearer = do
133133
putStrLn $ "Result: " ++ show result
134134
Mx.stop mux
135135

136-
Mx.run Mx.nullTracers mux bearer
136+
Mx.run mux bearer
137137
where
138138
ptcls :: [MiniProtocolInfo ResponderMode]
139139
ptcls = [ MiniProtocolInfo {
@@ -181,7 +181,7 @@ client n msg = do
181181

182182
clientWorker :: Mx.Bearer IO -> Int -> String -> IO ()
183183
clientWorker bearer n msg = do
184-
mux <- Mx.new ptcls
184+
mux <- Mx.new Mx.nullTracers ptcls
185185

186186
void $ forkIO $ do
187187
awaitResult <-
@@ -193,7 +193,7 @@ clientWorker bearer n msg = do
193193
putStrLn $ "Result: " ++ show result
194194
Mx.stop mux
195195

196-
Mx.run Mx.nullTracers mux bearer
196+
Mx.run mux bearer
197197
where
198198
ptcls :: [MiniProtocolInfo Mx.InitiatorMode]
199199
ptcls = [ MiniProtocolInfo {

network-mux/src/Network/Mux.hs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ data Mux (mode :: Mode) m =
9999
muxMiniProtocols :: !(Map (MiniProtocolNum, MiniProtocolDir)
100100
(MiniProtocolState mode m)),
101101
muxControlCmdQueue :: !(StrictTQueue m (ControlCmd mode m)),
102-
muxStatus :: StrictTVar m Status
102+
muxStatus :: StrictTVar m Status,
103+
muxTracers :: Tracers m
103104
}
104105

105106

@@ -127,18 +128,21 @@ stopped Mux { muxStatus } =
127128
--
128129
new :: forall (mode :: Mode) m.
129130
MonadLabelledSTM m
130-
=> [MiniProtocolInfo mode]
131+
=> Tracers m
132+
-> [MiniProtocolInfo mode]
131133
-- ^ description of protocols run by the mux layer. Only these protocols
132134
-- one will be able to execute.
133135
-> m (Mux mode m)
134-
new ptcls = do
136+
new muxTracers ptcls = do
137+
traceWith (tracer_ muxTracers) (TraceNewMux ptcls)
135138
muxMiniProtocols <- mkMiniProtocolStateMap ptcls
136139
muxControlCmdQueue <- atomically newTQueue
137140
muxStatus <- newTVarIO Ready
138141
return Mux {
139142
muxMiniProtocols,
140143
muxControlCmdQueue,
141-
muxStatus
144+
muxStatus,
145+
muxTracers
142146
}
143147

144148
mkMiniProtocolStateMap :: MonadSTM m
@@ -224,18 +228,20 @@ run :: forall m (mode :: Mode).
224228
, MonadTimer m
225229
, MonadMask m
226230
)
227-
=> Tracers m
228-
-> Mux mode m
231+
=> Mux mode m
229232
-> Bearer m
230233
-> m ()
231-
run tracers@TracersI { tracer_,
232-
bearerTracer_
233-
}
234-
Mux { muxMiniProtocols,
234+
run Mux { muxMiniProtocols,
235235
muxControlCmdQueue,
236-
muxStatus
236+
muxStatus,
237+
muxTracers = tracers@TracersI {
238+
tracer_,
239+
bearerTracer_
240+
}
237241
}
238242
bearer@Bearer{name} = do
243+
244+
traceWith tracer_ TraceStarting
239245
egressQueue <- atomically $ newTBQueue 100
240246

241247
-- label shared variables
@@ -361,20 +367,6 @@ data ControlCmd mode m =
361367
!(MiniProtocolAction m)
362368
| CmdShutdown
363369

364-
-- | Strategy how to start a mini-protocol.
365-
--
366-
data StartOnDemandOrEagerly =
367-
-- | Start a mini-protocol promptly.
368-
StartEagerly
369-
-- | Start a mini-protocol when data is received for the given
370-
-- mini-protocol. Must be used only when initial message is sent by the
371-
-- remote side.
372-
| StartOnDemand
373-
-- | Like `StartOnDemand`, but start a mini-protocol if data is received for
374-
-- any mini-protocol set to `StartOnDemand`.
375-
| StartOnDemandAny
376-
deriving (Eq, Show)
377-
378370
data MiniProtocolAction m where
379371
MiniProtocolAction :: forall m a.
380372
{ miniProtocolAction :: ByteChannel m -> m (a, Maybe BL.ByteString),
@@ -767,8 +759,13 @@ runMiniProtocol :: forall mode m a.
767759
-> StartOnDemandOrEagerly
768760
-> (ByteChannel m -> m (a, Maybe BL.ByteString))
769761
-> m (STM m (Either SomeException a))
770-
runMiniProtocol Mux { muxMiniProtocols, muxControlCmdQueue, muxStatus}
771-
ptclNum ptclDir startMode protocolAction
762+
runMiniProtocol Mux { muxMiniProtocols,
763+
muxControlCmdQueue,
764+
muxStatus
765+
}
766+
ptclNum ptclDir
767+
startMode
768+
protocolAction
772769

773770
-- Ensure the mini-protocol is known and get the status var
774771
| Just ptclState@MiniProtocolState{miniProtocolStatusVar}

network-mux/src/Network/Mux/Trace.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ data Trace =
163163
| TraceStartOnDemandAny MiniProtocolNum MiniProtocolDir
164164
| TraceStartedOnDemand MiniProtocolNum MiniProtocolDir
165165
| TraceTerminating MiniProtocolNum MiniProtocolDir
166+
| forall mode. TraceNewMux [MiniProtocolInfo mode]
167+
| TraceStarting
166168
| TraceStopping
167169
| TraceStopped
168170

@@ -175,6 +177,8 @@ instance Show Trace where
175177
show (TraceStartOnDemandAny mid dir) = printf "Preparing to start on any (%s) in %s" (show mid) (show dir)
176178
show (TraceStartedOnDemand mid dir) = printf "Started on demand (%s) in %s" (show mid) (show dir)
177179
show (TraceTerminating mid dir) = printf "Terminating (%s) in %s" (show mid) (show dir)
180+
show (TraceNewMux infos) = printf "New mux with protocols: %s" (show infos)
181+
show TraceStarting = "Mux starting"
178182
show TraceStopping = "Mux stopping"
179183
show TraceStopped = "Mux stoppped"
180184

network-mux/src/Network/Mux/Types.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Network.Mux.Types
1616
( MiniProtocolInfo (..)
1717
, MiniProtocolNum (..)
1818
, MiniProtocolDirection (..)
19+
, StartOnDemandOrEagerly (..)
1920
, MiniProtocolLimits (..)
2021
, Mode (..)
2122
, HasInitiator
@@ -99,6 +100,7 @@ newtype MiniProtocolLimits =
99100
--
100101
maximumIngressQueue :: Int
101102
}
103+
deriving Show
102104

103105
-- $interface
104106
--
@@ -147,6 +149,7 @@ data MiniProtocolInfo (mode :: Mode) =
147149
miniProtocolCapability :: !(Maybe Int)
148150
-- ^ capability on which the mini-protocol should run
149151
}
152+
deriving Show
150153

151154
data MiniProtocolDirection (mode :: Mode) where
152155
InitiatorDirectionOnly :: MiniProtocolDirection InitiatorMode
@@ -156,6 +159,21 @@ data MiniProtocolDirection (mode :: Mode) where
156159

157160
deriving instance Eq (MiniProtocolDirection (mode :: Mode))
158161
deriving instance Ord (MiniProtocolDirection (mode :: Mode))
162+
deriving instance Show (MiniProtocolDirection (mode :: Mode))
163+
164+
-- | Strategy how to start a mini-protocol.
165+
--
166+
data StartOnDemandOrEagerly =
167+
-- | Start a mini-protocol promptly.
168+
StartEagerly
169+
-- | Start a mini-protocol when data is received for the given
170+
-- mini-protocol. Must be used only when initial message is sent by the
171+
-- remote side.
172+
| StartOnDemand
173+
-- | Like `StartOnDemand`, but start a mini-protocol if data is received for
174+
-- any mini-protocol set to `StartOnDemand`.
175+
| StartOnDemandAny
176+
deriving (Eq, Show)
159177

160178
--
161179
-- Mux status

0 commit comments

Comments
 (0)