Skip to content

Commit

Permalink
Add reconnect delay customization (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesamsel authored and abalabahaha committed Oct 31, 2019
1 parent 6be78bf commit e16e645
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Expand Up @@ -445,6 +445,7 @@ declare namespace Eris {
}
type PossiblyUncachedMessage = Message | { id: string, channel: TextableChannel };
interface RawPacket { op: number; t?: string; d?: any; s?: number; }
type ReconnectDelayFunction = (lastDelay: number, attempts: number) => number;
interface ClientOptions {
autoreconnect?: boolean;
compress?: boolean;
Expand All @@ -465,7 +466,9 @@ declare namespace Eris {
defaultImageSize?: number;
ws?: any;
latencyThreshold?: number;
agent?: HTTPSAgent
agent?: HTTPSAgent;
reconnectAttempts: number;
reconnectDelay: ReconnectDelayFunction;
}
interface CommandClientOptions {
defaultHelpCommand?: boolean;
Expand Down
14 changes: 11 additions & 3 deletions lib/Client.js
Expand Up @@ -98,6 +98,8 @@ class Client extends EventEmitter {
* @arg {Number} [options.defaultImageSize=128] The default size to return user avatars, guild icons, banners, splashes, and group icons. Can be any power of two between 16 and 2048. If the height and width are different, the width will be the value specified, and the height relative to that
* @arg {Object} [options.ws] An object of WebSocket options to pass to the shard WebSocket constructors
* @arg {Object} [options.agent] A HTTP Agent used to proxy requests
* @arg {Number} [options.maxReconnectAttempts=Infinity] The maximum amount of times that the client is allowed to try to reconnect to Discord.
* @arg {Function} [options.reconnectDelay] A function which returns how long the bot should wait until reconnecting to Discord.
*/
constructor(token, options) {
super();
Expand All @@ -124,7 +126,9 @@ class Client extends EventEmitter {
restMode: false,
seedVoiceConnections: false,
ws: {},
agent: null
agent: null,
maxReconnectAttempts: Infinity,
reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000
}, options);
if(this.options.lastShardID === undefined && this.options.maxShards !== "auto") {
this.options.lastShardID = this.options.maxShards - 1;
Expand Down Expand Up @@ -177,6 +181,8 @@ class Client extends EventEmitter {
this.voiceConnections = new VoiceConnectionManager();

this.connect = this.connect.bind(this);
this.lastReconnectDelay = 0;
this.reconnectAttempts = 0;
}

get uptime() {
Expand Down Expand Up @@ -222,8 +228,10 @@ class Client extends EventEmitter {
if(!this.options.autoreconnect) {
throw err;
}
this.emit("error", err);
await sleep(2000);
const reconnectDelay = this.options.reconnectDelay(this.lastReconnectDelay, this.reconnectAttempts);
await sleep(reconnectDelay);
this.lastReconnectDelay = reconnectDelay;
this.reconnectAttempts = this.reconnectAttempts + 1;
return this.connect();
}
}
Expand Down

0 comments on commit e16e645

Please sign in to comment.