Skip to content

Commit

Permalink
fix(bot): improve concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Feb 12, 2024
1 parent 19a4bec commit 1c5a0eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"i18next": "^21.10.0",
"i18next-fs-backend": "^1.1.4",
"ioredis": "^5.0.6",
"just-group-by": "^2.2.0",
"just-range": "^4.2.0",
"just-split": "^3.2.0",
"nanoid": "^3.3.4",
"node-fetch": "2.6.7",
"slash-create": "^5.10.0",
Expand Down
20 changes: 12 additions & 8 deletions apps/bot/src/sharding/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from 'eventemitter3';
import groupBy from 'just-group-by';
import range from 'just-range';
import split from 'just-split';

import { wait } from '../util';
import * as logger from './logger';
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class ShardManager extends EventEmitter {
}
}

async spawnAllWithConcurrency(concurrency = this.options.concurrency, delay = 500) {
async spawnAllWithConcurrency(concurrency = this.options.concurrency || 1, delay = 500) {
const spawnShard = async (id: number) => {
let retries = 0;
while (retries < 5) {
Expand All @@ -97,12 +97,16 @@ export default class ShardManager extends EventEmitter {
}
};

const batches = split(range(this.options.shardCount), concurrency);
for (const batchNum in batches) {
const batch = batches[batchNum];
if (concurrency !== 1) logger.info(`Spawning shards ${batch[0]}-${[...batch].reverse()[0]} in a batch (#${batchNum})`);
await Promise.all(batch.map(spawnShard));
}
const spawnBucket = async (ids: number[]) => {
for (const id of ids) {
await spawnShard(id);
await wait(5_000);
}
logger.info(`Concurrency bucket #${ids[0] % concurrency} finished`);
};

const buckets = Object.values(groupBy(range(this.options.shardCount), (n) => n % concurrency));
await Promise.all(buckets.map(spawnBucket));
}

async spawnAll(delay = 500) {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4305,16 +4305,16 @@ jsonwebtoken@^8.5.1:
array-includes "^3.1.3"
object.assign "^4.1.2"

just-group-by@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/just-group-by/-/just-group-by-2.2.0.tgz#814be9b4ab55b1bed07c3df6587b7fbbfe0f9c9c"
integrity sha512-brZocDSmkvDYc+FOr07GiTar4cp7K6OB9huVOrj5LX+iXfCe3hTvn2KGM811LE/SBQkCzuYDDEcG8HA/dKDrnw==

just-range@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/just-range/-/just-range-4.2.0.tgz#34d901f76b2d0ce3a0d2c98cb691b06303945122"
integrity sha512-z2Ip8H2j7a9Vr8rKFi0IZf4IXn2Yuq2lGZFS41GjfPwksoHWTaTx1xIjU6C0HyQ1jBXEoz/FzjS5eyVXeW19tg==

just-split@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/just-split/-/just-split-3.2.0.tgz#14552b426e4f4c48bfbd7a716d97844ff73ea774"
integrity sha512-hh57dN5koTBkmg3T6gBFISVVaW5bgZ6Ct1W5KODD5M7hQJKqGzTKkfMwOil8MBxyztLQEjh/v6UGXE8cP5tnqQ==

jwa@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
Expand Down

0 comments on commit 1c5a0eb

Please sign in to comment.