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

feat: reconnect refactor #229

Merged
merged 11 commits into from
Oct 20, 2022
Merged

feat: reconnect refactor #229

merged 11 commits into from
Oct 20, 2022

Conversation

SSebo
Copy link
Contributor

@SSebo SSebo commented Aug 19, 2022

@nshaaban-cPacket This PR is based on your POC 👍
@nshaaban-cPacket @1c3t3a PTAL

@codecov
Copy link

codecov bot commented Aug 19, 2022

Codecov Report

Merging #229 (76193b7) into main (7d5bbb4) will increase coverage by 1.06%.
The diff coverage is 89.68%.

@@            Coverage Diff             @@
##             main     #229      +/-   ##
==========================================
+ Coverage   86.38%   87.44%   +1.06%     
==========================================
  Files          30       31       +1     
  Lines        2350     2478     +128     
==========================================
+ Hits         2030     2167     +137     
+ Misses        320      311       -9     
Impacted Files Coverage Δ
socketio/src/client/callback.rs 37.50% <ø> (ø)
socketio/src/error.rs 100.00% <ø> (ø)
socketio/src/socket.rs 78.49% <66.66%> (+4.02%) ⬆️
socketio/src/client/raw_client.rs 87.17% <87.17%> (ø)
socketio/src/client/client.rs 94.91% <94.59%> (+9.05%) ⬆️
engineio/src/client/client.rs 94.60% <100.00%> (+0.71%) ⬆️
engineio/src/packet.rs 97.95% <100.00%> (ø)
engineio/src/socket.rs 77.77% <100.00%> (+3.70%) ⬆️
socketio/src/client/builder.rs 92.95% <100.00%> (+2.18%) ⬆️
socketio/src/lib.rs 100.00% <100.00%> (ø)
... and 6 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@SSebo SSebo force-pushed the reconnect_refactor branch 2 times, most recently from ed777c2 to ef3f9e0 Compare August 19, 2022 09:36
@SSebo SSebo changed the title feat: reconnect feat: reconnect refactor Aug 19, 2022
@SSebo SSebo force-pushed the reconnect_refactor branch 3 times, most recently from 21c430d to d82d8f0 Compare August 19, 2022 09:47
@1c3t3a 1c3t3a mentioned this pull request Aug 19, 2022
4 tasks
Copy link
Owner

@1c3t3a 1c3t3a left a comment

Choose a reason for hiding this comment

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

This looks pretty good, only nits and a few minor things! Thanks for going the extra mile and adding such thorough tests, that is really good to have. The wrapper is a much cleaner solution as the one proposed in #217, thanks a lot for reacting to the feedback!

engineio/src/socket.rs Outdated Show resolved Hide resolved
socketio/examples/callback.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
@SSebo SSebo force-pushed the reconnect_refactor branch 2 times, most recently from aabbcd4 to 81ceda6 Compare August 19, 2022 12:25
@SSebo
Copy link
Contributor Author

SSebo commented Aug 19, 2022

Test case is stable now by checking Open and Close callback.

@SSebo SSebo force-pushed the reconnect_refactor branch 4 times, most recently from c6864d7 to 4d5f4d0 Compare August 19, 2022 14:15
Copy link
Collaborator

@ctrlaltf24 ctrlaltf24 left a comment

Choose a reason for hiding this comment

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

Couple blockers mostly related to maintaining public APIs. If you want to learn highly recommend this (for some reason hard to find) rust book on maintaining public APIs. The actual implementation looks much cleaner! Thanks!

socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Outdated Show resolved Hide resolved
socketio/src/client/builder.rs Show resolved Hide resolved
socketio/src/client/builder.rs Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved
socketio/src/socket.rs Outdated Show resolved Hide resolved
@SSebo SSebo force-pushed the reconnect_refactor branch 7 times, most recently from 698f76f to c8de969 Compare August 20, 2022 03:21
@SSebo
Copy link
Contributor Author

SSebo commented Aug 25, 2022

solution three: rename ReconnctingClient to Client (and take it's place), and rename existing client to InternalClient

@nshaaban-cPacket I tried, found that InternalClient is hard to hide from user because of the callback parameter. Compiler told me that trait is not object safe if I move emit function with generic type in.

Copy link
Owner

@1c3t3a 1c3t3a left a comment

Choose a reason for hiding this comment

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

solution three: rename ReconnctingClient to Client (and take it's place), and rename existing client to InternalClient

@nshaaban-cPacket I tried, found that InternalClient is hard to hide from user because of the callback parameter. Compiler told me that trait is not object safe if I move emit function with generic type in.

Could you share the compiler message? Would be interesting to see why.

socketio/src/client/reconnect.rs Outdated Show resolved Hide resolved

fn reconnect(&mut self) -> Result<()> {
let mut reconnect_attempts = 0;
if self.builder.reconnect {
Copy link
Owner

Choose a reason for hiding this comment

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

I agree with you @nshaaban-cPacket, this makes sense. Changing the Clients default reconnection behavior is a breaking change anyways, so it would make sense to release a new breaking version and be transparent about what has changed.

engineio/src/socket.rs Outdated Show resolved Hide resolved
socketio/examples/callback.rs Outdated Show resolved Hide resolved
@SSebo
Copy link
Contributor Author

SSebo commented Aug 25, 2022

@1c3t3a Compiler does not complain until I create a trait object Box<dyn ClientTrait>:

--> socketio/src/client/reconnect.rs:40:8
|
14 | pub trait ClientTrait {
| ----------- this trait cannot be made into an object...
...
40 | fn emit<E, D>(&self, event: E, data: D) -> Result<()>
| ^^^^ ...because method emit has generic type parameters
...
80 | fn emit_with_ack<F, E, D>(
| ^^^^^^^^^^^^^ ...because method emit_with_ack has generic type parameters
= help: consider moving emit to another trait
= help: consider moving emit_with_ack to another trait

For more information about this error, try rustc --explain E0038.
error: could not compile rust_socketio due to previous error

@ctrlaltf24
Copy link
Collaborator

If this is a breaking change anyways, we could expose BOTH ReconnectingClient and Client in the public interface

@1c3t3a
Copy link
Owner

1c3t3a commented Aug 27, 2022

@1c3t3a Compiler does not complain until I create a trait object Box<dyn ClientTrait>:

--> socketio/src/client/reconnect.rs:40:8 | 14 | pub trait ClientTrait { | ----------- this trait cannot be made into an object... ... 40 | fn emit<E, D>(&self, event: E, data: D) -> Result<()> | ^^^^ ...because method emit has generic type parameters ... 80 | fn emit_with_ack<F, E, D>( | ^^^^^^^^^^^^^ ...because method emit_with_ack has generic type parameters = help: consider moving emit to another trait = help: consider moving emit_with_ack to another trait

For more information about this error, try rustc --explain E0038. error: could not compile rust_socketio due to previous error

What is your definition of ClientTrait, and why do we necessarily need it? Anyways I agree with what @nshaaban-cPacket mentions and expose both versions of the client.

@SSebo
Copy link
Contributor Author

SSebo commented Aug 29, 2022

@1c3t3a Compiler does not complain until I create a trait object Box<dyn ClientTrait>:
--> socketio/src/client/reconnect.rs:40:8 | 14 | pub trait ClientTrait { | ----------- this trait cannot be made into an object... ... 40 | fn emit<E, D>(&self, event: E, data: D) -> Result<()> | ^^^^ ...because method emit has generic type parameters ... 80 | fn emit_with_ack<F, E, D>( | ^^^^^^^^^^^^^ ...because method emit_with_ack has generic type parameters = help: consider moving emit to another trait = help: consider moving emit_with_ack to another trait
For more information about this error, try rustc --explain E0038. error: could not compile rust_socketio due to previous error

What is your definition of ClientTrait, and why do we necessarily need it? Anyways I agree with what @nshaaban-cPacket mentions and expose both versions of the client.

@1c3t3a If we expose both versions of client,we do not need this.

@milandekruijf
Copy link

I've been testing this for a while now and it works great. I did notice that reconnects don't happen when losing internet connection, it just simply does nothing and stops emitting events.

@SSebo
Copy link
Contributor Author

SSebo commented Sep 13, 2022

I've been testing this for a while now and it works great. I did notice that reconnects don't happen when losing internet connection, it just simply does nothing and stops emitting events.

@milandekruijf @1c3t3a @nshaaban-cPacket I found the js version of engine.io client will close connection if ping time out. https://github.com/socketio/engine.io-client/blob/dfee8ded722a8c4f9f773505d0c77b4561569863/lib/socket.ts#L645. Suggest @milandekruijf to open a new issue, because the change will be in engine.io layer

@TomieAi
Copy link

TomieAi commented Oct 11, 2022

when this feature gonna be available? pls

@ctrlaltf24
Copy link
Collaborator

ctrlaltf24 commented Oct 11, 2022

What's left to do here:

  • expose reconnecting client as Client in external API (returned from connect function on the ClientBuilder)
  • expose Client as RawClient (or something along those lines) in public API
  • (optional) expose second function on builder called connect_raw or something along those lines.
  • bump MINOR version
  • MERGE!

@SSebo

(Listen to @1c3t3a over me if he says anything different)

@1c3t3a
Copy link
Owner

1c3t3a commented Oct 14, 2022

Sounds good! From my experience it was never bad advice to listen to @nshaaban-cPacket :) I would accept any PR that fixes the existing questions.

@SSebo
Copy link
Contributor Author

SSebo commented Oct 15, 2022

@1c3t3a @nshaaban-cPacket PTAL

Copy link
Owner

@1c3t3a 1c3t3a left a comment

Choose a reason for hiding this comment

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

LGTM % comment :)

socketio/examples/callback.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@ctrlaltf24 ctrlaltf24 left a comment

Choose a reason for hiding this comment

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

Blocked on removed doc comments from public interface

socketio/src/client/client.rs Outdated Show resolved Hide resolved
socketio/src/client/client.rs Outdated Show resolved Hide resolved
Comment on lines +128 to +129
pub(crate) struct Iter {
socket: Arc<RwLock<RawClient>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

(will let it go because it's an internal component)
suggestion: shouldn't need locking here. Should be able to take a ref of Client, and use a poll method (that handles reconnects) similar to before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RawClient is guarded by RwLock in Client, Can not just ref Client without hold the guard, when guard dropped, the ref is invlaid.

socketio/src/client/client.rs Outdated Show resolved Hide resolved
socketio/src/client/raw_client.rs Outdated Show resolved Hide resolved
socketio/src/client/client.rs Outdated Show resolved Hide resolved
socketio/src/client/client.rs Outdated Show resolved Hide resolved
Copy link
Owner

@1c3t3a 1c3t3a left a comment

Choose a reason for hiding this comment

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

Thanks a lot @SSebo for the feature! Appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants