File
Blob: src/worker/do/repo/repoDO/access.ts
| 1 | import type { Logger } from "@/worker/common/logger"; |
| 2 | |
| 3 | import { asTypedStorage } from "../repoState"; |
| 4 | import type { RepoStateSchema } from "../repoState"; |
| 5 | import { ensureScheduled } from "../scheduler"; |
| 6 | |
| 7 | export type RepoDOAccessContext = { |
| 8 | ctx: DurableObjectState; |
| 9 | env: Env; |
| 10 | logger: Logger; |
| 11 | getLastAccessMemMs(): number | undefined; |
| 12 | setLastAccessMemMs(value: number): void; |
| 13 | }; |
| 14 | |
| 15 | export async function touchAndMaybeSchedule(args: RepoDOAccessContext): Promise<void> { |
| 16 | const now = Date.now(); |
| 17 | const store = asTypedStorage<RepoStateSchema>(args.ctx.storage); |
| 18 | const lastAccessMemMs = args.getLastAccessMemMs(); |
| 19 | |
| 20 | // Throttle access writes so RPC-heavy read paths do not amplify storage churn. |
| 21 | try { |
| 22 | if (!lastAccessMemMs || now - lastAccessMemMs >= 60_000) { |
| 23 | await store.put("lastAccessMs", now); |
| 24 | args.setLastAccessMemMs(now); |
| 25 | } |
| 26 | } catch {} |
| 27 | await ensureScheduled(args.ctx, args.env, now); |
| 28 | } |
| 29 | |
| 30 | export async function ensureAccessAndAlarm(args: RepoDOAccessContext): Promise<void> { |
| 31 | try { |
| 32 | await touchAndMaybeSchedule(args); |
| 33 | } catch (error) { |
| 34 | try { |
| 35 | args.logger.warn("touch:schedule:failed", { error: String(error) }); |
| 36 | } catch {} |
| 37 | } |
| 38 | } |