Skip to content

Commit

Permalink
ouroboros-network-framework: added unbounded buffered channel
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Jun 24, 2022
1 parent 9cf5713 commit cd83fdd
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ouroboros-network-framework/src/Ouroboros/Network/Channel.hs
Expand Up @@ -13,6 +13,7 @@ module Ouroboros.Network.Channel
, mvarsAsChannel
, handlesAsChannel
, createConnectedChannels
, createConnectedBufferedChannelsUnbounded
, createConnectedBufferedChannels
, createConnectedBufferedChannelsSTM
, createPipelineTestChannels
Expand Down Expand Up @@ -175,6 +176,30 @@ createConnectedChannels = do
return (mvarsAsChannel bufferB bufferA,
mvarsAsChannel bufferA bufferB)


-- | Create a pair of channels that are connected via two unbounded buffers.
--
-- This is primarily useful for testing protocols.
--
createConnectedBufferedChannelsUnbounded :: forall m a. MonadSTM m
=> m (Channel m a, Channel m a)
createConnectedBufferedChannelsUnbounded = do
-- Create two TQueues to act as the channel buffers (one for each
-- direction) and use them to make both ends of a bidirectional channel
bufferA <- atomically $ newTQueue
bufferB <- atomically $ newTQueue

return (queuesAsChannel bufferB bufferA,
queuesAsChannel bufferA bufferB)
where
queuesAsChannel bufferRead bufferWrite =
Channel{send, recv, tryRecv}
where
send x = atomically (writeTQueue bufferWrite x)
recv = atomically ( Just <$> readTQueue bufferRead)
tryRecv = atomically (fmap Just <$> tryReadTQueue bufferRead)


-- | Create a pair of channels that are connected via N-place buffers.
--
-- This variant /blocks/ when 'send' would exceed the maximum buffer size.
Expand Down

0 comments on commit cd83fdd

Please sign in to comment.