1 sec faster

This commit is contained in:
Skillz4Killz
2021-05-15 19:52:23 +00:00
committed by GitHub
parent 79c3435d6c
commit d58d3a043a
7 changed files with 20 additions and 22 deletions

View File

@@ -10,7 +10,7 @@ export function handleReady(
shardId: number,
) {
// Triggered on each shard
eventHandlers.shardReady?.(shardId);
// eventHandlers.shardReady?.(shardId);
// The bot has already started, the last shard is resumed, however.
if (cache.isReady) return;

View File

@@ -3,8 +3,7 @@ import { identify } from "./identify.ts";
import { resume } from "./resume.ts";
import { ws } from "./ws.ts";
// deno-lint-ignore require-await
export async function createShard(shardId: number) {
export function createShard(shardId: number) {
const socket = new WebSocket(ws.botGatewayData.url);
socket.binaryType = "arraybuffer";

View File

@@ -93,6 +93,8 @@ export async function handleOnMessage(message: any, shardId: number) {
// Important for RESUME
if (messageData.t === "READY") {
eventHandlers.shardReady?.(shardId);
const shard = ws.shards.get(shardId);
if (shard) {
shard.sessionId = (messageData.d as DiscordReady).session_id;
@@ -105,7 +107,7 @@ export async function handleOnMessage(message: any, shardId: number) {
const bucket = ws.buckets.get(
shardId % ws.botGatewayData.sessionStartLimit.maxConcurrency,
);
if (bucket) bucket.createNextShard = true;
if (bucket) bucket.createNextShard[0]?.();
}, 5000);
}

View File

@@ -12,7 +12,7 @@ export async function identify(shardId: number, maxShards: number) {
}
// CREATE A SHARD
const socket = await ws.createShard(shardId);
const socket = ws.createShard(shardId);
// Identify can just set/reset the settings for the shard
ws.shards.set(shardId, {

View File

@@ -1,7 +1,7 @@
import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { ws } from "./ws.ts";
export async function resume(shardId: number) {
export function resume(shardId: number) {
ws.log("RESUMING", { shardId });
// NOW WE HANDLE RESUMING THIS SHARD
@@ -16,7 +16,7 @@ export async function resume(shardId: number) {
}
// CREATE A SHARD
const socket = await ws.createShard(shardId);
const socket = ws.createShard(shardId);
const sessionId = oldShard?.sessionId || "";
const previousSequenceNumber = oldShard?.previousSequenceNumber || 0;

View File

@@ -1,4 +1,3 @@
import { delay } from "../util/utils.ts";
import { ws } from "./ws.ts";
/** Begin spawning shards. */
@@ -29,7 +28,7 @@ export function spawnShards(firstShardId = 0) {
// Create the bucket since it doesnt exist
ws.buckets.set(bucketId, {
clusters: [[cluster, i]],
createNextShard: true,
createNextShard: [],
});
if (cluster + 1 <= ws.maxClusters) cluster++;
@@ -51,7 +50,7 @@ export function spawnShards(firstShardId = 0) {
}
// SPREAD THIS OUT TO DIFFERENT CLUSTERS TO BEGIN STARTING UP
ws.buckets.forEach(async (bucket, bucketId) => {
ws.buckets.forEach((bucket, bucketId) => {
ws.log(
"DEBUG",
`3. Running forEach loop in spawnShards function.`,
@@ -61,19 +60,14 @@ export function spawnShards(firstShardId = 0) {
"DEBUG",
`4. Running for of loop in spawnShards function.`,
);
let shardId = queue.shift();
while (shardId !== undefined) {
ws.log("DEBUG", "5. Running while loop in spawnShards function.");
if (!bucket.createNextShard) {
await delay(100);
continue;
}
queue.forEach(shardId => {
bucket.createNextShard.push(async () => {
await ws.tellClusterToIdentify(clusterId, shardId, bucketId);
})
})
bucket.createNextShard = false;
await ws.tellClusterToIdentify(clusterId, shardId, bucketId);
shardId = queue.shift();
}
bucket.createNextShard[0]?.();
}
});
}

View File

@@ -82,7 +82,10 @@ export const ws = {
/** Stored as bucketId: { clusters: [clusterId, [ShardIds]], createNextShard: boolean } */
buckets: new Collection<
number,
{ clusters: number[][]; createNextShard: boolean }
{
clusters: number[][];
createNextShard: (() => unknown)[];
}
>(),
utf8decoder: new TextDecoder(),