perf: throttle address activity updates (#1104)

* perf: throttle address activity updates

* docs: record address activity write throttling

* refactor: inline address activity interval

* test: cover address activity throttling
This commit is contained in:
Dream Hunter
2026-08-07 10:50:38 +08:00
committed by GitHub
parent d04c1a865d
commit 5553c6484a
5 changed files with 119 additions and 14 deletions
+26 -2
View File
@@ -250,10 +250,34 @@ export function updateAddressUpdatedAt(
c.executionCtx.waitUntil((async () => {
try {
await c.env.DB.prepare(
`UPDATE address SET updated_at = datetime('now') where name = ?`
`UPDATE address SET updated_at = datetime('now')`
+ ` WHERE name = ?`
+ ` AND (updated_at IS NULL OR updated_at < datetime('now', '-1 day'))`
).bind(address).run();
} catch (e) {
console.warn("[updateAddressUpdatedAt] failed:", address, e);
const errorName = e instanceof Error ? e.name : "UnknownError";
console.warn("[updateAddressUpdatedAt] failed:", errorName);
}
})());
}
export function updateUserAddressesUpdatedAt(
c: Context<HonoCustomType>,
userId: number | string | undefined | null
): void {
if (!userId) {
return;
}
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 = ?)`
+ ` AND (updated_at IS NULL OR updated_at < datetime('now', '-1 day'))`
).bind(userId).run();
} catch (e) {
const errorName = e instanceof Error ? e.name : "UnknownError";
console.warn("[updateUserAddressesUpdatedAt] failed:", errorName);
}
})());
}
+2 -12
View File
@@ -4,7 +4,7 @@ import i18n from "../i18n";
import { UserOauth2Settings, UserSettings } from "../models";
import { getJsonSetting, getUserRoles } from "../utils"
import { CONSTANTS } from "../constants";
import { commonGetUserRole } from "../common";
import { commonGetUserRole, updateUserAddressesUpdatedAt } from "../common";
import { Jwt } from "hono/utils/jwt";
export default {
@@ -64,17 +64,7 @@ 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 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);
}
})());
updateUserAddressesUpdatedAt(c, user.user_id);
return c.json({
...user,
is_admin: is_admin,