Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/main/paradox/client-side/websocket-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ the WebSocket stream fail with a `java.util.concurrent.TimeoutException`. If the
deadline but cannot accept it as a WebSocket upgrade, the status and parsed headers are still available through the
upgrade result's @scala[`response`]@java[`response()`].

If the opening handshake itself is cancelled, the upgrade result fails even when the timeout is disabled.

The deadline may overlap transport setup, but does not replace the TCP connection or TLS timeouts. It stops
on a successful upgrade and is independent of idle timeouts and the WebSocket close handshake. It can also be
configured through `ClientConnectionSettings` and `WebSocketSettings.withOpeningTimeout`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private[http] object WebSocketClientBlueprint {
setHandlers(in, out, this)

override def onDownstreamFinish(cause: Throwable): Unit = {
if (openingDeadline.isDefined && !result.isCompleted)
if (!result.isCompleted)
result.tryFailure(new RuntimeException("WebSocket opening handshake was cancelled before completion.",
cause))
super.onDownstreamFinish(cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,33 +215,45 @@ class WebSocketClientSpec extends PekkoSpecWithMaterializer("pekko.http.client.w
}
}

"complete the opening handshake result when cancelled before dispatch" in {
Utils.assertAllStagesStopped {
val defaults = ClientConnectionSettings(system)
val settings = defaults.withWebsocketSettings(defaults.websocketSettings.withOpeningTimeout(1.minute))
val handshake =
WebSocketClientBlueprint.handshake(WebSocketRequest("ws://example.org/ws"), settings, system.log)
val incoming = TestPublisher.probe[ByteString]()
val application = TestSubscriber.probe[ByteString]()
val network = TestSubscriber.probe[ByteString]()
val result = RunnableGraph.fromGraph(GraphDSL.createGraph(handshake) { implicit b => stage =>
import GraphDSL.Implicits._
Source.fromPublisher(incoming) ~> stage.in1
stage.out1 ~> Sink.fromSubscriber(application)
Source.maybe[ByteString] ~> stage.in2
stage.out2 ~> Sink.fromSubscriber(network)
ClosedShape
}).run()

network.ensureSubscription()
try {
// The network has not requested an opening handshake, so the deadline cannot fire.
application.cancel()
result.failed.awaitResult(3.seconds.dilated).getMessage should include("cancelled before completion")
incoming.expectCancellation()
} finally network.cancel()
Seq(
"complete the opening handshake result when cancelled before dispatch" -> false,
"complete the opening handshake result when cancelled after dispatch without an opening timeout" -> true)
.foreach { case (description, afterDispatch) =>
description in {
Utils.assertAllStagesStopped {
val defaults = ClientConnectionSettings(system)
val settings =
if (afterDispatch) defaults
else defaults.withWebsocketSettings(defaults.websocketSettings.withOpeningTimeout(1.minute))
val handshake =
WebSocketClientBlueprint.handshake(WebSocketRequest("ws://example.org/ws"), settings, system.log)
val incoming = TestPublisher.probe[ByteString]()
val application = TestSubscriber.probe[ByteString]()
val network = TestSubscriber.probe[ByteString]()
val result = RunnableGraph.fromGraph(GraphDSL.createGraph(handshake) { implicit b => stage =>
import GraphDSL.Implicits._
Source.fromPublisher(incoming) ~> stage.in1
stage.out1 ~> Sink.fromSubscriber(application)
Source.maybe[ByteString] ~> stage.in2
stage.out2 ~> Sink.fromSubscriber(network)
ClosedShape
}).run()

network.ensureSubscription()
try {
if (afterDispatch) {
settings.websocketSettings.openingTimeout shouldBe Duration.Inf
network.requestNext().utf8String should startWith("GET /ws HTTP/1.1\r\n")
}
// Do not respond or close the input: the handshake is ended by downstream cancellation alone.
result.isCompleted shouldBe false
application.cancel()
incoming.expectCancellation()
result.failed.awaitResult(3.seconds.dilated).getMessage should include("cancelled before completion")
} finally network.cancel()
}
}
}
}

"retain the HTTP rejection with a configured opening handshake timeout" in {
Utils.assertAllStagesStopped {
Expand Down