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

Full support for websocket reconnection/resubscription #3135

Merged

Conversation

nivida
Copy link
Contributor

@nivida nivida commented Oct 16, 2019

Proposed the PR from @gabmontes again. Because the 1.0 branch is no longer existing is not possible to re-open the already existing PR #1966.

@nivida nivida added Enhancement Includes improvements or optimizations 1.x 1.0 related issues labels Oct 16, 2019
@nivida nivida requested a review from cgewecke October 16, 2019 12:09
@coveralls
Copy link

coveralls commented Oct 16, 2019

Coverage Status

Coverage decreased (-0.2%) to 83.815% when pulling 66f7f74 on bloq:reconnect-resubscribe into 9dd09db on ethereum:1.x.

@nivida
Copy link
Contributor Author

nivida commented Oct 17, 2019

@gabmontes I'm not able to push any commits because this fork is located in an organization. Can you resolve these conflicts for me?

Copy link
Collaborator

@cgewecke cgewecke left a comment

Choose a reason for hiding this comment

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

There's a lot of code here, no tests, and a meaningful drop in coverage.

Perhaps we could merge this into a temporary branch at Web3 and work up some tests which describe how all of it works. And schedule for 1.2.3 instead of 1.2.2?

@gabmontes gabmontes force-pushed the reconnect-resubscribe branch from 8103d28 to 2547b68 Compare October 22, 2019 12:22
@gabmontes
Copy link
Contributor

@nivida conflicts solved!

@TrejGun
Copy link

TrejGun commented Oct 24, 2019

guys can we please have this already?

@cgewecke
Copy link
Collaborator

@nivida Can we merge this into a temporary branch and I will write some tests for it?

When using `.on<event>=fn` to attach listeners, only one listener can be
set at the same time. Since multiple request managers can use the same
provider, the EventTarget API has to be used to ensure all of them
receive the events emitted from the provider.

This is needed on both the `on` and `removeListener` functions.
The method `once` is required to allow the subscription logic to
identify if the provider is able to reconnect/resubscribe and then
attach to the following `connect` event the function to resubscribe.
When the subscription fails on start and when it fails after it was
successfully established, the same logic needs to be executed: remove
subscription, listen for the next `connect` event if available to
actually subscribe again, emit the error and call the callback.

Prior code did that only for established subscriptions so if a
subscription was unable to be set right on start, no resubscription was
ever tried.

The logic was moved to a single method to avoid duplication of code.
In addition reentry is avoided by checking and properly clearing the
`_reconnectIntervalId` variable.
On subscribe, if there is an existing `id`, the subscription listeners
are removed. In the case of a resubscription, the listeners have to be
kept. Therefore, the `id` property -that will change anyway- must be
cleared so the listeners are not removed.

Then, after the subscription object resubscribes, the listeners set by
the subscription user code remain untouched, making the resubscription
transparent to the user code.
When the request manager removes a subscription due to an error, it
tries to send an unsubscribe package, which can also fail if i.e. the
network is down. In such a case, the function must not allow reentry.
Removing the subscription first ensures it will not do so.

In addition, if the subscription was already removed, the callback shall
be called anyway.
When error events are emitted by the provider, all subscriptions shall
receive the event and trigger the unsubscription/resubscription logic.
By wrapping the available WebSocket implementation (native WebSocket
object or `websocket` package) with `websocket-reconnector`, the
provider is given a WebSocket that will automatically reconnect on
errors.

A new option was added to the WebSocket provider to controll whether it
should automatically reconnect or it should behave as usual.
In the case any websocket call takes too long to return and a timeout
was set for the provider to timeout, the provider should try to restart
the connection.

This could happen, for instance, if the client loses connection with the
server, the server closes the connection and later, the connectivity is
up again but since the client did not receive the closing frame *and*
the client does not attempt to send any package to the server, no error
is observed.

`websocket` implementation for Node.js has an option to send keep-alive
frames and detect such scenarios, but the standard browser W3C WebSocket
does not, so it is "vulnerable" to this kind of failure which will
mostly affect web3 subscriptions.
If the provider silently recoonects and emits a new "connect" event, the subscriptions have to be set again over that new connection.
@gabmontes gabmontes force-pushed the reconnect-resubscribe branch from 2547b68 to 66f7f74 Compare October 27, 2019 20:59
@cgewecke cgewecke changed the base branch from 1.x to staging/reconnect-resubscribe October 29, 2019 01:20
@cgewecke
Copy link
Collaborator

@nivida Merging into a staging branch and will re-open as a draft while I write some tests.

@gabmontes Thanks for fixing the conflicts (and for writing this!)

@cgewecke cgewecke merged commit 5c5e95d into web3:staging/reconnect-resubscribe Oct 29, 2019
@TrejGun
Copy link

TrejGun commented Oct 29, 2019

thanks you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Enhancement Includes improvements or optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants