Skip to content

WS reconnect backoff function #22

@DoganM95

Description

@DoganM95

Hi, i currently listen to balance changes like this:

        const balanceWsClient = new WsClient({
            url: wsUrl,
            maxReconnectAttempts: 10 * 60 * 1000,
            reconnectInterval: 1,
            onOpen: () => {
                // Subscribe, etc

And my reconnectInterval of 1ms is obviously very tight, but i need the Websockets to get back up as soon as possible always.
The 1 ms is fine most of the cases and dthe WS reconnects most of the times as expected, but every once in a while it doesn't, so the program exits.

My proposal would be to add another optional parameter to the WsClient, named e.g. reconnectBackoffStrategy or something, whcih takes a function definition it uses to calculate the delay until the next reconnect.

e.g. passing it like this

const balanceWsClient = new WsClient({
    url: wsUrl,
    maxReconnectAttempts: 10 * 60 * 1000,
    reconnectInterval: 1,
    reconnectBackoffStrategy: reconnectPolicy,
    onOpen: () => {
        // Subscribe, etc

with the function definition like this:

const reconnectPolicy = (currentDelay) => {
    return currentDelay * 2;
};

This would allow e.g. an exponential backoff, so the websocket does not retry every e.g. 1ms in my case, but like this:
1ms -> 2ms -> 4ms -> ... -> 1073741824ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions