-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(socketio): add ability to wait for socketio completion #3
Conversation
e04deca
to
47f6540
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it works, I do not really know whether this makes the API design better.
It feels a little janky to have that wait_for_completion
function while the client doesn't really have a notion of completion. I am thinking of alternative and ergonomic ways of using the client.
One should be able to:
- manually disconnect
- send messages
- receive messages
- receive an error and forcefully tell the user of the client about that
Without calling the wait_for_completion
function, you wouldn't know that there was an error, ay?
/// If the connection has not been started yet, this terminates immediately, | ||
/// so make sure to call [`Client::poll_stream`] (done by [`ClientBuilder::connect`]) | ||
/// first. | ||
pub async fn wait_for_completion(&self) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should return some kind of result (like an error reason?), as it can fail in various ways:
- Client was never connected
- Client did successfully disconnect itself (graceful shutdown requested).
- Server sent an error or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Client did successfully disconnect itself (graceful shutdown requested).
We could make the DisconnectReason
public and return it here.
Server sent an error or whatever.
That's handled through .on(Event::Error
.
Client was never connected
We could return a Result which contains an error if the client was not connected and the disconnect reason otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then why don't handle the server-side disconnect via the on
callbacks and make this poll function blocking? I see some kind of split API here and I feel like not knowing which method to use when.
If the polling task is completed, the client is done, until you call
Depends on what you define as |
47f6540
to
2a9a6ba
Compare
This is also a little weird. Shouldn't I call But this aside, as said in the discussion above, not sure whether the additional function is the way to go. 😊 |
4ead8c8
to
e9b5ace
Compare
I think closing the upper stream is a better option. Thank you. :) |
e9b5ace
to
2cafa9e
Compare
When the async client connects to the server, it spawns a new thread to handle the arriving messages asynchronously, which is immediately detached with no chance of awaiting its completion. For long-running programs (e.g. a client program that never really disconnects from the server) this can be problematic, as unexpected stream completions would go unnoticed. This may happen if the underlying tungstenite websocket shuts down ('Received close frame: None') but there are no engineio/socketio close frames. Hence, since the stream terminates, the message handling task stops without a Close or Error event being fired. Thus, we now fire an additional Event::Close when the stream terminates, to signal the (potentially unexpected) close to the user.
2cafa9e
to
0c492fb
Compare
Submitted to upstream. |
When the async client connects to the server, it spawns a new thread to handle the arriving messages asynchronously. Previously, this thread was immediately detached with no chance of awaiting its completion.
For long-running programs (e.g. a client program that never really disconnects from the server) this can be problematic, as unexpected stream completions would go unnoticed. This may happen if the underlying tungstenite websocket shuts down ('Received close frame: None') but there are no engineio/socketio close frames. Hence, since the stream terminates, the message handling task stops without a Close or Error event being fired.
Therefore, we now provide a new
wait_for_completion
function on the socketio client which allows to wait until the polling stream has terminated. Programs can then use this to determine whether an unexpected shutdown has happened and take further actions on their own.