Repository navigation
feat: customize server WebSocket close on handler completion - #1636
Conversation
pjfanning
left a comment
There was a problem hiding this comment.
This adds public Scala and Java API but nothing under docs/ changes. AGENTS.md asks for docs on public API changes, so could server-side/websocket-support.md get a short section for both DSLs?
e14259a to
ad78c92
Compare
pjfanning
left a comment
There was a problem hiding this comment.
Thanks, this revision addresses my earlier points. One small, non-blocking docs suggestion: most users install handlers with the handleWebSocketMessages directive rather than calling WebSocketUpgrade directly, and the new "Closing after the last outgoing message" section only shows the low-level form. Could it add a line showing that routing-DSL users can wrap the directive with mapResponse(WebSocketUpgrade.withCompletionClose(_, code, reason)) (Java: mapResponse(r -> WebSocket.withCompletionClose(r, code, reason), ...))?
Motivation: A finite WebSocket handler can complete normally for an application-specific reason. Make it possible to report that reason without replacing the existing closing handshake. Modification: Decorate WebSocket upgrade responses in Scala and Java with the requested server Close status and reason. Describe both direct upgrade use and composing the helper with handleWebSocketMessages via mapResponse in both routing DSLs. Result: Ordinary handler completion can send the selected code and reason after its final message. Opt-in only; existing subprotocol, compression, timeout and unmodified response behavior are preserved. Tests: The exact Scala and Java routing examples compile with JDK 17 against both Scala 2.13.18 and 3.3.9 APIs. Paradox rendered both examples and both directive links. No runtime changes in this follow-up. References: RFC 6455 §§5.5.1 and 7.4; apache#1636 review.
ad78c92 to
063bba9
Compare
pjfanning
left a comment
There was a problem hiding this comment.
Thanks for adding the mapResponse examples. The code looks good to me: the close frame is validated when the application sets it, it only applies when the server handler completes first, and the peer-initiated and failure paths are tested as unchanged. One thing left:
WebSocketCompletionCloseSpec.scala and WebSocketCompletionCloseJavaApi.java are new files but carry the header for Akka-derived code ("This file is part of the Apache Pekko project, which was derived from Akka"). New code should have the standard Apache header: please delete the headers and run sbt headerCreateAll. The header check in CI does not catch this.
For whoever merges: #1608 also changes UpgradeToOtherProtocolResponseHeader, so the second of the two to merge will need a rebase.
… close tests Motivation: WebSocketCompletionCloseSpec.scala and WebSocketCompletionCloseJavaApi.java are new files but carried the header for code derived from Akka. Modification: Replace the header on both files with the standard Apache license header, as generated by sbt headerCreate. Result: The new files carry the header that AGENTS.md requires for new code. Tests: - Not run - header comments only - git diff --check: clean References: Refs apache#1636
|
I pushed de386d7 to this branch to fix the header point from my review: |
Motivation:
When a finite server WebSocket handler completes normally and initiates the
closing handshake, it sends status 1000 without a reason. Applications may
want to tell the peer why an otherwise orderly handler completed, for example
during a service restart.
Modification:
Add
WebSocketUpgrade.withCompletionClose(response, code, reason)forScala and
WebSocket.withCompletionClose(response, code, reason)for Java.Both decorate the response returned by an existing message handler overload.
The code must be permitted for servers and the reason may be empty or up to
123 UTF-8 bytes. The handler retains its subprotocol and compression settings,
including compression opt-out and per-message filtering. Neither upgrade
trait changes, and the helpers do not require a particular upgrade subtype.
The server-side WebSocket guide shows both DSLs.
Result:
When successful outbound handler completion initiates the existing closing
handshake, the selected Close code and reason follow the final application
message. Without opting in, the server still sends 1000 with no reason.
Peer-initiated closing, handler failures, close timers, client handling,
and responses that do not upgrade are unchanged.
Validation:
Ten focused in-memory tests pass on each of Scala 2.13.18 and 3.3.9 with
JDK 17. They cover the typed Java handler (
Flow<Object, TextMessage, NotUsed>)with and without subprotocol selection, compression opt-out and a per-message
filter, the final message and Close frame, default and custom close,
peer-first and failure paths, an ordinary non-upgrade response, and the
UTF-8 boundary. Both http-core MiMa checks against 2.0.0, native Scalafmt,
Java formatting and scoped header checks pass locally. No full suite or
external network test was run locally.
References:
RFC 6455 sections 5.5.1 and 7.4:
https://www.rfc-editor.org/rfc/rfc6455.html#section-5.5.1
https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4
Netty 4.2's positive test demonstrates the code/reason frame semantics,
not a matching automatic handler-completion lifecycle:
https://github.com/netty/netty/blob/fd00bc3a2a02a614fca8241d16570801a3a08314/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/CloseWebSocketFrameTest.java#L61-L71