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

Introduce the capability to aggregate WebSocket continuation frames. #5357

Merged
merged 5 commits into from
Jan 24, 2024

Conversation

minwoox
Copy link
Member

@minwoox minwoox commented Dec 22, 2023

Motivation:
Users may find it beneficial to aggregate WebSocket continuation frames and their text or binary frame into a single frame instead of managing them individually.

Modifications:

  • Introduce properties WebSocketServiceBuilder.aggregateContinuation() and WebSocketClientBuilder.aggregateContinuation() to enable the aggregation of continuation frames.

Result:

  • You now have the option to aggregate WebSocket continuation frames for more streamlined handling.

Motivation:
Users may find it beneficial to aggregate WebSocket continuation frames and their text or binary frame into a single frame instead of managing them individually.

Modifications:
- Introduce properties `WebSocketServiceBuilder.aggregateContinuation()` and `WebSocketClientBuilder.aggregateContinuation()` to enable the aggregation of continuation frames.

Result:
- You now have the option to aggregate WebSocket continuation frames for more streamlined handling.
Copy link

codecov bot commented Dec 22, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (3963f2a) 0.00% compared to head (5ae09ea) 73.69%.

❗ Current head 5ae09ea differs from pull request most recent head 03cb41e. Consider uploading reports for the commit 03cb41e to get more accurate results

Files Patch % Lines
...ternal/common/websocket/WebSocketFrameDecoder.java 91.66% 1 Missing and 2 partials ⚠️
...meria/internal/common/websocket/WebSocketUtil.java 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5357       +/-   ##
===========================================
+ Coverage        0   73.69%   +73.69%     
- Complexity      0    20126    +20126     
===========================================
  Files           0     1741     +1741     
  Lines           0    74392    +74392     
  Branches        0     9491     +9491     
===========================================
+ Hits            0    54824    +54824     
- Misses          0    15038    +15038     
- Partials        0     4530     +4530     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

This is cut out for me to implement #5167 😆

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Changes look straightforward enough 👍 Thanks @minwoox 🙇 👍 🙇

Comment on lines 323 to 324
// The message must not exceed 125 length:
// https://datatracker.ietf.org/doc/html/rfc6455/#section-5.5
Copy link
Contributor

Choose a reason for hiding this comment

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

nit; this comment probably isn't related since it refers to control frames only

Suggested change
// The message must not exceed 125 length:
// https://datatracker.ietf.org/doc/html/rfc6455/#section-5.5

Copy link
Member Author

Choose a reason for hiding this comment

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

The message is set to the reason phrase of the close frame and the close frame is one of control frames so I left the comment here. 😉
However, I've changed to logic to truncate the message if it exceeds the limit in WebSocketUtil so I've moved this comment to there. PTAL. 😉

@@ -80,6 +81,7 @@ public final class WebSocketClientBuilder extends AbstractWebClientBuilder {
private int maxFramePayloadLength = DEFAULT_MAX_FRAME_PAYLOAD_LENGTH;
private boolean allowMaskMismatch;
private List<String> subprotocols = ImmutableList.of();
private boolean aggregateContinuation;
Copy link
Contributor

Choose a reason for hiding this comment

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

minor question) Maybe it is just me, but I feel like most users won't be interested in continuation frames and true would be the more sensible default.

Copy link
Contributor

@ikhoon ikhoon Jan 9, 2024

Choose a reason for hiding this comment

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

I think we may provide a higher API that can automatically aggregate continuation frames with StreamMessage<String> or StreamMessage<(ByteBuf|byte[])>.

Copy link
Member Author

Choose a reason for hiding this comment

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

most users won't be interested in continuation frame

I completely agree with your perspective on this matter, but I believe that setting true for aggregateContinuation as a default is a separate consideration.
If continuation frames are used that the server developers do not notice, the server may experience memory pressure. So I prefer to set it false. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we may provide a higher API that can automatically aggregate continuation frames with StreamMessage or StreamMessage<(ByteBuf|byte[])>.

Could you share an example of the usage? I'm not sure how it's going to be used because what we need here is the stream message of WebSocketFrame instead of strings or byte arrays. 🤔

Copy link
Contributor

@ikhoon ikhoon Jan 16, 2024

Choose a reason for hiding this comment

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

Continuation is a low-level detail that many users might not be interested in for their business logic. I think we can abstract a set of WebSocket frames followed by continuation frames with StreamMessage so that users easily aggregate the whole message or subscribe to the messages one by one.

TextStreamMessage extends StreamMessage<String> {
   CompletableFuture<String> aggregate();
}

ByteStreamMessage extends StreamMessage<byte[]> {
   CompletableFuture<byte[]> aggregate();
}

interface WebSocketStreamHandler {
   void onText(RequestContext ctx, TextStreamMessage texts, WebSocketWriter out);
   
   void onBinary(RequestContext ctx, ByteStreamMessage bytes, WebSocketWriter out);
   ...  
}
class EchoWebSocketStreamHandler implements WebSocketStreamHandler {
   void onText(RequestContext ctx, TextStreamMessage texts, WebSocketWriter out) {
     // If users want to get the whole text
     texts.aggregate().thenAccept(text -> {
        out.write(text);
     });
     // To get a text frame one by one with backpressure
     texts.subscribe(...)
   }
   
   void onBinary(RequestContext ctx, ByteStreamMessage bytes, WebSocketWriter out) {
       ...
   }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I got it. I think I've seen a similar API from AKKA whether I like it or not. 😆

We can probably provide two different web socket handler APIs later.
The first one is the API you showed above and the second one is the simple callback style that you've used in your k8s PR.
https://github.com/line/armeria/pull/5167/files#diff-6dfa9f45d08fd2befd27967eec61221bcb1489bb0ceef6764d39f93aa0369d2bR138

For the second one, I think using this boolean value is more proper because the first one introduces another layer with a completable future that isn't needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. We don't know which one is preferred by users. Both versions could be provided later if necessary.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

This is a useful option! 🚀🙇‍♂️

@ikhoon ikhoon merged commit 3f7dc14 into line:main Jan 24, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants