Add Lookahead, the dual to Pipelined#93
Open
nfrisby wants to merge 2 commits into
Open
Conversation
This is intended to run on servers, which means there will be potentially hundreds of instances. Therefore, it can be worthwhile for a driver to use merely a counter rather than a queue; hence the motivation for FixedSender and TheSender.
Contributor
Author
|
With the new interface, the motivating use case (originally listed here #92 (comment)) now looks as follows: leiosNotifyServerPeerLookahead ::
forall m point announcement vote.
MonadThrow m =>
m Bool ->
-- ^ increments the number of outstanding requests
m (Message (LeiosNotify point announcement vote) StBusy StIdle) ->
-- ^ blocks until the next reply (announcement\/offer\/vote) is ready
PeerLookaheadFixedSender (LeiosNotify point announcement vote) AsServer StIdle m ()
leiosNotifyServerPeerLookahead incr next =
PeerLookaheadFixedSender responder start
where
responder :: Sender (LeiosNotify point announcement vote) AsServer VariableSender StBusy StIdle m
responder = SenderEffect $ next <&> \msg -> SenderYield ReflServerAgency msg SenderDone
-- StIdle with nothing outstanding: receive the first request (or done).
-- We must use a plain Await here: AwaitLookahead defers the StBusy->StIdle
-- send, so it is only usable once we hold a request (i.e. are at StBusy).
start ::
Peer (LeiosNotify point announcement vote) AsServer (Lookahead Z (FixedSender StBusy StIdle)) StIdle m ()
start =
Await ReflClientAgency $ \case
MsgDone -> Done ReflNobodyAgency ()
MsgLeiosNotificationRequestNext ->
Effect $ do
incr >>= \b -> when b $ throwIO MkExnLeiosNotifyExcessiveRequests
pure $ busy Zero
-- StBusy with @n@ outstanding: hand this reply off to the responder and
-- look ahead to the next request (or done), in one step.
busy :: forall n.
Nat n ->
Peer (LeiosNotify point announcement vote) AsServer (Lookahead n (FixedSender StBusy StIdle)) StBusy m ()
busy n =
AwaitLookahead TheSender ReflClientAgency $ \case
MsgDone -> drain (Succ n)
MsgLeiosNotificationRequestNext ->
Effect $ do
incr >>= \b -> when b $ throwIO MkExnLeiosNotifyExcessiveRequests
pure $ busy (Succ n)
-- on termination, flush the sends we've handed off, then Done.
drain :: forall n.
Nat n ->
Peer (LeiosNotify point announcement vote) AsServer (Lookahead n (FixedSender StBusy StIdle)) StDone m ()
drain = \case
Zero -> Done ReflNobodyAgency ()
Succ j -> FlushSender Nothing (drain j) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR supersedes PR #92. Same key idea: be able to receive (pipelined) messages possibly before sending response when you have the agency. But with better names, types, UX, etc.
The key semantic change was explained here: #92 (comment)
#92 (comment)