Skip to content

Commit

Permalink
fix(rl): shared rl and retryAfter behaviour, header getter on httperr…
Browse files Browse the repository at this point in the history
…or (#1344)

Co-authored-by: Bsian <[email protected]>
  • Loading branch information
curtisf and bsian03 authored Jun 1, 2022
1 parent d6df49a commit 2712a85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from "events";
import { Duplex, Readable as ReadableStream, Stream } from "stream";
import { Agent as HTTPSAgent } from "https";
import { IncomingMessage, ClientRequest } from "http";
import { IncomingMessage, ClientRequest, IncomingHttpHeaders } from "http";
import OpusScript = require("opusscript"); // Thanks TypeScript
import { URL } from "url";
import { Socket as DgramSocket } from "dgram";
Expand Down Expand Up @@ -2577,6 +2577,7 @@ declare namespace Eris {

export class DiscordHTTPError extends Error {
code: number;
headers: IncomingHttpHeaders;
name: "DiscordHTTPError";
req: ClientRequest;
res: IncomingMessage;
Expand All @@ -2587,6 +2588,7 @@ declare namespace Eris {

export class DiscordRESTError extends Error {
code: number;
headers: IncomingHttpHeaders;
name: string;
req: ClientRequest;
res: IncomingMessage;
Expand Down
4 changes: 4 additions & 0 deletions lib/errors/DiscordHTTPError.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class DiscordHTTPError extends Error {
}
}

get headers() {
return this.response.headers;
}

get name() {
return this.constructor.name;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/errors/DiscordRESTError.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class DiscordRESTError extends Error {
}
}

get headers() {
return this.response.headers;
}

get name() {
return `${this.constructor.name} [${this.code}]`;
}
Expand Down
17 changes: 13 additions & 4 deletions lib/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class RequestHandler {

this.ratelimits[route].remaining = resp.headers["x-ratelimit-remaining"] === undefined ? 1 : +resp.headers["x-ratelimit-remaining"] || 0;

const retryAfter = parseInt(resp.headers["x-ratelimit-reset-after"] || resp.headers["retry-after"]) * 1000;
const retryAfter = Number(resp.headers["x-ratelimit-reset-after"] || resp.headers["retry-after"]) * 1000;
if(retryAfter >= 0) {
if(resp.headers["x-ratelimit-global"]) {
this.globalBlock = true;
Expand All @@ -299,12 +299,21 @@ class RequestHandler {
if(resp.statusCode >= 300) {
if(resp.statusCode === 429) {
const content = typeof body === "object" ? `${body.content} ` : "";
this._client.emit("debug", `${resp.headers["x-ratelimit-global"] ? "Global" : "Unexpected"} 429 (╯°□°)╯︵ ┻━┻: ${response}\n${content} ${now} ${route} ${resp.statusCode}: ${latency}ms (${this.latencyRef.latency}ms avg) | ${this.ratelimits[route].remaining}/${this.ratelimits[route].limit} left | Reset ${this.ratelimits[route].reset} (${this.ratelimits[route].reset - now}ms left)`);
if(retryAfter) {
let delay = retryAfter;
if(resp.headers["x-ratelimit-scope"] === "shared") {
try {
delay = JSON.parse(response).retry_after * 1000;
} catch(err) {
reject(err);
return;
}
}
this._client.emit("debug", `${resp.headers["x-ratelimit-global"] ? "Global" : "Unexpected"} 429 (╯°□°)╯︵ ┻━┻: ${response}\n${content} ${now} ${route} ${resp.statusCode}: ${latency}ms (${this.latencyRef.latency}ms avg) | ${this.ratelimits[route].remaining}/${this.ratelimits[route].limit} left | Reset ${delay} (${this.ratelimits[route].reset - now}ms left) | Scope ${resp.headers["x-ratelimit-scope"]}`);
if(delay) {
setTimeout(() => {
cb();
this.request(method, url, auth, body, file, route, true).then(resolve).catch(reject);
}, retryAfter);
}, delay);
return;
} else {
cb();
Expand Down

0 comments on commit 2712a85

Please sign in to comment.