-
-
Notifications
You must be signed in to change notification settings - Fork 25
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 more control over handshake (#4) #62
Conversation
First attempt at this. Server handlers now receive a WebSocketRequest instead of a WebSocketConnection. The request object provides the client's handshake parameters and allows the server to finalize handshake parameters. The server calls the requests ``accept()`` method to finish the handshake and obtain a connection object. This first version exposes the request URL, headers, and subprotocols to the server. The server is allowed to select a subprotocol. In future versions (given support in wsproto), we can expand the request object to customize headers and choose extensions. In addition, the tests are updated: one new test to cover the selection of a subprotocol, plus updates to existing tests to handle the new request->connection step. Finally, the README and example server are updated to show the new request->connection step.
Pull Request Test Coverage Report for Build 43
💛 - Coveralls |
We might want to make it so handlers have to |
I've been wondering about how to support timeouts. It seems like Trio leans towards, "let the caller specify the timeout". This feels like it will be tricky because the library has hidden certain parts of the protocol from the caller. For example:
This little snippet hides two important details:
Both of these operations could timeout, especially if the peer is malicious and leaves the handshake half-finished. But where can the caller place timeouts if they want to timeout the websocket handshakes but not waiting for an item from their internal queue? This example makes me think that there should be some timeouts inside the library for things like handshakes. The library should allow timeouts to be disabled in case the caller wants to handle it themselves. Thoughts? |
something like: with trio.move_on_after(CONNECTION_TIMEOUT) as cancel_scope:
async with open_websocket_url('ws://my.example'):
cancel_scope.shield = True
msg = await queue.get()
cancel_scope.shield = False
cancel_scope.deadline = trio.current_time() + REPLY_TIMEOUT
await ws.send_message(msg) |
Shielding isn't what you want here... setting If you want to disable the cancel scope's deadline, just do It's an interesting point that Trio doesn't have a convenient way to apply a timeout just to entering or exiting a context manager. I guess we could make a context manager wrapper that does this: async with limit_entry_exit(open_websocket_url(...), entry_timeout=..., exit_timeout=...):
... I don't know whether this will be a problem in practice? I'm curious to find out, but I don't see any way to do that except wait and see whether it frustrates people... |
I'm moving the timeout discussion over to #64 so that I can merge this PR. The timeout issues go much deeper than what's been raised here, so I'll tackle them separately. |
First attempt at this. Server handlers now receive a WebSocketRequest
instead of a WebSocketConnection. The request object provides the
client's handshake parameters and allows the server to finalize
handshake parameters. The server calls the requests
accept()
method to finish the handshake and obtain a connection object.
This first version exposes the request URL, headers, and subprotocols
to the server. The server is allowed to select a subprotocol. In future
versions (given support in wsproto), we can expand the request object
to customize headers and choose extensions.
In addition, the tests are updated: one new test to cover the
selection of a subprotocol, plus updates to existing tests to handle
the new request->connection step.
Finally, the README and example server are updated to show the new
request->connection step.