network-mux
Safe HaskellNone
LanguageHaskell2010

Network.Mux

Description

Network multiplexer API.

The module should be imported qualified.

Synopsis

Defining Mux protocol bundles

new Source #

Arguments

:: forall (mode :: Mode) m. MonadLabelledSTM 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.

data Mux (mode :: Mode) (m :: Type -> Type) Source #

Mux handle which allows to control the multiplexer, e.g.

  • run: run the multiplexer
  • runMiniProtocol: start a mini-protocol (eagerly or lazily)
  • stop: stop the multiplexer, causing run to return.

data Mode where Source #

Statically configured multiplexer mode.

Constructors

InitiatorMode :: Mode

Only execute initiator protocols. In this mode the multiplexer will only run its egress side.

ResponderMode :: Mode

Only execute responder protocols. It this mode the multiplexer will only run its ingress side.

InitiatorResponderMode :: Mode

Execute initiator and responder protocols. In this mode the multiplexer will run both ingress and egress sides.

data MiniProtocolInfo (mode :: Mode) Source #

A static description of a mini-protocol.

Constructors

MiniProtocolInfo 

Fields

newtype MiniProtocolNum Source #

The 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.

Constructors

MiniProtocolNum Word16 

Instances

Instances details
Enum MiniProtocolNum Source # 
Instance details

Defined in Network.Mux.Types

Ix MiniProtocolNum Source # 
Instance details

Defined in Network.Mux.Types

Show MiniProtocolNum Source # 
Instance details

Defined in Network.Mux.Types

Eq MiniProtocolNum Source # 
Instance details

Defined in Network.Mux.Types

Ord MiniProtocolNum Source # 
Instance details

Defined in Network.Mux.Types

newtype MiniProtocolLimits Source #

Per Miniprotocol limits

Constructors

MiniProtocolLimits 

Fields

  • maximumIngressQueue :: Int

    Limit on the maximum number of bytes that can be queued in the miniprotocol's ingress queue.

Running the Mux

run :: forall m (mode :: Mode). (MonadAsync m, MonadFork m, MonadLabelledSTM m, Alternative (STM m), MonadThrow (STM m), MonadTimer m, MonadMask m) => Tracer m Trace -> Mux mode m -> Bearer m -> m () Source #

run 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

stop :: forall m (mode :: Mode). MonadSTM m => Mux mode m -> m () Source #

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.

Run a mini-protocol

runMiniProtocol :: forall (mode :: Mode) m a. (Alternative (STM m), MonadSTM m, MonadThrow m, MonadThrow (STM m)) => Mux mode m -> MiniProtocolNum -> MiniProtocolDirection mode -> StartOnDemandOrEagerly -> (ByteChannel m -> m (a, Maybe ByteString)) -> m (STM m (Either SomeException a)) Source #

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.

data StartOnDemandOrEagerly Source #

Strategy how to start a mini-protocol.

Constructors

StartEagerly

Start a mini-protocol promptly.

StartOnDemand

Start 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.

type ByteChannel (m :: Type -> Type) = Channel m ByteString Source #

Channel using ByteString.

data Channel (m :: Type -> Type) a Source #

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.

Constructors

Channel 

Fields

  • send :: 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).

Bearer

data Bearer (m :: Type -> Type) Source #

Low level access to underlying socket or pipe. There are three smart constructors:

newtype MakeBearer (m :: Type -> Type) fd Source #

Constructors

MakeBearer 

Fields

newtype SDUSize Source #

Constructors

SDUSize 

Fields

Instances

Instances details
Enum SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Generic SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Associated Types

type Rep SDUSize 
Instance details

Defined in Network.Mux.Types

type Rep SDUSize = D1 ('MetaData "SDUSize" "Network.Mux.Types" "network-mux-0.5-inplace" 'True) (C1 ('MetaCons "SDUSize" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSDUSize") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word16)))

Methods

from :: SDUSize -> Rep SDUSize x #

to :: Rep SDUSize x -> SDUSize #

Num SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Integral SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Real SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Show SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Eq SDUSize Source # 
Instance details

Defined in Network.Mux.Types

Methods

(==) :: SDUSize -> SDUSize -> Bool #

(/=) :: SDUSize -> SDUSize -> Bool #

Ord SDUSize Source # 
Instance details

Defined in Network.Mux.Types

type Rep SDUSize Source # 
Instance details

Defined in Network.Mux.Types

type Rep SDUSize = D1 ('MetaData "SDUSize" "Network.Mux.Types" "network-mux-0.5-inplace" 'True) (C1 ('MetaCons "SDUSize" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSDUSize") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word16)))

Monitoring

miniProtocolStateMap :: forall (m :: Type -> Type) (mode :: Mode). MonadSTM m => Mux mode m -> Map (MiniProtocolNum, MiniProtocolDir) (STM m MiniProtocolStatus) Source #

Get information about all statically registered mini-protocols.

stopped :: forall (m :: Type -> Type) (mode :: Mode). MonadSTM m => Mux mode m -> STM m (Maybe SomeException) Source #

Await until mux stopped.

Errors

data Error Source #

Enumeration of error conditions.

Constructors

UnknownMiniProtocol MiniProtocolNum

returned by decodeSDUHeader, thrown by Bearer.

BearerClosed String

thrown by Bearer when received a null byte.

IngressQueueOverRun MiniProtocolNum MiniProtocolDir

thrown by demux when violating maximumIngressQueue byte limit.

InitiatorOnly MiniProtocolNum

thrown when data arrives on a responder channel when the mux was set up as an InitiatorApp.

IOException IOException String

IOException thrown by

SDUDecodeError String

return by decodeSDUHeader, thrown by Bearer.

SDUReadTimeout

thrown when reading of a single SDU takes too long

SDUWriteTimeout

thrown when writing a single SDU takes too long

Shutdown (Maybe SomeException) Status

Result 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.

Instances

Instances details
Exception Error Source # 
Instance details

Defined in Network.Mux.Trace

Show Error Source # 
Instance details

Defined in Network.Mux.Trace

Methods

showsPrec :: Int -> Error -> ShowS #

show :: Error -> String #

showList :: [Error] -> ShowS #

Tracing

data BearerState Source #

Constructors

Mature

Bearer has successfully completed the handshake.

Dead

Bearer is dead and the underlying bearer has been closed.

Instances

Instances details
Show BearerState Source # 
Instance details

Defined in Network.Mux.Trace

Eq BearerState Source # 
Instance details

Defined in Network.Mux.Trace

data WithBearer peerid a Source #

Type used for tracing mux events.

Constructors

WithBearer 

Fields

  • wbPeerId :: !peerid

    A tag that should identify a specific mux bearer.

  • wbEvent :: !a
     

Instances

Instances details
Generic (WithBearer peerid a) Source # 
Instance details

Defined in Network.Mux.Trace

Associated Types

type Rep (WithBearer peerid a) 
Instance details

Defined in Network.Mux.Trace

type Rep (WithBearer peerid a) = D1 ('MetaData "WithBearer" "Network.Mux.Trace" "network-mux-0.5-inplace" 'False) (C1 ('MetaCons "WithBearer" 'PrefixI 'True) (S1 ('MetaSel ('Just "wbPeerId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 peerid) :*: S1 ('MetaSel ('Just "wbEvent") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))

Methods

from :: WithBearer peerid a -> Rep (WithBearer peerid a) x #

to :: Rep (WithBearer peerid a) x -> WithBearer peerid a #

(Show peerid, Show a) => Show (WithBearer peerid a) Source # 
Instance details

Defined in Network.Mux.Trace

Methods

showsPrec :: Int -> WithBearer peerid a -> ShowS #

show :: WithBearer peerid a -> String #

showList :: [WithBearer peerid a] -> ShowS #

type Rep (WithBearer peerid a) Source # 
Instance details

Defined in Network.Mux.Trace

type Rep (WithBearer peerid a) = D1 ('MetaData "WithBearer" "Network.Mux.Trace" "network-mux-0.5-inplace" 'False) (C1 ('MetaCons "WithBearer" 'PrefixI 'True) (S1 ('MetaSel ('Just "wbPeerId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 peerid) :*: S1 ('MetaSel ('Just "wbEvent") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))