Repository navigation
Reduce per-invocation maintenance CPU work #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { | |
| createHmacEmailCodeProtector, | ||
| createIdentity, | ||
| type Identity, | ||
| type EmailCodeProtector, | ||
| type MailProvider, | ||
| type MailReconciliationPort, | ||
| type MailWorker, | ||
|
|
@@ -91,6 +92,18 @@ function durableLimiter( | |
| ); | ||
| } | ||
|
|
||
| /** Keep key validation eager, but import HMAC only when code work needs it. */ | ||
| function deferredEmailCodeProtector(secret: Uint8Array): EmailCodeProtector { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Deferred EmailCodeProtector proxies only three methods ( A request that exercises an EmailCodeProtector method not proxied by the deferred wrapper throws
Checked
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not applicable to the pinned dependency. The installed |
||
| let protector: EmailCodeProtector | undefined; | ||
| const get = () => (protector ??= createHmacEmailCodeProtector(secret)); | ||
| return { | ||
| deriveCode: (handleHash) => get().deriveCode(handleHash), | ||
| verifier: (handleHash, code) => get().verifier(handleHash, code), | ||
| matches: (expected, handleHash, code) => | ||
| get().matches(expected, handleHash, code), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Production composition for pegma.dev's first-party Identity provider. | ||
| * | ||
|
|
@@ -159,37 +172,33 @@ export function createIdentityRuntime( | |
| origins: ['https://pegma.dev'], | ||
| registrationLimiter, | ||
| authenticationLimiter, | ||
| emailCodeProtector: createHmacEmailCodeProtector( | ||
| emailCodeProtector: deferredEmailCodeProtector( | ||
| emailCodeSecret(options.emailCodeSecretBase64), | ||
| ), | ||
| emailCodeRequestLimiter, | ||
| emailCodeVerificationLimiter, | ||
| }); | ||
|
|
||
| const resendKey = | ||
| typeof options.resendApiKey === 'string' && | ||
| options.resendApiKey.length > 0 | ||
| typeof options.resendApiKey === 'string' && options.resendApiKey.length > 0 | ||
| ? options.resendApiKey | ||
| : null; | ||
| const delivery = | ||
| !options.emailEnabled | ||
| ? null | ||
| : options.mailDelivery ?? | ||
| (resendKey === null | ||
| ? null | ||
| : (() => { | ||
| const ports = createResendMailPorts({ | ||
| apiKey: resendKey, | ||
| from: options.emailFrom, | ||
| ...(options.fetch === undefined | ||
| ? {} | ||
| : { fetch: options.fetch }), | ||
| }); | ||
| return { | ||
| ...ports, | ||
| classifyFailure: classifyResendFailure, | ||
| }; | ||
| })()); | ||
| const delivery = !options.emailEnabled | ||
| ? null | ||
| : (options.mailDelivery ?? | ||
| (resendKey === null | ||
| ? null | ||
| : (() => { | ||
| const ports = createResendMailPorts({ | ||
| apiKey: resendKey, | ||
| from: options.emailFrom, | ||
| ...(options.fetch === undefined ? {} : { fetch: options.fetch }), | ||
| }); | ||
| return { | ||
| ...ports, | ||
| classifyFailure: classifyResendFailure, | ||
| }; | ||
| })())); | ||
| const mailWorker = | ||
| delivery === null | ||
| ? null | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 One-record pages bound CPU but can starve mail and retention (
risk)With
PAGE_LIMIT = 1and separate 1-minute triggers, a burst of >60 sends or >59 reconciliations per hour plus retained terminal records keeps the scan cursor on terminal rows, extending time-to-drain beyond the email-code lifetime and turning a transient spike into expired codes and dead-letter growth.worker/src/identity-maintenance.ts:59andworker/src/support-maintenance.ts:23cap every sweep andrunSendPage/runReconciliationPageto1. Mail still retains terminal records for operator inspection untilsweepMaildeletes after 7 days (worker/src/identity-maintenance.ts:232), andworker/README.md:294itself notes scanning "at most 60 send and 59 reconciliation operation records per hour, including retained terminal records." Support queue repair has the same cap whilerepairQueueProjectionPagecan touch multiple rows per ticket.Checked
worker/src/identity-maintenance.ts:187,worker/src/maintenance-cursors.d1.test.ts:122(drains 3 jobs over 6 ticks), andworker/README.md:292. No adaptive back-pressure or batch acknowledgement exists.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a real, intentionally documented capacity tradeoff, rather than an unacknowledged throughput guarantee. The request is to reduce per-invocation work for this low-traffic Worker; the README explicitly states 60 send / 59 reconciliation scanned records per hour (including retained records), requires a full scan comfortably below the ten-minute code lifetime, and calls for indexed discovery / paginated component APIs or a larger CPU allowance as backlog grows. Raising the page size or processing several pages in one invocation would remove the CPU bound this change introduces. The cursors make bounded progress; retained rows increase scan latency rather than pinning a cursor permanently. I am leaving this unresolved for a maintainer's capacity/design decision. A larger-traffic queue-discovery redesign is outside this CPU-reduction patch.