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

Allow WebSocket maxFrameSize to be configured #2195

Merged
merged 2 commits into from Mar 12, 2020

Conversation

bridger
Copy link
Contributor

@bridger bridger commented Feb 23, 2020

The maximum frame size of an incoming WebSocket packet can be configured. This fixes issues for apps that were running into the limit (#2195, fixes #2194).

let maxFrameSize: Int = 1024 * 1024 * 2 // 2mb
app.webSocket("api", "ws", maxFrameSize: .override(maxFrameSize)) { (request, ws) in
    websocketController.connected(request: request, connection: ws)
}

Copy link
Member

@Joannis Joannis left a comment

Choose a reason for hiding this comment

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

@bridger Thanks for creating the issue, and a huge thanks for creating a PR to back it up. Really cool 👍

I think the API design could be more future-proof if we introduce a small change elsewhere.

Sources/Vapor/Routing/RoutesBuilder+WebSocket.swift Outdated Show resolved Hide resolved
@bridger
Copy link
Contributor Author

bridger commented Feb 24, 2020

I like the idea of returning a route that can be configured. I’d be happy to make that change if that’s preferred. I’m new to Vapor so I wasn’t sure about existing practices to follow.

Sent with GitHawk

@Joannis
Copy link
Member

Joannis commented Feb 24, 2020

We're in beta stages, so for now these changes are in a good spot for consideration.

@tanner0101 tanner0101 added the enhancement New feature or request label Feb 26, 2020
@tanner0101 tanner0101 added this to In Progress in Vapor 4 via automation Feb 26, 2020
@tanner0101 tanner0101 added the semver-minor Contains new API label Feb 26, 2020
Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

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

+1 to adding this as a parameter. I do think it would be nice to be able to change the default maxFrameSize for all web socket connections as well, but that could be a separate PR. Something like:

app.webSocket.maxFrameSize = 1<<20

Sources/Vapor/Routing/RoutesBuilder+WebSocket.swift Outdated Show resolved Hide resolved
Sources/Vapor/Response/Response.swift Outdated Show resolved Hide resolved
Sources/Vapor/Server/HTTPServerUpgradeHandler.swift Outdated Show resolved Hide resolved
@bridger
Copy link
Contributor Author

bridger commented Feb 28, 2020

+1 to adding this as a parameter. I do think it would be nice to be able to change the default maxFrameSize for all web socket connections as well, but that could be a separate PR.

To support that case, I changed the parameter to be optional. Now the default (1<<14) is an internal detail. When we add an Application-level default, like app.webSocket.maxFrameSize then nil can indicate "use the application default".

Copy link
Sponsor Member

@MrLotU MrLotU left a comment

Choose a reason for hiding this comment

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

LGTM. @tanner0101 could you give this another pass too? 😄

@@ -2,11 +2,12 @@ extension RoutesBuilder {
@discardableResult
public func webSocket(
_ path: PathComponent...,
maxFrameSize: Int? = nil,
Copy link
Member

Choose a reason for hiding this comment

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

As a user, passing app.webSocket(maxFrameSize: nil) { ... } might make me think I'm disabling max frame size here. Maybe an enum instead would better express this?

enum WebSocketMaxFrameSize {
    case default
    case override(Int)
}

extension WebSocketMaxFrameSize: ExpressibleByIntegerLiteral {
    init(integerValue: Int) { 
        self = .override(integerValue)
    }
}

We could re-use this enum on the top-level config as well.

This may be overly pedantic though and perhaps a doc block explaining the behavior would be sufficient. What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The enum is a good idea! I just updated the PR to use the enum.

@tanner0101 tanner0101 moved this from Awaiting Review to Awaiting Updates in Vapor 4 Mar 6, 2020
@lmcd
Copy link

lmcd commented Mar 11, 2020

Can this be merged?

@bridger
Copy link
Contributor Author

bridger commented Mar 12, 2020

Can this be merged?

Apologies. There was an update to the PR that was waiting for me. I just made the change so I think it is good to merge.

@tanner0101 tanner0101 moved this from Awaiting Updates to Awaiting Review in Vapor 4 Mar 12, 2020
Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@tanner0101 tanner0101 merged commit 6954c47 into vapor:master Mar 12, 2020
Vapor 4 automation moved this from Awaiting Review to Done Mar 12, 2020
@tanner0101
Copy link
Member

These changes are now available in 4.0.0-rc.3.4

pull bot pushed a commit to scope-demo/vapor that referenced this pull request Mar 12, 2020
* Allow WebSocket maxFrameSize to be configured.

* Change maxFrameSize to an enum instead of optional.
nachoBonafonte pushed a commit to scope-demo/vapor that referenced this pull request Mar 17, 2020
* Remove double quotes from cooke value (vapor#2215)

* remove double quotes from cookie value

* naming

* naming

* Expose initializer of Validator (vapor#2232)

vapor#2231

Co-authored-by: seeppp <jonas.schoch@naptics.ch>

* Allow WebSocket maxFrameSize to be configured (vapor#2195)

* Allow WebSocket maxFrameSize to be configured.

* Change maxFrameSize to an enum instead of optional.

Co-authored-by: Tanner <tannernelson@gmail.com>
Co-authored-by: Seeppp <jonas.schoch@icloud.com>
Co-authored-by: seeppp <jonas.schoch@naptics.ch>
Co-authored-by: Bridger Maxwell <bridger.maxwell@gmail.com>
nachoBonafonte pushed a commit to scope-demo/vapor that referenced this pull request Mar 20, 2020
* Remove double quotes from cooke value (vapor#2215)

* remove double quotes from cookie value

* naming

* naming

* Expose initializer of Validator (vapor#2232)

vapor#2231

Co-authored-by: seeppp <jonas.schoch@naptics.ch>

* Allow WebSocket maxFrameSize to be configured (vapor#2195)

* Allow WebSocket maxFrameSize to be configured.

* Change maxFrameSize to an enum instead of optional.

* Implement `.hex`, `hexEncodedString()`, and `hexEncodedBytes()` on Sequence instead of restricting them to Collection. (vapor#2249)

- Make `hexEncodedBytes()` public, why wouldn't it be?
- Specialize `hexEncodedBytes()` on `Collection` to use a (theoretically) more efficient method. It's probably a completely unmeasurable difference in practice.
- Add unit test to make sure it works on `Sequence`s.

Co-authored-by: Tanner <tannernelson@gmail.com>
Co-authored-by: Seeppp <jonas.schoch@icloud.com>
Co-authored-by: seeppp <jonas.schoch@naptics.ch>
Co-authored-by: Bridger Maxwell <bridger.maxwell@gmail.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
nachoBonafonte pushed a commit to scope-demo/vapor that referenced this pull request Mar 23, 2020
* Remove double quotes from cooke value (vapor#2215)

* remove double quotes from cookie value

* naming

* naming

* Expose initializer of Validator (vapor#2232)

vapor#2231

Co-authored-by: seeppp <jonas.schoch@naptics.ch>

* Allow WebSocket maxFrameSize to be configured (vapor#2195)

* Allow WebSocket maxFrameSize to be configured.

* Change maxFrameSize to an enum instead of optional.

* Implement `.hex`, `hexEncodedString()`, and `hexEncodedBytes()` on Sequence instead of restricting them to Collection. (vapor#2249)

- Make `hexEncodedBytes()` public, why wouldn't it be?
- Specialize `hexEncodedBytes()` on `Collection` to use a (theoretically) more efficient method. It's probably a completely unmeasurable difference in practice.
- Add unit test to make sure it works on `Sequence`s.

* close mode output keep-alive (vapor#2257)

* fix body stream leak (vapor#2258)

Co-authored-by: Tanner <tannernelson@gmail.com>
Co-authored-by: Seeppp <jonas.schoch@icloud.com>
Co-authored-by: seeppp <jonas.schoch@naptics.ch>
Co-authored-by: Bridger Maxwell <bridger.maxwell@gmail.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
nachoBonafonte pushed a commit to scope-demo/vapor that referenced this pull request Mar 24, 2020
* Remove double quotes from cooke value (vapor#2215)

* remove double quotes from cookie value

* naming

* naming

* Expose initializer of Validator (vapor#2232)

vapor#2231

Co-authored-by: seeppp <jonas.schoch@naptics.ch>

* Allow WebSocket maxFrameSize to be configured (vapor#2195)

* Allow WebSocket maxFrameSize to be configured.

* Change maxFrameSize to an enum instead of optional.

* Implement `.hex`, `hexEncodedString()`, and `hexEncodedBytes()` on Sequence instead of restricting them to Collection. (vapor#2249)

- Make `hexEncodedBytes()` public, why wouldn't it be?
- Specialize `hexEncodedBytes()` on `Collection` to use a (theoretically) more efficient method. It's probably a completely unmeasurable difference in practice.
- Add unit test to make sure it works on `Sequence`s.

* close mode output keep-alive (vapor#2257)

* fix body stream leak (vapor#2258)

Co-authored-by: Tanner <tannernelson@gmail.com>
Co-authored-by: Seeppp <jonas.schoch@icloud.com>
Co-authored-by: seeppp <jonas.schoch@naptics.ch>
Co-authored-by: Bridger Maxwell <bridger.maxwell@gmail.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver-minor Contains new API
Projects
Vapor 4
  
Done
Development

Successfully merging this pull request may close these issues.

Allow WebSocket maxFrameSize to be configured
5 participants