Skip to content

Commit

Permalink
feat(requests): Add custom request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Mar 30, 2020
1 parent 6c29944 commit eb3adc7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions i18n/bot/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
}
},
"interactiveConfig": {
"useWeb": "Please use the {{{ configCmd }}} command or our website at {{{ link }}} to configure your bot.",
"add": "Enter a value or list of values to add.",
"change": "**Current value**\n{{{ current }}}\n\n**Possible values**\n{{{ possible }}}",
"current": {
Expand Down
47 changes: 47 additions & 0 deletions src/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IMClient } from './client';

// tslint:disable-next-line: variable-name
const RequestHandler = require('eris/lib/rest/RequestHandler');

export interface RequestStat {
total: number;
succeeded: number;
errors: number;
}

export class IMRequestHandler extends RequestHandler {
public requestStats: Map<string, RequestStat> = new Map();

public constructor(client: IMClient, forceQueueing?: boolean) {
super(client, forceQueueing);
}

public request(method: string, url: string, auth: boolean, body: any, file: any, _route: string, short: string) {
// This is similar to https://github.com/abalabahaha/eris/blob/master/lib/rest/RequestHandler.js#L46
// but we don't actually care about rate limits, so no exceptions in grouping
const route = url
.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, (_, p) => `/${p}/:id`)
.replace(/\/reactions\/[^/]+/g, '/reactions/:id')
.replace(/^\/webhooks\/(\d+)\/[A-Za-z0-9-_]{64,}/, '/webhooks/$1/:token');

const statKey = `${method}:${route}`;
let info = this.requestStats.get(statKey);
if (!info) {
info = { total: 0, succeeded: 0, errors: 0 };
this.requestStats.set(statKey, info);
}

info.total++;

return super
.request(method, url, auth, body, file, _route, short)
.then((res: any) => {
info.succeeded++;
return res;
})
.catch((err: any) => {
info.errors++;
throw err;
});
}
}
7 changes: 4 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { StrikesCache } from './moderation/cache/StrikesCache';
import { ModerationService } from './moderation/services/Moderation';
import { MusicCache } from './music/cache/MusicCache';
import { MusicService } from './music/services/MusicService';
import { IMRequestHandler } from './RequestHandler';
import { botDefaultSettings, BotSettingsObject, guildDefaultSettings } from './settings';
import { BotType, ChannelType, LavaPlayerManager } from './types';

Expand Down Expand Up @@ -108,6 +109,7 @@ export class IMClient extends Client {
public shardId: number;
public shardCount: number;

public requestHandler: IMRequestHandler;
public service: ClientServiceObject;
private startingServices: IMService[];
public cache: ClientCacheObject;
Expand Down Expand Up @@ -137,7 +139,6 @@ export class IMClient extends Client {
wsErrors: number;
cmdProcessed: number;
cmdErrors: number;
cmdHttpErrors: Map<number, number>;
};

public disabledGuilds: Set<string> = new Set();
Expand Down Expand Up @@ -168,10 +169,10 @@ export class IMClient extends Client {
wsWarnings: 0,
wsErrors: 0,
cmdProcessed: 0,
cmdErrors: 0,
cmdHttpErrors: new Map()
cmdErrors: 0
};

this.requestHandler = new IMRequestHandler(this);
this.version = version;
this.type = type;
this.instance = instance;
Expand Down
5 changes: 0 additions & 5 deletions src/framework/services/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,6 @@ export class CommandsService extends IMService {

if (error) {
this.client.stats.cmdErrors++;
if (error.code) {
const num = this.client.stats.cmdHttpErrors.get(error.code) || 0;
this.client.stats.cmdHttpErrors.set(error.code, num + 1);
}

console.error(error);

withScope((scope) => {
Expand Down
6 changes: 3 additions & 3 deletions src/framework/services/RabbitMq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class RabbitMqService extends IMService {

public async sendToManager(message: { id: string; [x: string]: any }, isResend: boolean = false) {
if (!this.conn) {
console.log('Send message to RabbitMQ', message);
console.log('Send message to RabbitMQ', JSON.stringify(message, null, 2));
return;
}

Expand Down Expand Up @@ -499,15 +499,15 @@ export class RabbitMqService extends IMService {
};
}
private getMetrics() {
const req = (this.client as any).requestHandler;
const req = this.client.requestHandler;

return {
wsEvents: this.client.stats.wsEvents,
wsWarnings: this.client.stats.wsWarnings,
wsErrors: this.client.stats.wsErrors,
cmdProcessed: this.client.stats.cmdProcessed,
cmdErrors: this.client.stats.cmdErrors,
cmdHttpErrors: [...this.client.stats.cmdHttpErrors.entries()].map(([code, count]) => ({ code, count })),
httpRequests: [...req.requestStats.entries()].map(([url, stats]) => ({ url, stats })),
httpRequestsQueued: Object.keys(req.ratelimits)
.filter((endpoint) => req.ratelimits[endpoint]._queue.length > 0)
.reduce((acc, endpoint) => acc.concat([{ endpoint, count: req.ratelimits[endpoint]._queue.length }]), [])
Expand Down

0 comments on commit eb3adc7

Please sign in to comment.