Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(p2p): fix EventChannel, add tests #293

Merged
merged 3 commits into from
Nov 24, 2021
Merged

fix(p2p): fix EventChannel, add tests #293

merged 3 commits into from
Nov 24, 2021

Conversation

elenaf9
Copy link
Contributor

@elenaf9 elenaf9 commented Nov 23, 2021

Description of change

Refactor Sink and Stream implementation for EventChannel.
The Sink implementation allows to write a message to the channel (blocking or non-blocking).
In case of an non-blocking Sink, the Stream implementation has to be continuously polled. The polling drives the sending of messages once the inner channel has the capacity to receive it (in the SwarmTask, this polling is done in the central loop in Stream::run).
The ChannelSinkConfig allows to configure the channels behaviour:

  • in case of ChannelSinkConfig::Block:
    • Sink implementation delegates every call to the inner mpsc::Sender
    • if the inner channel has capacity for the message: the message is send and EventChannel::send returns
    • if the inner channel is full: EventChannel::send blocks until the message can be send
    • Stream implementation is not relevant.
  • in case of ChannelSinkConfig::DropLatest | ChannelSinkConfig::BufferLatest:
    • if the inner channel has capacity for the message:
      • start sending the messages
      • polling <EventChannel as Stream> flushes the message
    • if the inner channel is full:
      • if ChannelSinkConfig::BufferLatest
        • Write message to buffer
        • polling <EventChannel as Stream> writes from buffer to inner channel once there is capacity
      • if ChannelSinkConfig::DropLatest: drop message

Type of change

Choose a type of change, and delete any options that are not relevant.

  • Bug fix (a non-breaking change which fixes an issue)

How the change has been tested

Added event_channel::test module to test every ChannelSinkConfig.

Change checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@elenaf9 elenaf9 requested a review from felsweg-iota November 23, 2021 16:04
}

impl<T> EventChannel<T> {
pub fn new(capacity: usize, config: ChannelSinkConfig) -> (Self, mpsc::Receiver<T>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since an EventChannel<T> can only have the values of ChannelSinkConfig this implementation is Ok, but I would suggest to leave room for extension: Reduce the responsibility of new to one, create new functions like new_blocking(..), new_drop_latest(...), etc...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally favor having a single new method with an enum, over having a separate new method for each variant. Especially since the rest of the parameters (in this case only capacity) is the same for all variants, hence all 3 methods would have the same signature.

Copy link
Contributor

@felsweg-iota felsweg-iota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I also ask you to describe the bug in a few sentences, that has been fixed? Thank you :)

@elenaf9
Copy link
Contributor Author

elenaf9 commented Nov 24, 2021

May I also ask you to describe the bug in a few sentences, that has been fixed? Thank you :)

The bug was caused by a fundamental misunderstanding on my side of what Sink::poll_flush does. In the old implementation I assumed that "flushing" should do something like clear all pending requests, hence I actually just cleared the buffer and then called inner.poll_flush. In reality poll_flush is responsible for doing the actual writing of the request and in case of mpsc::Sender it blocks the send operation until the channel has capacity to received the request. Now with this PR it is implemented that poll_flush only blocks in case of ChannelSinkConfig::Block, otherwise it immediately returns and the send-request-once-channel-has-capacity part is done in the Stream implementation

@elenaf9 elenaf9 force-pushed the p2p/fix/event-channel branch from 15a7b29 to b64e615 Compare November 24, 2021 13:25
@elenaf9 elenaf9 requested a review from felsweg-iota November 24, 2021 13:30
Copy link
Contributor

@felsweg-iota felsweg-iota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants