Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lib/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const queue: (() => Promise<unknown>)[] = [];

export async function consumeQueue(batchSize = 2) {
console.log('consume queue');

const ts = queue.splice(0, batchSize);
if (ts.length > 0) {
console.log('got from queue', ts.length);

await Promise.all(ts.map((t) => t()));
}

setTimeout(() => consumeQueue(batchSize), 1000);
}

export function queueTask(task: () => Promise<unknown>) {
console.log('queue task');
return new Promise((resolve) => {
queue.push(async () => {
await task();
resolve(true);
});
});
}
10 changes: 9 additions & 1 deletion src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import { getRandomInt, rand } from './lib/rand';
import { getClients } from './macros/getClients';
// import { joinRooms } from './macros/joinRooms';
import { populateDatabase, isFullPopulation } from './populate';
import { consumeQueue, queueTask } from './lib/queue';

const queueLogin = async (client: Client) => {
await queueTask(async () => client.login());
};

export default (): void => {
let clients: Client[];

async function getLoggedInClient() {
const client = rand(clients);
if (!client.loggedIn) {
await client.login();
// await client.login();
await queueLogin(client);
}
return client;
}
Expand Down Expand Up @@ -86,6 +92,8 @@ export default (): void => {

b.on('ready', async () => {
console.log('Starting sending messages');

await consumeQueue(config.LOGIN_BATCH);
});

b.on('message', async () => {
Expand Down