new :: MonadLabelledSTM m=> Tracers m-> [MiniProtocolInfo mode]description of protocols run by the mux layer. Only these protocols one will be able to execute.
-> m (Mux mode m)
Create a mux handle in Mode and register mini-protocols.
:: a typeCtrl KGHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12
Modulenetwork-mux-0.11.0.0Haskell2010
Network multiplexer API.
The module should be imported qualified.
new :: MonadLabelledSTM m=> Tracers m-> [MiniProtocolInfo mode]description of protocols run by the mux layer. Only these protocols one will be able to execute.
-> m (Mux mode m)Create a mux handle in Mode and register mini-protocols.
Mux handle which allows to control the multiplexer, e.g.
run: run the multiplexer
runMiniProtocol: start a mini-protocol (eagerly or lazily)
Statically configured multiplexer mode.
InitiatorMode :: ModeOnly execute initiator protocols. In this mode the multiplexer will only run its egress side.
ResponderMode :: ModeOnly execute responder protocols. It this mode the multiplexer will only run its ingress side.
InitiatorResponderMode :: ModeExecute initiator and responder protocols. In this mode the multiplexer will run both ingress and egress sides.
A static description of a mini-protocol.
MiniProtocolInfominiProtocolNum :: !MiniProtocolNumUnique mini-protocol number.
miniProtocolDir :: !MiniProtocolDirection modeMini-protocol direction.
miniProtocolLimits :: !MiniProtocolLimitsingress queue limits for the protocol
miniProtocolCapability :: !Maybe Intcapability on which the mini-protocol should run
Show (MiniProtocolInfo mode)Defined in network-mux-0.11.0.0 · Network.Mux.TypesThe wire format includes the protocol numbers, and it's vital that these are stable. They are not necessarily dense however, as new ones are added and some old ones retired. So we use a dedicated class for this rather than reusing Enum. This also covers unrecognised protocol numbers on the decoding side.
Enum MiniProtocolNumDefined in network-mux-0.11.0.0 · Network.Mux.TypesEq MiniProtocolNumDefined in network-mux-0.11.0.0 · Network.Mux.TypesOrd MiniProtocolNumDefined in network-mux-0.11.0.0 · Network.Mux.TypesShow MiniProtocolNumDefined in network-mux-0.11.0.0 · Network.Mux.TypesIx MiniProtocolNumDefined in network-mux-0.11.0.0 · Network.Mux.TypesEq (MiniProtocolDirection mode)Defined in network-mux-0.11.0.0 · Network.Mux.TypesOrd (MiniProtocolDirection mode)Defined in network-mux-0.11.0.0 · Network.Mux.TypesShow (MiniProtocolDirection mode)Defined in network-mux-0.11.0.0 · Network.Mux.TypesPer Miniprotocol limits
MiniProtocolLimitsmaximumIngressQueue :: IntLimit on the maximum number of bytes that can be queued in the miniprotocol's ingress queue.
Show MiniProtocolLimitsDefined in network-mux-0.11.0.0 · Network.Mux.Typesrun starts a mux bearer for the specified protocols corresponding to one of the provided Versions.
Isometric flow control: analysis of head-of-line blocking of the ingress side of the multiplexer
For each mini-protocol (enumerated by ptcl), mux will create two
channels. One for initiator and one for the responder. Each channel will use
a single Wanton. When it is filled, it is put in a common queue
tsrQueue. This means that the queue is bound by 2 * |ptcl|. Every side
of a mini-protocol is served by a single Wanton: when an application sends
data, the channel will try to put it into the Wanton (which might block).
Wantons are taken from the tsrQueue queue by one of mux threads. This
eliminates head of line blocking: each mini-protocol thread can block on
putting more bytes into its Wanton, but it cannot block the other
mini-protocols or the thread that is reading the tsrQueue queue. This is
ensured since the muxChannel will put only a non-empty Wanton to the
tsrQueue queue, and on such wantons the queue is never blocked. This means
that the only way the queue can block is when its empty, which means that
none of the mini-protocols wanted to send. The egress part will read
a Wanton, take a fixed amount of bytes encode them in as an MuxSDU; if
there are leftovers it will put them back in the Wanton and place it at the
end of the queue (reading and writing to it will happen in a single STM
transaction which assures that the order of requests from a mini-protocol is
preserved.
Properties:
at any given time the tsrQueue contains at most one
TranslocationServiceRequest from a given mini-protocol of the given
MiniProtocolDir, thus the queue contains at most 2 * |ptcl|
translocation requests.
at any given time each TranslocationServiceRequest contains a non-empty
Wanton
Shut down the mux. This will cause run to return. It does not wait for any protocol threads to finish, so you should do that first if necessary.
Arrange to run a protocol thread (for a particular MiniProtocolNum and MiniProtocolDirection) to interact on this protocol's Channel.
The protocol thread can either be started eagerly or on-demand:
With StartEagerly, the thread is started promptly. This is appropriate for mini-protocols where the opening message may be sent by this thread.
With StartOnDemand, the thread is not started until the first data is received for this mini-protocol. This is appropriate for mini-protocols where the opening message is sent by the remote peer.
The result is a STM action to block and wait on the protocol completion. It is safe to call this completion action multiple times: it will always return the same result once the protocol thread completes. In case the Mux has stopped, either due to an exception or because of a call to muxStop a `Left Error` will be returned from the STM action.
It is an error to start a new protocol thread while one is still running, for the same MiniProtocolNum and MiniProtocolDirection. This can easily be avoided by using the STM completion action to wait for the previous one to finish.
It is safe to ask to start a protocol thread before run. In this case the protocol thread will not actually start until run is called, irrespective of the StartOnDemandOrEagerly value.
Strategy how to start a mini-protocol.
StartEagerlyStart a mini-protocol promptly.
StartOnDemandStart a mini-protocol when data is received for the given mini-protocol. Must be used only when initial message is sent by the remote side.
StartOnDemandAnyLike StartOnDemand, but start a mini-protocol if data is received for any mini-protocol set to StartOnDemand.
Eq StartOnDemandOrEagerlyDefined in network-mux-0.11.0.0 · Network.Mux.TypesShow StartOnDemandOrEagerlyDefined in network-mux-0.11.0.0 · Network.Mux.TypesChannel using ByteString.
A channel which can send and receive values.
It is more general than what `network-mux` requires, see ByteChannel
instead. However this is useful for testing purposes when one is either
using mux or connecting two ends directly.
Channelsend :: a -> m ()Write bytes to the channel.
It maybe raise exceptions.
recv :: m (Maybe a)Read some input from the channel, or Nothing to indicate EOF.
Note that having received EOF it is still possible to send. The EOF condition is however monotonic.
It may raise exceptions (as appropriate for the monad and kind of channel).
Low level access to underlying socket or pipe. There are three smart constructors:
Network.Socket.socketAsBearer
Network.Pipe.pipeAsBearer
Test.Mux.queuesAsBearerConstruct a bearer using a MakeBearerCb.
MakeBearergetBearer :: MakeBearerCb m fdReadBuffer for Mux Bearers
This is used to read more data than whats currently needed in one syscall. Any extra data read is cached in rbVar until the next read request.
withReadBufferIO :: (Maybe (ReadBuffer m) -> m a) -> m aMonadReadBuffer IODefined in network-mux-0.11.0.0 · Network.Mux.BearerEnum SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesEq SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesIntegral SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesNum SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesOrd SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesReal SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesShow SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.TypesGeneric SDUSizeDefined in network-mux-0.11.0.0 · Network.Mux.Typestype Rep SDUSize = D1 ('MetaData "SDUSize"
"Network.Mux.Types"
"network-mux-0.11.0.0-HTPTgbnoxBEJogkIsdugRg"
'True) (C1 ('MetaCons "SDUSize"
'PrefixI 'True) (S1 ('MetaSel ('Just "getSDUSize"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word16)))Defined in network-mux-0.11.0.0 · Network.Mux.TypesGet information about all statically registered mini-protocols.
Await until mux stopped.
Enumeration of error conditions.
UnknownMiniProtocol MiniProtocolNumreturned by decodeSDUHeader, thrown by Bearer.
BearerClosed Stringthrown by Bearer when received a null byte.
IngressQueueOverRun MiniProtocolNum MiniProtocolDirthrown by demux when violating maximumIngressQueue
byte limit.
InitiatorOnly MiniProtocolNumthrown when data arrives on a responder channel when the
mux was set up as an InitiatorApp.
IOException IOException StringIOException thrown by
SDUDecodeError Stringreturn by decodeSDUHeader, thrown by Bearer.
SDUReadTimeoutthrown when reading of a single SDU takes too long
SDUWriteTimeoutthrown when writing a single SDU takes too long
Shutdown (Maybe SomeException) StatusResult of runMiniProtocol's completionAction in case of an error or mux being closed while a mini-protocol was still running, this is not a clean exit.
Show RuntimeErrorDefined in network-mux-0.11.0.0 · Network.Mux.TypesException RuntimeErrorDefined in network-mux-0.11.0.0 · Network.Mux.TypesBundle of tracers used directly by mux.
Tracerstracer :: Tracer m (f Trace)high-level tracer of mux state events
channelTracer :: Tracer m (f ChannelTrace)channel tracer
bearerTracer :: Tracer m (f BearerTrace)high-frequency tracer
pattern TracersI :: Monad m => Tracer m Trace -> Tracer m ChannelTrace -> Tracer m BearerTrace -> Tracers mContravariant natural transformation of Tracers m`.
High-level mux events.
TraceState StateTraceCleanExit MiniProtocolNum MiniProtocolDirTraceExceptionExit MiniProtocolNum MiniProtocolDir SomeExceptionTraceStartEagerly MiniProtocolNum MiniProtocolDirTraceStartOnDemand MiniProtocolNum MiniProtocolDirTraceStartOnDemandAny MiniProtocolNum MiniProtocolDirTraceStartedOnDemand MiniProtocolNum MiniProtocolDirTraceTerminating MiniProtocolNum MiniProtocolDirforall mode. TraceNewMux [MiniProtocolInfo mode]TraceStartingTraceStoppingTraceStoppedLow-level bearer trace tags (these are not traced by the tracer which is passed to Mux).
TraceRecvHeaderStartTraceRecvHeaderEnd SDUHeaderTraceRecvDeltaQObservation SDUHeader TimeTraceRecvDeltaQSample Double Int Int Double Double Double Double StringTraceEmitDeltaQTraceRecvRaw IntTraceRecvStart IntTraceRecvEnd IntTraceSendStart SDUHeaderTraceSendEndTraceSDUReadTimeoutExceptionTraceSDUWriteTimeoutExceptionTraceTCPInfo StructTCPInfo Word16Show BearerTraceDefined in network-mux-0.11.0.0 · Network.Mux.TypesMid-level channel events traced independently by each mini protocol job.
Show ChannelTraceDefined in network-mux-0.11.0.0 · Network.Mux.TraceType used for tracing mux events.
(Show peerid, Show a) => Show (WithBearer peerid a)Defined in network-mux-0.11.0.0 · Network.Mux.TraceGeneric (WithBearer peerid a)Defined in network-mux-0.11.0.0 · Network.Mux.Tracetype Rep (WithBearer peerid a) = D1 ('MetaData "WithBearer"
"Network.Mux.Trace"
"network-mux-0.11.0.0-HTPTgbnoxBEJogkIsdugRg"
'False) (C1 ('MetaCons "WithBearer"
'PrefixI 'True) (S1 ('MetaSel ('Just "wbPeerId"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 peerid) :*: S1 ('MetaSel ('Just "wbEvent"
) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))Defined in network-mux-0.11.0.0 · Network.Mux.Trace