mirror of
https://git.sr.ht/~emersion/gamja
synced 2024-11-27 10:05:37 -05:00
lib/client: don't throttle reconnections if opened long ago
If a connection was opened a long time ago, and recently got broken, try to reconnect immediately.
This commit is contained in:
parent
414d93ebab
commit
721ae1d2ae
1 changed files with 10 additions and 4 deletions
|
@ -74,8 +74,8 @@ class IRCError extends Error {
|
|||
class Backoff {
|
||||
n = 0;
|
||||
|
||||
constructor(min, max) {
|
||||
this.min = min;
|
||||
constructor(base, max) {
|
||||
this.base = base;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
|
@ -86,10 +86,10 @@ class Backoff {
|
|||
next() {
|
||||
if (this.n === 0) {
|
||||
this.n = 1;
|
||||
return this.min;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let dur = this.n * this.min;
|
||||
let dur = this.n * this.base;
|
||||
if (dur > this.max) {
|
||||
dur = this.max;
|
||||
} else {
|
||||
|
@ -134,6 +134,7 @@ export default class Client extends EventTarget {
|
|||
autoReconnect = true;
|
||||
reconnectTimeoutID = null;
|
||||
reconnectBackoff = new Backoff(RECONNECT_MIN_DELAY_MSEC, RECONNECT_MAX_DELAY_MSEC);
|
||||
lastReconnectDate = new Date(0);
|
||||
pingIntervalID = null;
|
||||
pendingCmds = {
|
||||
WHO: Promise.resolve(null),
|
||||
|
@ -159,6 +160,7 @@ export default class Client extends EventTarget {
|
|||
|
||||
console.log("Connecting to " + this.params.url);
|
||||
this.setStatus(Client.Status.CONNECTING);
|
||||
this.lastReconnectDate = new Date();
|
||||
|
||||
try {
|
||||
this.ws = new WebSocket(this.params.url);
|
||||
|
@ -207,6 +209,10 @@ export default class Client extends EventTarget {
|
|||
console.info("Waiting for network to go back online");
|
||||
} else {
|
||||
let delay = this.reconnectBackoff.next();
|
||||
let sinceLastReconnect = new Date().getTime() - this.lastReconnectDate.getTime();
|
||||
if (sinceLastReconnect < RECONNECT_MIN_DELAY_MSEC) {
|
||||
delay = Math.max(delay, RECONNECT_MIN_DELAY_MSEC);
|
||||
}
|
||||
console.info("Reconnecting to server in " + (delay / 1000) + " seconds");
|
||||
clearTimeout(this.reconnectTimeoutID);
|
||||
this.reconnectTimeoutID = setTimeout(() => {
|
||||
|
|
Loading…
Reference in a new issue