kiwiirc / irc-framework

🛠️ A better IRC framework for node.js. For bots and full clients.
MIT License
181 stars 64 forks source link

Add exponential reconnection backoff, solves #140 #248

Closed Frotty closed 4 years ago

Frotty commented 4 years ago

See https://github.com/kiwiirc/irc-framework/issues/140

prawnsalad commented 4 years ago

That jitter value should be much larger.. probably in the range of 1-5 seconds even. Then it should also have a max timeout length so the timeout doesn't get insanely huge. Something like max of 5 minutes sounds reasonable IMO.

Frotty commented 4 years ago

@prawnsalad Agreed on the jitter. For the maximum I would prefer a higher one, since my actual use case was getting g-lined from quakenet, which takes about an hour to clear up.

xPaw commented 4 years ago

Looks good, will need to give it some testing.

Frotty commented 4 years ago

@prawnsalad Anything left to do here?

prawnsalad commented 4 years ago

After trying this out the reconnect timeouts (reconnect_attempts++ between each call) gives:

13657
14954
19766
37475
68025
135704
260587

That's a bit high and clustered at the beginning so I tweaked it to:

function calculateExponentialBackoff() {
    const jitter = 1000 + Math.floor(Math.random() * 5000);
    const attempts = Math.min(reconnect_attempts, 30);
    const time = 1000 * Math.pow(2, attempts);
    return Math.min(time, auto_reconnect_max_wait) + jitter;
}

Which gives a better result:

3077
7235
8395
11505
21760
36047
65086

This has also removed the redundant auto_reconnect_wait option.

xPaw commented 4 years ago

@Frotty are you able to rebase/squash this and apply the tweak above?

prawnsalad commented 4 years ago

Nice, will get to testing it tonight/tomorrow ++