mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-15 19:08:17 +00:00
fix: resharder bugs from testing gamer
This commit is contained in:
@@ -66,6 +66,7 @@ import { transformVoiceRegion } from "./transformers/voiceRegion.ts";
|
||||
import { transformWidget } from "./transformers/widget.ts";
|
||||
import { transformStageInstance } from "./transformers/stageInstance.ts";
|
||||
import { transformSticker } from "./transformers/sticker.ts";
|
||||
import { transformGatewayBot } from "./transformers/gatewayBot.ts";
|
||||
|
||||
export function createBot(options: CreateBotOptions): Bot {
|
||||
const bot = {
|
||||
@@ -326,6 +327,7 @@ export function createBaseHelpers(options: Partial<Helpers>) {
|
||||
|
||||
export interface Transformers {
|
||||
snowflake: typeof snowflakeToBigint;
|
||||
gatewayBot: typeof transformGatewayBot;
|
||||
channel: typeof transformChannel;
|
||||
guild: typeof transformGuild;
|
||||
user: typeof transformUser;
|
||||
@@ -394,6 +396,7 @@ export function createTransformers(options: Partial<Transformers>) {
|
||||
widget: options.widget || transformWidget,
|
||||
stageInstance: options.stageInstance || transformStageInstance,
|
||||
sticker: options.sticker || transformSticker,
|
||||
gatewayBot: options.gatewayBot || transformGatewayBot
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+15
-7
@@ -1,6 +1,6 @@
|
||||
import { transformGatewayBot } from "../transformers/gatewayBot.ts";
|
||||
import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts";
|
||||
import { DiscordReady } from "../types/gateway/ready.ts";
|
||||
import { Collection } from "../util/collection.ts";
|
||||
import { createGatewayManager, GatewayManager } from "./gateway_manager.ts";
|
||||
|
||||
/** The handler to automatically reshard when necessary. */
|
||||
@@ -12,14 +12,22 @@ export async function resharder(
|
||||
|
||||
const gateway = createGatewayManager({
|
||||
...oldGateway,
|
||||
// IGNORE EVENTS FOR NOW
|
||||
handleDiscordPayload: async function (_, data, 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
|
||||
gateway.maxShards = results.shards;
|
||||
@@ -31,7 +39,7 @@ export async function resharder(
|
||||
gateway.sessionStartLimitResetAfter = results.sessionStartLimit.resetAfter;
|
||||
gateway.maxConcurrency = results.sessionStartLimit.maxConcurrency;
|
||||
// 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.maxShards = Math.ceil(
|
||||
gateway.maxShards /
|
||||
@@ -115,7 +123,7 @@ export async function startReshardingChecks(gateway: GatewayManager) {
|
||||
headers: {
|
||||
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;
|
||||
// Less than necessary% being used so do nothing
|
||||
|
||||
@@ -15,7 +15,7 @@ export function prepareBuckets(gateway: GatewayManager, firstShardId: number, la
|
||||
|
||||
// ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5,14 +5,5 @@ import type { Bot } from "../../bot.ts";
|
||||
export async function getGatewayBot(bot: Bot): Promise<GetGatewayBot> {
|
||||
const result = await bot.rest.runMethod<GetGatewayBot>(bot.rest, "get", bot.constants.endpoints.GATEWAY_BOT);
|
||||
|
||||
return {
|
||||
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,
|
||||
},
|
||||
};
|
||||
return bot.transformers.gatewayBot(result);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user