Skip to content

Commit

Permalink
Reduced websocket latency
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Apr 13, 2021
1 parent a7056a1 commit 6826887
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Release 0.12.2
- Reduced *WebSocket* latency.

## Release 0.12.1
- *WebSocket* now can returns when send correctly a `SendStatus::MaxPacketSizeExceeded` instead of `ResourceNotFound` if the packet size is exceeded.
- *UDP* has increases the packet size when send.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "message-io"
version = "0.12.1"
version = "0.12.2"
authors = ["lemunozm <[email protected]>"]
edition = "2018"
readme = "README.md"
Expand Down
14 changes: 14 additions & 0 deletions src/adapters/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,24 @@ impl Remote for RemoteResource {
match web_socket.read_message() {
Ok(message) => match message {
Message::Binary(data) => {
// As an optimization.
// Fast check to knwo if there is more data to avoid call
// WebSocker::read_message() again.
let mut should_wait = false;
if let Err(err) = web_socket.get_ref().peek(&mut [0; 0]) {
if err.kind() == ErrorKind::WouldBlock {
should_wait = true;
}
}

// We can not call process_data while the socket is blocked.
// The user could lock it again if sends from the callback.
drop(state);
process_data(&data);

if should_wait {
break ReadStatus::WaitNextEvent
}
}
Message::Close(_) => break ReadStatus::Disconnected,
_ => continue,
Expand Down

0 comments on commit 6826887

Please sign in to comment.