fix: resharder bugs from testing gamer

This commit is contained in:
Skillz4Killz
2022-02-21 05:17:08 +00:00
committed by GitHub
parent 7914ef1d11
commit 0afd928650
5 changed files with 38 additions and 21 deletions
+3
View File
@@ -66,6 +66,7 @@ import { transformVoiceRegion } from "./transformers/voiceRegion.ts";
import { transformWidget } from "./transformers/widget.ts"; import { transformWidget } from "./transformers/widget.ts";
import { transformStageInstance } from "./transformers/stageInstance.ts"; import { transformStageInstance } from "./transformers/stageInstance.ts";
import { transformSticker } from "./transformers/sticker.ts"; import { transformSticker } from "./transformers/sticker.ts";
import { transformGatewayBot } from "./transformers/gatewayBot.ts";
export function createBot(options: CreateBotOptions): Bot { export function createBot(options: CreateBotOptions): Bot {
const bot = { const bot = {
@@ -326,6 +327,7 @@ export function createBaseHelpers(options: Partial<Helpers>) {
export interface Transformers { export interface Transformers {
snowflake: typeof snowflakeToBigint; snowflake: typeof snowflakeToBigint;
gatewayBot: typeof transformGatewayBot;
channel: typeof transformChannel; channel: typeof transformChannel;
guild: typeof transformGuild; guild: typeof transformGuild;
user: typeof transformUser; user: typeof transformUser;
@@ -394,6 +396,7 @@ export function createTransformers(options: Partial<Transformers>) {
widget: options.widget || transformWidget, widget: options.widget || transformWidget,
stageInstance: options.stageInstance || transformStageInstance, stageInstance: options.stageInstance || transformStageInstance,
sticker: options.sticker || transformSticker, sticker: options.sticker || transformSticker,
gatewayBot: options.gatewayBot || transformGatewayBot
}; };
} }
+18 -10
View File
@@ -1,6 +1,6 @@
import { transformGatewayBot } from "../transformers/gatewayBot.ts";
import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts"; import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts";
import { DiscordReady } from "../types/gateway/ready.ts"; import { DiscordReady } from "../types/gateway/ready.ts";
import { Collection } from "../util/collection.ts";
import { createGatewayManager, GatewayManager } from "./gateway_manager.ts"; import { createGatewayManager, GatewayManager } from "./gateway_manager.ts";
/** The handler to automatically reshard when necessary. */ /** The handler to automatically reshard when necessary. */
@@ -12,15 +12,23 @@ export async function resharder(
const gateway = createGatewayManager({ const gateway = createGatewayManager({
...oldGateway, ...oldGateway,
// IGNORE EVENTS FOR NOW
handleDiscordPayload: async function (_, data, shardId) {
if (data.t === "READY") {
const payload = data.d as DiscordReady;
await gateway.resharding.markNewGuildShardId(payload.guilds.map((g) => BigInt(g.id)), shardId);
}
},
}); });
for (const key of Object.keys(oldGateway)) {
if (key === "handleDiscordPayload") {
gateway.handleDiscordPayload = async function (_, data, shardId) {
if (data.t === "READY") {
const payload = data.d as DiscordReady;
await gateway.resharding.markNewGuildShardId(payload.guilds.map((g) => BigInt(g.id)), shardId);
}
};
continue;
}
// USE ANY CUSTOMIZED HANDLERS FROM OLD GATEWAY
if (typeof key === "function") gateway[key] = oldGateway[key];
}
// Begin resharding // Begin resharding
gateway.maxShards = results.shards; gateway.maxShards = results.shards;
// FOR MANUAL SHARD CONTROL, OVERRIDE THIS SHARD ID! // FOR MANUAL SHARD CONTROL, OVERRIDE THIS SHARD ID!
@@ -31,7 +39,7 @@ export async function resharder(
gateway.sessionStartLimitResetAfter = results.sessionStartLimit.resetAfter; gateway.sessionStartLimitResetAfter = results.sessionStartLimit.resetAfter;
gateway.maxConcurrency = results.sessionStartLimit.maxConcurrency; gateway.maxConcurrency = results.sessionStartLimit.maxConcurrency;
// If more than 100K servers, begin switching to 16x sharding // If more than 100K servers, begin switching to 16x sharding
if (gateway.maxShards && gateway.useOptimalLargeBotSharding) { if (gateway.maxShards > 60 && gateway.useOptimalLargeBotSharding) {
gateway.debug("[Resharding] Using optimal large bot sharding solution."); gateway.debug("[Resharding] Using optimal large bot sharding solution.");
gateway.maxShards = Math.ceil( gateway.maxShards = Math.ceil(
gateway.maxShards / gateway.maxShards /
@@ -115,7 +123,7 @@ export async function startReshardingChecks(gateway: GatewayManager) {
headers: { headers: {
Authorization: `Bot ${gateway.token}`, Authorization: `Bot ${gateway.token}`,
}, },
}).then((res) => res.json())) as GetGatewayBot; }).then((res) => res.json()).then((res) => transformGatewayBot(res))) as GetGatewayBot;
const percentage = ((results.shards - gateway.maxShards) / gateway.maxShards) * 100; const percentage = ((results.shards - gateway.maxShards) / gateway.maxShards) * 100;
// Less than necessary% being used so do nothing // Less than necessary% being used so do nothing
+1 -1
View File
@@ -15,7 +15,7 @@ export function prepareBuckets(gateway: GatewayManager, firstShardId: number, la
// ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS // ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS
for (let i = firstShardId; i < lastShardId; i++) { for (let i = firstShardId; i < lastShardId; i++) {
gateway.debug(`1. Running for loop in spawnShards function.`); gateway.debug(`1. Running for loop in spawnShards function for shardId ${i}.`);
if (i >= gateway.maxShards) { if (i >= gateway.maxShards) {
continue; continue;
} }
+1 -10
View File
@@ -5,14 +5,5 @@ import type { Bot } from "../../bot.ts";
export async function getGatewayBot(bot: Bot): Promise<GetGatewayBot> { export async function getGatewayBot(bot: Bot): Promise<GetGatewayBot> {
const result = await bot.rest.runMethod<GetGatewayBot>(bot.rest, "get", bot.constants.endpoints.GATEWAY_BOT); const result = await bot.rest.runMethod<GetGatewayBot>(bot.rest, "get", bot.constants.endpoints.GATEWAY_BOT);
return { return bot.transformers.gatewayBot(result);
url: result.url,
shards: result.shards,
sessionStartLimit: {
total: result.session_start_limit.total,
remaining: result.session_start_limit.remaining,
resetAfter: result.session_start_limit.reset_after,
maxConcurrency: result.session_start_limit.max_concurrency,
},
};
} }
+15
View File
@@ -0,0 +1,15 @@
import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts";
import { SnakeCasedPropertiesDeep } from "../types/util.ts";
export function transformGatewayBot(payload: SnakeCasedPropertiesDeep<GetGatewayBot>): GetGatewayBot {
return {
url: payload.url,
shards: payload.shards,
sessionStartLimit: {
total: payload.session_start_limit.total,
remaining: payload.session_start_limit.remaining,
resetAfter: payload.session_start_limit.reset_after,
maxConcurrency: payload.session_start_limit.max_concurrency,
},
};
}