Retry Resend calls on 429 and throttle Newsletter Archive lookups; v2.7.1
Publish Container / build (push) Successful in 15m57s
Publish Container / build (push) Successful in 15m57s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
66cdb9c4c9
commit
dad0e5691e
4 files changed
+37
-17
No files matched your search
+17
-4
@@ -84,6 +84,19 @@ export async function sendResendEmailWithRetry({ resend, payload, context, maxAt
|
||||
throw lastError ?? new Error(`[${context}] email send failed`)
|
||||
}
|
||||
|
||||
// Retry a Resend SDK call when the account's rate limit (429) is hit. The SDK returns { error } rather than throwing.
|
||||
export async function resendRetry(call, maxAttempts = 5) {
|
||||
let result
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
||||
result = await call()
|
||||
const err = result?.error
|
||||
const limited = err && (err.statusCode === 429 || err.name === 'rate_limit_exceeded')
|
||||
if (!limited) return result
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * 2 ** attempt))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export async function syncContactToResend(name, email) {
|
||||
if (!process.env.RESEND_API_KEY) return
|
||||
const { firstName, lastName } = splitName(name)
|
||||
@@ -91,19 +104,19 @@ export async function syncContactToResend(name, email) {
|
||||
try {
|
||||
// Never touch `unsubscribed` for a contact that already exists — otherwise a form submission
|
||||
// would silently resubscribe someone who opted out in Resend.
|
||||
const { data: existing } = await contactResend.contacts.get({ email })
|
||||
const { data: existing } = await resendRetry(() => contactResend.contacts.get({ email }))
|
||||
if (existing) {
|
||||
const { error: updateError } = await contactResend.contacts.update({ email, firstName, lastName })
|
||||
const { error: updateError } = await resendRetry(() => contactResend.contacts.update({ email, firstName, lastName }))
|
||||
if (updateError) console.error('[resend] contact sync error:', updateError)
|
||||
return
|
||||
}
|
||||
const { error: createError } = await contactResend.contacts.create({
|
||||
const { error: createError } = await resendRetry(() => contactResend.contacts.create({
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
unsubscribed: false,
|
||||
...(process.env.RESEND_SEGMENT_ID ? { segments: [{ id: process.env.RESEND_SEGMENT_ID }] } : {}),
|
||||
})
|
||||
}))
|
||||
if (createError) console.error('[resend] contact sync error:', createError)
|
||||
} catch (err) {
|
||||
console.error('[resend] contact sync exception:', err)
|
||||
|
||||
Reference in new issue
Block a user