Allow delaying HTTP upgrade decisions. (#844)

Motivation:

While the HTTPServerUpgrader and WebSocketUpgrader allowed users to take
their time when reconfiguring the pipeline after they decided to upgrade,
users had to synchronously decide if they wanted to upgrade. This is a bit
inconvenient.

A particular limitation here is that some routes may want to upgrade
only if the upgrading user is authenticated. Checking authentication
credentials is almost always an I/O operation, and so cannot safely be done
on the event loop. Our original design made this impossible.

Modifications:

- Changed the shouldUpgrade callback to return a Future.
- Passed the shouldUpgrade callback the Channel that is upgrading, in
    no small part so that it has an EventLoop it can create Futures
    on.
- Rewrote the upgrader to handle this new state.

Result:

Users can delay decisions about when to upgrade.
This commit is contained in:
Cory Benfield authored and Johannes Weiss committed 2019-03-01 18:24:59 +00:00
1 parent 56669eabd2
commit 9c07f89ec7
8 files changed
+570 -120

No files matched your search

+1 -1
View File
@@ -202,7 +202,7 @@ private final class WebSocketTimeHandler: ChannelInboundHandler {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let upgrader = WebSocketUpgrader(shouldUpgrade: { (head: HTTPRequestHead) in HTTPHeaders() },
let upgrader = WebSocketUpgrader(shouldUpgrade: { (channel: Channel, head: HTTPRequestHead) in channel.eventLoop.makeSucceededFuture(HTTPHeaders()) },
upgradePipelineHandler: { (channel: Channel, _: HTTPRequestHead) in
channel.pipeline.addHandler(WebSocketTimeHandler())
})