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 #3167

Closed
wants to merge 12 commits into from

Commits on Nov 6, 2019

  1. Attach ws event listeners using EventTarget API

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    57be867 View commit details
    Browse the repository at this point in the history
  2. Add once to the WS provider interface

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    7c8451c View commit details
    Browse the repository at this point in the history
  3. Merge logic for resubscription

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    53a7d36 View commit details
    Browse the repository at this point in the history
  4. Clear subscription id before resubscribing

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    666f087 View commit details
    Browse the repository at this point in the history
  5. Avoid reentry when removing subscriptions

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    9c366e4 View commit details
    Browse the repository at this point in the history
  6. Broadcast provider error to subscribers

    When error events are emitted by the provider, all subscriptions shall
    receive the event and trigger the unsubscription/resubscription logic.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    f35fc88 View commit details
    Browse the repository at this point in the history
  7. Add support for WebSocket reconnections

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    4af2f2b View commit details
    Browse the repository at this point in the history
  8. Try to reconnect on timeout too

    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.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    fbba26e View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    5f5c2b8 View commit details
    Browse the repository at this point in the history
  10. Ensure resubscription is done on silent reconnects

    If the provider silently recoonects and emits a new "connect" event, the subscriptions have to be set again over that new connection.
    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    6305262 View commit details
    Browse the repository at this point in the history
  11. Fix indentation

    gabmontes authored and cgewecke committed Nov 6, 2019
    Configuration menu
    Copy the full SHA
    ed96084 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    d31f7e2 View commit details
    Browse the repository at this point in the history