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

Process all pending WS messages #3154

Merged
merged 4 commits into from
Nov 4, 2024

Conversation

kziemianek
Copy link
Member

@kziemianek kziemianek commented Oct 31, 2024

Keep on processing ws messages even if all data from tls stream was read.

Before we were reading all data from tls stream while processing only single message from websocket connection. If there was no data in stream then connection was closed, even if there were more than one message to be processed from ws connection.

Problematic flow:

  1. client sends 1000 messages very fast
  2. mio event raised
  3. all data read from tls stream
  4. only one ws message processed
  5. mio event raised
  6. no data in tls stream (all was read in previous iteration)
  7. connection closed (leaving 999 messages not processed)

@kziemianek kziemianek requested a review from a team October 31, 2024 15:27
Copy link

linear bot commented Oct 31, 2024

let mut is_closing = false;

// Looping over 'read_message' is merely a workaround for the unexpected behavior of mio event triggering.
// Final solution will be applied in P-907.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will we still have a "final solution" ? Or we stop here for the moment?

@Kailai-Wang
Copy link
Collaborator

What if we simply continue as long as tls_session.read_tls() returns Ok without checking the actual read length? process_new_packets shall be called in any case, and I assume it will be "processed" even if 0 bytes were read.

We follow it by ws data frame processing as long as mio event is triggered, something like:

    for event in &events {
        if event.readiness().is_readable() {
            // TLS layer tries to read and decrypt data.
            let tls_read_result = tls_session.read_tls(&mut tcp_stream);
            
            if tls_read_result.is_ok() {
                tls_session.process_new_packets().unwrap();
                
                // WebSocket layer reads from the decrypted TLS buffer.
                if let Ok(Some(message)) = ws_stream.read_message() {
                    // Process the WebSocket message
                    println!("Received message: {:?}", message);
                }
            }
        }
    }

@kziemianek
Copy link
Member Author

@Kailai-Wang we can do that, i've changed the code.

That was my initial fix but:

  1. I was unsure about root cause (why there are more mio events but not data in tls stream)
  2. PollOpt::Level is deprecated so if we decide to bump mio version in future we will need to change it anyway

@BillyWooo
Copy link
Collaborator

@Kailai-Wang we can do that, i've changed the code.

That was my initial fix but:

  1. I was unsure about root cause (why there are more mio events but not data in tls stream)
  2. PollOpt::Level is deprecated so if we decide to bump mio version in future we will need to change it anyway

As far as I can recall, there was problem with "more mio events but not data" vs "more data but no mio event". I can't recall the detail. That's the reason we had such fix in #2852, right ?

@kziemianek
Copy link
Member Author

@Kailai-Wang we can do that, i've changed the code.
That was my initial fix but:

  1. I was unsure about root cause (why there are more mio events but not data in tls stream)
  2. PollOpt::Level is deprecated so if we decide to bump mio version in future we will need to change it anyway

As far as I can recall, there was problem with "more mio events but not data" vs "more data but no mio event". I can't recall the detail. That's the reason we had such fix in #2852, right ?

We claimed at that time that mio events were missing.
If we expect 1000 events, but connection is closed after processing 4 of them, then connection will be deregistered and remianing will never be emited.

@Kailai-Wang Kailai-Wang merged commit 9d797a5 into dev Nov 4, 2024
21 checks passed
@Kailai-Wang Kailai-Wang deleted the p-907-only-one-ws-message-read-per-mio-event branch November 4, 2024 11:28
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.

4 participants