perf(signup): derive a web sign-up's password off the main thread

POST /api/register used accounts.register, whose scryptSync froze every
player for as long as each password took, so a burst of sign-ups was felt
in the game. accounts.registerAsync checks the same things, derives the
hash on libuv's pool (hashPasswordAsync), checks the name again after it
(another request may have taken it meanwhile) and inserts the account as
before. The game's own Create Account keeps the sync path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
SnipzilandClaude Opus 5.5 committed 2026-10-21 22:24:59 -04:00
1 parent 6645bf66b3
commit 43f2717b44
2 files changed
+30 -2

No files matched your search

+29 -1
View File
@@ -737,6 +737,34 @@ const register = (username, password, avatar = {}) => {
return {account};
};
// Public sign-ups can arrive in a burst. scryptSync is fine for the local,
// legacy Create Account socket but it would freeze all players while each web
// registration is derived. Keep the same validation and database insert on
// the main thread, but derive its password on libuv's worker pool. Check the
// username again after awaiting: another request may have claimed it first.
const registerAsync = async (username, password, avatar = {}) => {
username = typeof username === "string" ? username.trim() : username;
let error = usernameProblem(username) || passwordProblem(password, username) || (defpacks.avatarDef(avatar.type) ? null : "choose_animal");
if (error) {
return {error};
}
const salt = crypto.randomBytes(16).toString("hex");
const hash = await hashPasswordAsync(password, salt);
error = usernameProblem(username) || passwordProblem(password, username) || (defpacks.avatarDef(avatar.type) ? null : "choose_animal");
if (error) {
return {error};
}
const type = Number(avatar.type);
const account = insert(username, salt, hash, now(), {
avatars: [newAvatar({perUserAvId: 1, avInvId: 1}, avatar.name || username, type, -1)],
activeAvatar: 1,
badges: byId.size < EARLY_MEMBERS ? [EARLY_BADGE] : [],
recoveryEmail: typeof avatar.email === "string" && EMAIL_PATTERN.test(avatar.email.trim()) ? avatar.email.trim().toLowerCase() : "",
});
console.log(`[accounts] registered "${logText(username)}" (id ${logText(account.id)})`);
return {account};
};
const authenticate = (username, password) => {
// "name" and "name " are the same account.
const account = typeof username === "string" ? touch(byKey.get(username.trim().toLowerCase())) : null;
@@ -954,4 +982,4 @@ const all = () => [...byId.values()];
const database = () => db;
module.exports = {FEATURES, featureOn, recordLogin, revokeTokens, load, save, flush, hold, release, startBackups, database, setLoginChoices, setPassword, rename, remove, setting, setSetting, hashPassword, register, usernameProblem, authenticate, authenticateAsync, issueToken, byToken, byName, nameOf, byId: accountById, newAvatar, freeAvatarSlot, all};
module.exports = {FEATURES, featureOn, recordLogin, revokeTokens, load, save, flush, hold, release, startBackups, database, setLoginChoices, setPassword, rename, remove, setting, setSetting, hashPassword, register, registerAsync, usernameProblem, authenticate, authenticateAsync, issueToken, byToken, byName, nameOf, byId: accountById, newAvatar, freeAvatarSlot, all};
+1 -1
View File
@@ -282,7 +282,7 @@ const handle = async (route, params, body, from) => {
if (!STARTER_ANIMALS.includes(avatarType)) {
return {status: 400, body: {error: "choose_animal"}};
}
const result = accounts.register(username, password, {type: avatarType, email});
const result = await accounts.registerAsync(username, password, {type: avatarType, email});
if (result.account) {
result.account.agreed = {version: config.termsVersion, at: Math.floor(Date.now() / 1000)};
accounts.save(result.account);