perf: use waitUntil for async address activity updates (#826)

- Change updateAddressUpdatedAt to non-blocking async execution
- GET /api/mails, /api/settings, /user_api/settings no longer wait for DB update
- Improves response time for GET requests
- Also updates dependencies

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Dream Hunter
2026-02-01 21:20:39 +08:00
committed by GitHub
parent d367bc92b2
commit e4b6c82e92
10 changed files with 1072 additions and 991 deletions

View File

@@ -66,21 +66,23 @@ const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
return DEFAULT_NAME_REGEX;
}
export async function updateAddressUpdatedAt(
export function updateAddressUpdatedAt(
c: Context<HonoCustomType>,
address: string | undefined | null
): Promise<void> {
): void {
if (!address) {
return;
}
// update address updated_at
try {
await c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where name = ?`
).bind(address).run();
} catch (e) {
console.warn("Failed to update address updated_at", e);
}
// update address updated_at asynchronously
c.executionCtx.waitUntil((async () => {
try {
await c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where name = ?`
).bind(address).run();
} catch (e) {
console.warn("[updateAddressUpdatedAt] failed:", address, e);
}
})());
}
export const generateRandomPassword = (): string => {

View File

@@ -27,7 +27,7 @@ api.get('/api/mails', async (c) => {
return c.json({ "error": "No address" }, 400)
}
const { limit, offset } = c.req.query();
if (Number.parseInt(offset) <= 0) await updateAddressUpdatedAt(c, address);
if (Number.parseInt(offset) <= 0) updateAddressUpdatedAt(c, address);
return await handleListQuery(c,
`SELECT * FROM raw_mails where address = ?`,
`SELECT count(*) as count FROM raw_mails where address = ?`,
@@ -90,7 +90,7 @@ api.get('/api/settings', async (c) => {
return c.text(msgs.InvalidAddressMsg, 400)
}
await updateAddressUpdatedAt(c, address);
updateAddressUpdatedAt(c, address);
const no_limit_roles = getSplitStringListValue(c.env.NO_LIMIT_SEND_ROLE);
const is_no_limit_send_balance = user_role && no_limit_roles.includes(user_role);

View File

@@ -64,16 +64,17 @@ export default {
exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
iat: Math.floor(Date.now() / 1000),
}, c.env.JWT_SECRET, "HS256");
// update address updated_at
try {
await c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where id IN `
+ `(SELECT address_id FROM users_address WHERE user_id = ?)`
).bind(user.user_id).run();
} catch (e) {
console.warn("Failed to update address updated_at")
}
// update address updated_at asynchronously
c.executionCtx.waitUntil((async () => {
try {
await c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where id IN `
+ `(SELECT address_id FROM users_address WHERE user_id = ?)`
).bind(user.user_id).run();
} catch (e) {
console.warn("[user_api/settings] updateAddressUpdatedAt failed:", user.user_id, e);
}
})());
return c.json({
...user,
is_admin: is_admin,