:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): stop blaming the daily cap when the rate limiter fails (#101284)
Daniel Marchuk479e05a3cd96
diff --git a/nodejs/src/cdp/services/messaging/email.service.test.ts b/nodejs/src/cdp/services/messaging/email.service.test.tsindex ab567bbb2bdb..b1feda043a43 100644--- a/nodejs/src/cdp/services/messaging/email.service.test.ts+++ b/nodejs/src/cdp/services/messaging/email.service.test.ts@@ -773,6 +773,9 @@ describe('EmailService', () => {const scheduledMs = result.invocation.queueScheduledAt!.toMillis()expect(scheduledMs).toBeGreaterThanOrEqual(before + minMs)expect(scheduledMs).toBeLessThan(before + maxMs + 5000)+ // A real denial names the cap it hit, which is what makes the limiter-fault case+ // above distinguishable to the customer reading the run's logs.+ expect(result.logs.map((log) => log.message).join(' ')).toContain('reached its email sending limit of')})it('retries on the token bucket cadence when the limiter reports no horizon', async () => {@@ -794,6 +797,10 @@ describe('EmailService', () => {const scheduledMs = result.invocation.queueScheduledAt!.toMillis()expect(scheduledMs).toBeGreaterThanOrEqual(before + 5 * 60 * 1000)expect(scheduledMs).toBeLessThan(before + 5 * 60 * 1000 + 5000)+ // No bucket denied, so the customer must not be told they hit a cap they never hit.+ const messages = result.logs.map((log) => log.message).join(' ')+ expect(messages).not.toContain('reached its email sending limit')+ expect(messages).toContain("Could not check this project's email sending limit")})it('sends when the claim is granted', async () => {diff --git a/nodejs/src/cdp/services/messaging/email.service.ts b/nodejs/src/cdp/services/messaging/email.service.tsindex 1e95f8cf2a34..d786ceff9f04 100644--- a/nodejs/src/cdp/services/messaging/email.service.ts+++ b/nodejs/src/cdp/services/messaging/email.service.ts@@ -560,9 +560,12 @@ export class EmailService {// without them the rescheduled dequeue resumes the Hog VM and drops the send.result.invocation.queueParameters = paramsresult.invocation.queueScheduledAt = DateTime.utc().plus({ milliseconds: capDelay.retryDelayMs })+ const capRetrySeconds = Math.round(capDelay.retryDelayMs / 1000)addLog('info',- `This project reached its email sending limit of ${capDelay.label}. Retrying this email in ${Math.round(capDelay.retryDelayMs / 1000)}s. The limit rises as the project builds a clean sending history.`+ capDelay.label+ ? `This project reached its email sending limit of ${capDelay.label}. Retrying this email in ${capRetrySeconds}s. The limit rises as the project builds a clean sending history.`+ : `Could not check this project's email sending limit, so this email is waiting. Retrying in ${capRetrySeconds}s. No action is needed on your side.`)return result}@@ -688,12 +691,14 @@ export class EmailService {* Failure stances differ on purpose. A failed tier lookup lets the send through, because a* config blip must never throttle a legitimate customer. A failed bucket claim returns 0 from* the limiter, which delays the send, because we must not send when we cannot account for it.+ * That second case returns a null `label`: the send waits, but no cap was reached, so the+ * caller must not tell the customer it hit one.*/private async claimTeamSendingBudget(invocation: CyclotronJobInvocationHogFunction,isTest: boolean,recipients: number = 1- ): Promise<{ retryDelayMs: number; label: string } | null> {+ ): Promise<{ retryDelayMs: number; label: string | null } | null> {const mode: TeamEmailCapMode = this.sesConfig.teamEmailCapMode ?? 'off'if (mode === 'off' || isTest || !this.teamEmailRateLimiter) {return null@@ -734,13 +739,23 @@ export class EmailService {if (claim.granted) {return null}- const denied = buckets[claim.deniedIndex ?? 1]- teamEmailCapDelayedTotal.inc({ tier: String(tier), bucket: denied.name, mode })- const retryDelayMs = pickReservedRetryDelayMs(claim.retryAfterMs, denied.refillPerSecond, claim.reserved)+ // `deniedIndex` is null when the limiter itself failed, not when a bucket was full. The+ // send still waits, because we must not send what we cannot account for, but no cap was+ // reached, so a null label tells the caller it has no cap to name to the customer.+ const denied = claim.deniedIndex === null ? null : buckets[claim.deniedIndex]+ // The daily bucket paces a limiter failure because it is the slower of the two, so the+ // retry backs off at the safer rate while the limiter is unreachable.+ const pacing = denied ?? buckets[1]+ teamEmailCapDelayedTotal.inc({+ tier: String(tier),+ bucket: denied?.name ?? 'limiter_unavailable',+ mode,+ })+ const retryDelayMs = pickReservedRetryDelayMs(claim.retryAfterMs, pacing.refillPerSecond, claim.reserved)emailReservedParkMs.labels('team-email').observe(retryDelayMs)return {retryDelayMs,- label: denied.label,+ label: denied?.label ?? null,}}