perf: threads everywhere! (for encryption & decryption)

This commit is contained in:
Isaac
2025-02-12 04:26:15 +00:00
parent 5a908e77a7
commit d99cb202d5
16 changed files with 529 additions and 424 deletions

View File

@@ -39,7 +39,7 @@ async function sendToHouston(client) {
activated_users: users._count,
arch: process.arch,
database: process.env.DB_PROVIDER,
guilds: await relativePool(0.25, 'stats', pool => Promise.all(
guilds: await relativePool(.25, 'stats', pool => Promise.all(
guilds
.filter(guild => client.guilds.cache.has(guild.id))
.map(guild => {

View File

@@ -8,13 +8,13 @@ const { cpus } = require('node:os');
/**
* Use a thread pool of a fixed size
* @param {number} size number of threads
* @param {string} name name of file in workers directory
* @param {function} fun async function
* @param {import('threads/dist/master/pool').PoolOptions} options
* @returns {Promise<any>}
*/
async function pool(size, name, fun) {
const pool = Pool(() => spawn(new Worker(`./workers/${name}.js`)), { size });
async function pool(name, fun, options) {
const pool = Pool(() => spawn(new Worker(`./workers/${name}.js`)), options);
try {
return await fun(pool);
} finally {
@@ -40,19 +40,35 @@ async function quick(name, fun) {
/**
* Use a thread pool of a variable size
* @param {number} size fraction of available CPU cores to use (ceil'd)
* @param {number} fraction fraction of available CPU cores to use (ceil'd)
* @param {string} name name of file in workers directory
* @param {function} fun async function
* @param {import('threads/dist/master/pool').PoolOptions} options
* @returns {Promise<any>}
*/
function relativePool(fraction, ...args) {
function relativePool(fraction, name, fun, options) {
// ! ceiL: at least 1
const poolSize = Math.ceil(fraction * cpus().length);
return pool(poolSize, ...args);
const size = Math.ceil(fraction * cpus().length);
return pool(name, fun, {
...options,
size,
});
}
/**
* Spawn one thread
* @param {string} name name of file in workers directory
* @returns {Promise<{terminate: function}>}
*/
async function reusable(name) {
const thread = await spawn(new Worker(`./workers/${name}.js`));
thread.terminate = () => Thread.terminate(thread);
return thread;
};
module.exports = {
pool,
quick,
relativePool,
reusable,
};

View File

@@ -1,5 +1,5 @@
const Cryptr = require('cryptr');
const { encrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
const { reusable } = require('../threads');
/**
* Returns highest (roles.highest) hoisted role, or everyone
@@ -71,85 +71,90 @@ module.exports = class TicketArchiver {
});
}
for (const member of members) {
const data = {
avatar: member.avatar || member.user.avatar, // TODO: save avatar in user/avatars/
bot: member.user.bot,
discriminator: member.user.discriminator,
displayName: member.displayName ? encrypt(member.displayName) : null,
roleId: !!member && hoistedRole(member).id,
ticketId,
userId: member.user.id,
username: encrypt(member.user.username),
};
await this.client.prisma.archivedUser.upsert({
create: data,
update: data,
where: {
ticketId_userId: {
ticketId,
userId: member.user.id,
const worker = await reusable('crypto');
try {
for (const member of members) {
const data = {
avatar: member.avatar || member.user.avatar, // TODO: save avatar in user/avatars/
bot: member.user.bot,
discriminator: member.user.discriminator,
displayName: member.displayName ? await worker.encrypt(member.displayName) : null,
roleId: !!member && hoistedRole(member).id,
ticketId,
userId: member.user.id,
username: await worker.encrypt(member.user.username),
};
await this.client.prisma.archivedUser.upsert({
create: data,
update: data,
where: {
ticketId_userId: {
ticketId,
userId: member.user.id,
},
},
});
}
let reference;
if (message.reference) reference = await message.fetchReference();
const messageD = {
author: {
connect: {
ticketId_userId: {
ticketId,
userId: message.author?.id || 'default',
},
},
},
});
}
let reference;
if (message.reference) reference = await message.fetchReference();
const messageD = {
author: {
connect: {
ticketId_userId: {
ticketId,
userId: message.author?.id || 'default',
},
},
},
content: encrypt(
JSON.stringify({
attachments: [...message.attachments.values()],
components: [...message.components.values()],
content: message.content,
embeds: message.embeds.map(embed => ({ ...embed })),
reference: reference ? reference.id : null,
}),
),
createdAt: message.createdAt,
edited: !!message.editedAt,
external,
id: message.id,
};
return await this.client.prisma.ticket.update({
data: {
archivedChannels: {
upsert: channels.map(channel => {
const data = {
channelId: channel.id,
name: channel.name,
};
return {
create: data,
update: data,
where: {
ticketId_channelId: {
channelId: channel.id,
ticketId,
},
},
};
content: await worker.encrypt(
JSON.stringify({
attachments: [...message.attachments.values()],
components: [...message.components.values()],
content: message.content,
embeds: message.embeds.map(embed => ({ ...embed })),
reference: reference ? reference.id : null,
}),
},
archivedMessages: {
upsert: {
create: messageD,
update: messageD,
where: { id: message.id },
),
createdAt: message.createdAt,
edited: !!message.editedAt,
external,
id: message.id,
};
return await this.client.prisma.ticket.update({
data: {
archivedChannels: {
upsert: channels.map(channel => {
const data = {
channelId: channel.id,
name: channel.name,
};
return {
create: data,
update: data,
where: {
ticketId_channelId: {
channelId: channel.id,
ticketId,
},
},
};
}),
},
archivedMessages: {
upsert: {
create: messageD,
update: messageD,
where: { id: message.id },
},
},
},
},
where: { id: ticketId },
});
where: { id: ticketId },
});
} finally {
await worker.terminate();
}
}
};
};

View File

@@ -19,13 +19,13 @@ const { logTicketEvent } = require('../logging');
const { isStaff } = require('../users');
const { Collection } = require('discord.js');
const spacetime = require('spacetime');
const Cryptr = require('cryptr');
const {
decrypt,
encrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
const { getSUID } = require('../logging');
const { getAverageTimes } = require('../stats');
const {
quick,
reusable,
} = require('../threads');
/**
* @typedef {import('@prisma/client').Category &
@@ -148,7 +148,9 @@ module.exports = class TicketManager {
/**
* @param {object} data
* @param {string} data.categoryId
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").SelectMenuInteraction} data.interaction
* @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").SelectMenuInteraction} data.interaction
* @param {string?} [data.topic]
*/
async create({
@@ -353,7 +355,9 @@ module.exports = class TicketManager {
/**
* @param {object} data
* @param {string} data.category
* @param {import("discord.js").ButtonInteraction|import("discord.js").SelectMenuInteraction|import("discord.js").ModalSubmitInteraction} data.interaction
* @param {import("discord.js").ButtonInteraction
* | import("discord.js").SelectMenuInteraction
* | import("discord.js").ModalSubmitInteraction} data.interaction
* @param {string?} [data.topic]
*/
async postQuestions({
@@ -367,11 +371,22 @@ module.exports = class TicketManager {
let answers;
if (interaction.isModalSubmit()) {
if (action === 'questions') {
answers = category.questions.filter(q => q.type === 'TEXT').map(q => ({
questionId: q.id,
userId: interaction.user.id,
value: interaction.fields.getTextInputValue(q.id) ? encrypt(interaction.fields.getTextInputValue(q.id)) : '',
}));
const worker = await reusable('crypto');
try {
answers = await Promise.all(
category.questions
.filter(q => q.type === 'TEXT')
.map(async q => ({
questionId: q.id,
userId: interaction.user.id,
value: interaction.fields.getTextInputValue(q.id)
? await worker.encrypt(interaction.fields.getTextInputValue(q.id))
: '', // TODO: maybe this should be null?
})),
);
} finally {
await worker.terminate();
}
if (category.customTopic) topic = interaction.fields.getTextInputValue(category.customTopic);
} else if (action === 'topic') {
topic = interaction.fields.getTextInputValue('topic');
@@ -612,7 +627,7 @@ module.exports = class TicketManager {
embed.addFields({
inline: false,
name: getMessage('ticket.references_ticket.fields.topic'),
value: decrypt(ticket.topic),
value: await quick('crypto', worker => worker.decrypt(ticket.topic)),
});
}
await channel.send({ embeds: [embed] });
@@ -631,7 +646,7 @@ module.exports = class TicketManager {
id: channel.id,
number,
openingMessageId: sent.id,
topic: topic ? encrypt(topic) : null,
topic: topic ? await quick('crypto', worker => worker.encrypt(topic)) : null,
};
if (referencesTicketId) data.referencesTicket = { connect: { id: referencesTicketId } };
if (answers) data.questionAnswers = { createMany: { data: answers } };
@@ -1073,7 +1088,9 @@ module.exports = class TicketManager {
}
/**
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").ModalSubmitInteraction} interaction
* @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").ModalSubmitInteraction} interaction
* @param {string} reason
*/
async requestClose(interaction, reason) {
@@ -1143,7 +1160,9 @@ module.exports = class TicketManager {
}
/**
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").ModalSubmitInteraction} interaction
* @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").ModalSubmitInteraction} interaction
*/
async acceptClose(interaction) {
const ticket = await this.getTicket(interaction.channel.id);
@@ -1191,7 +1210,7 @@ module.exports = class TicketManager {
where: { id: closedBy },
},
} || undefined, // Prisma wants undefined not null because it is a relation
closedReason: reason && encrypt(reason),
closedReason: reason && await quick('crypto', worker => worker.encrypt(reason)),
messageCount: archivedMessages,
open: false,
};
@@ -1248,7 +1267,7 @@ module.exports = class TicketManager {
embed.addFields({
inline: true,
name: getMessage('dm.closed.fields.topic'),
value: decrypt(ticket.topic),
value: await quick('crypto', worker => worker.decrypt(ticket.topic)),
});
}

11
src/lib/workers/crypto.js Normal file
View File

@@ -0,0 +1,11 @@
const { expose } = require('threads/worker');
const Cryptr = require('cryptr');
const {
encrypt,
decrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
expose({
decrypt,
encrypt,
});

View File

@@ -0,0 +1,36 @@
const { expose } = require('threads/worker');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
function getTranscript(ticket) {
ticket.claimedBy = ticket.archivedUsers.find(u => u.userId === ticket.claimedById);
ticket.closedBy = ticket.archivedUsers.find(u => u.userId === ticket.closedById);
ticket.createdBy = ticket.archivedUsers.find(u => u.userId === ticket.createdById);
if (ticket.closedReason) ticket.closedReason = decrypt(ticket.closedReason);
if (ticket.feedback?.comment) ticket.feedback.comment = decrypt(ticket.feedback.comment);
if (ticket.topic) ticket.topic = decrypt(ticket.topic).replace(/\n/g, '\n\t');
ticket.archivedUsers.forEach((user, i) => {
if (user.displayName) user.displayName = decrypt(user.displayName);
user.username = decrypt(user.username);
ticket.archivedUsers[i] = user;
});
ticket.archivedMessages.forEach((message, i) => {
message.author = ticket.archivedUsers.find(u => u.userId === message.authorId);
message.content = JSON.parse(decrypt(message.content));
message.text = message.content.content?.replace(/\n/g, '\n\t') ?? '';
message.content.attachments?.forEach(a => (message.text += '\n\t' + a.url));
message.content.embeds?.forEach(() => (message.text += '\n\t[embedded content]'));
message.number = 'M' + String(i + 1).padStart(ticket.archivedMessages.length.toString().length, '0');
ticket.archivedMessages[i] = message;
});
ticket.pinnedMessageIds = ticket.pinnedMessageIds.map(id => ticket.archivedMessages.find(message => message.id === id)?.number);
return ticket;
}
expose(getTranscript);