Skip to content

Commit

Permalink
Expose latency in rawREST event (abalabahaha#1017)
Browse files Browse the repository at this point in the history
Co-authored-by: bsian03 <[email protected]>
  • Loading branch information
Thewsomeguy and bsian03 authored Nov 29, 2020
1 parent ad00c9b commit e789deb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class RequestHandler {
let latency = Date.now();

req.once("response", (resp) => {
latency = Date.now() - latency;
this.latencyRef.raw.push(latency);
this.latencyRef.latency = this.latencyRef.latency - ~~(this.latencyRef.raw.shift() / 10) + ~~(latency / 10);

if(this._client.listeners("rawREST").length) {
/**
* Fired when the Client's RequestHandler receives a response
Expand All @@ -166,17 +170,15 @@ class RequestHandler {
* @prop {Object} [request.file] The file object sent in the request
* @prop {Buffer} request.file.file A buffer containing file data
* @prop {String} request.file.name The name of the file
* @prop {Number} request.latency The HTTP response latency
* @prop {String} request.method Uppercase HTTP method
* @prop {IncomingMessage} request.resp The HTTP response to the request
* @prop {String} request.route The calculated ratelimiting route for the request
* @prop {Boolean} request.short Whether or not the request was prioritized in its ratelimiting queue
* @prop {String} request.url URL of the endpoint
*/
this._client.emit("rawREST", {method, url, auth, body, file, route, short, resp});
this._client.emit("rawREST", {method, url, auth, body, file, route, short, resp, latency});
}
latency = Date.now() - latency;
this.latencyRef.raw.push(latency);
this.latencyRef.latency = this.latencyRef.latency - ~~(this.latencyRef.raw.shift() / 10) + ~~(latency / 10);

const headerNow = Date.parse(resp.headers["date"]);
if(this.latencyRef.lastTimeOffsetCheck < Date.now() - 5000) {
Expand Down

0 comments on commit e789deb

Please sign in to comment.