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 blocking receiver sleeping after every read #48

Merged
merged 4 commits into from
Oct 24, 2024
Merged

Conversation

jprochazk
Copy link
Member

@jprochazk jprochazk commented Oct 23, 2024

Currently, the ws_receiver_blocking function would always sleep after every read. Additionally, it did not use the value from options.delay_blocking for the sleep delay. This would result in that thread being blocked for 10ms after every read no matter what the user configured it to, meaning it would receive data at an unnecessarily slow rate.

In this PR:

  • Instead of using set_nonblocking or calling std::thread::sleep after every read, we now use set_read_timeout in the non-tokio native impls.
  • The delay_blocking option is removed in favor of read_timeout
  • The default for read_timeout is Some(10ms)

Copy link
Member

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Nice find!

But I actually think there is an even better fix we can do.

Guided by this comment:

If we replace our calls to set_nonblocking with TcpStream::set_read_timeout(Duration::from_millis(10)) (or whatever) then socket.read would block waiting for message, and then error with Error::Io and e.kind() == std::io::ErrorKind::WouldBlock (which we can then ignore)

@jprochazk
Copy link
Member Author

There's no call to set_nonblocking in the receiver impl, it just blocks. I would much rather jump ahead and switch it to polling if the bandaid here isn't good enough 😄

@jprochazk jprochazk changed the title Fix ws_receiver_blocking ignoring options.delay_blocking Fix blocking receiver sleeping after every read Oct 24, 2024
@jprochazk jprochazk added the include in changelog Include this in CHANGELOG.md label Oct 24, 2024
@jprochazk jprochazk requested a review from emilk October 24, 2024 11:28
@jprochazk
Copy link
Member Author

I ended up just going with set_read_timeout after all, at least for the time being, because using polling would introduce unsafe code into the project.

Copy link
Member

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Beautiful! LGTM if tested

@jprochazk jprochazk merged commit ac6552e into main Oct 24, 2024
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
include in changelog Include this in CHANGELOG.md
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants