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

Rewrite WebSocket internals #1410

Merged
merged 31 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bceda3f
Start rewriting Manager and Connection
amishshah Apr 23, 2017
388b4c6
more stuff
amishshah Apr 23, 2017
740cde0
stuff
amishshah Apr 23, 2017
bf97848
Fix ready bug
amishshah Apr 23, 2017
e12ce4f
some stuff i forgot
amishshah Apr 24, 2017
2d6daef
Merge branch 'master' into wsrewrite
amishshah Apr 24, 2017
630cae7
fix some stuff
amishshah Apr 24, 2017
c3e1e02
add stupid heartbeat ack like seriously who cares
amishshah Apr 24, 2017
7a4a3e2
woo!
amishshah Apr 24, 2017
a75d2ef
fix a bug
amishshah Apr 24, 2017
65da634
rate limit the dumb websocket
amishshah Apr 24, 2017
a2d5bec
stuff
amishshah Apr 24, 2017
4025fd7
hdocs
amishshah Apr 24, 2017
f3bcb47
Docs
amishshah Apr 24, 2017
579e242
Remove ClientManager#setupKeepAlive as it is now redundant
amishshah Apr 24, 2017
4bee16b
Change Client._pingTimestamp to a getter that fetches the timestamp f…
amishshah Apr 24, 2017
872cd8c
are you happy now eslint smh
amishshah Apr 24, 2017
df12e4b
make gus happy
amishshah Apr 24, 2017
b621053
Add CloseEvent external doc
amishshah Apr 24, 2017
142183b
Make sure to emit 'reconnecting' when actually reconnecting
amishshah Apr 25, 2017
e644c5e
ffs
amishshah Apr 25, 2017
1e6c2ff
Merge branch 'master' into wsrewrite
amishshah Apr 25, 2017
3348310
Fix RESUME logic
amishshah Apr 25, 2017
1e4abe0
Add heartbeat ack debug messages, including latency data
amishshah Apr 25, 2017
234b2fc
Merge branch 'master' into wsrewrite
amishshah Apr 26, 2017
e7db3c5
Dumb stuff for Gus
amishshah Apr 26, 2017
2961402
thx eslint
amishshah Apr 26, 2017
8d1661d
more dumb stuff
amishshah Apr 26, 2017
ce4ef50
more dumb crap smh gus i h8 u
amishshah Apr 26, 2017
aec0ec3
moar messages
amishshah Apr 26, 2017
c61b66a
fix for using wrong status, causing certain events not to be fired (#…
Drahcirius Apr 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ class Client extends EventEmitter {
*/
this.pings = [];

/**
* Timestamp of the latest ping's start time
* @type {number}
* @private
*/
this._pingTimestamp = 0;

/**
* Timeouts set by {@link Client#setTimeout} that are still active
* @type {Set<Timeout>}
Expand All @@ -182,6 +175,15 @@ class Client extends EventEmitter {
}
}

/**
* Timestamp of the latest ping's start time
* @type {number}
* @private
*/
get _pingTimestamp() {
return this.ws.connection ? this.ws.connection.lastPingTimestamp : 0;
}

/**
* Current status of the client's connection to Discord
* @type {?number}
Expand Down
2 changes: 1 addition & 1 deletion src/client/ClientDataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ClientDataManager {
}

get pastReady() {
return this.client.ws.status === Constants.Status.READY;
return this.client.ws.connection.status === Constants.Status.READY;
}

newGuild(data) {
Expand Down
18 changes: 9 additions & 9 deletions src/client/ClientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class ClientManager {
this.heartbeatInterval = null;
}

/**
* The status of the client
* @type {number}
*/
get status() {
return this.connection ? this.connection.status : Constants.Status.IDLE;
}

/**
* Connects the Client to the WebSocket
* @param {string} token The authorization token
Expand Down Expand Up @@ -47,17 +55,9 @@ class ClientManager {
}, reject);
}

/**
* Sets up a keep-alive interval to keep the Client's connection valid
* @param {number} time The interval in milliseconds at which heartbeat packets should be sent
*/
setupKeepAlive(time) {
this.heartbeatInterval = time;
this.client.setInterval(() => this.client.ws.heartbeat(true), time);
}

destroy() {
this.client.ws.destroy();
this.client.rest.destroy();
if (!this.client.user) return Promise.resolve();
if (this.client.user.bot) {
this.client.token = null;
Expand Down
6 changes: 6 additions & 0 deletions src/client/rest/RESTManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class RESTManager {
this.globallyRateLimited = false;
}

destroy() {
for (const handlerID in this.handlers) {
this.handlers[handlerID].destroy();
}
}

push(handler, apiRequest) {
return new Promise((resolve, reject) => {
handler.push({
Expand Down
4 changes: 4 additions & 0 deletions src/client/rest/RequestHandlers/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class RequestHandler {
* Attempts to get this RequestHandler to process its current queue
*/
handle() {} // eslint-disable-line no-empty-function

destroy() {
this.queue = [];
}
}

module.exports = RequestHandler;
59 changes: 31 additions & 28 deletions src/client/rest/RequestHandlers/Sequential.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,42 @@ class SequentialRequestHandler extends RequestHandler {
execute(item) {
this.busy = true;
return new Promise(resolve => {
item.request.gen().end((err, res) => {
if (res && res.headers) {
this.requestLimit = Number(res.headers['x-ratelimit-limit']);
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
}
if (err) {
if (err.status === 429) {
this.queue.unshift(item);
this.restManager.client.setTimeout(() => {
this.globalLimit = false;
resolve();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else {
item.reject(err);
resolve(err);
item.request
.gen()
.on('error', e => item.reject(e))
.end((err, res) => {
if (res && res.headers) {
this.requestLimit = Number(res.headers['x-ratelimit-limit']);
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
}
} else {
this.globalLimit = false;
const data = res && res.body ? res.body : {};
item.resolve(data);
if (this.requestRemaining === 0) {
this.restManager.client.setTimeout(
if (err) {
if (err.status === 429) {
this.queue.unshift(item);
this.restManager.client.setTimeout(() => {
this.globalLimit = false;
resolve();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else {
item.reject(err);
resolve(err);
}
} else {
this.globalLimit = false;
const data = res && res.body ? res.body : {};
item.resolve(data);
if (this.requestRemaining === 0) {
this.restManager.client.setTimeout(
() => resolve(data),
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
);
} else {
resolve(data);
} else {
resolve(data);
}
}
}
});
});
});
}

Expand Down
Loading