Skip to content
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

Handle 409s in fetch cache #51652

Merged
merged 9 commits into from
Jul 6, 2023
39 changes: 39 additions & 0 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getDerivedTags } from './utils'

let memoryCache: LRUCache<string, CacheHandlerValue> | undefined

let rateLimitedUntil = 0

interface NextFetchCacheParams {
internal?: boolean
fetchType?: string
Expand Down Expand Up @@ -102,6 +104,14 @@ export default class FetchCache implements CacheHandler {
if (this.debug) {
console.log('revalidateTag', tag)
}

if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited ', rateLimitedUntil)
}
return
}

try {
const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${tag}`,
Expand All @@ -113,6 +123,11 @@ export default class FetchCache implements CacheHandler {
}
)

if (res.status === 429) {
const retryAfter = res.headers.get('retry-after') || '60000'
rateLimitedUntil = Date.now() + parseInt(retryAfter)
lubakravche marked this conversation as resolved.
Show resolved Hide resolved
}

if (!res.ok) {
throw new Error(`Request failed with status ${res.status}.`)
}
Expand All @@ -129,6 +144,13 @@ export default class FetchCache implements CacheHandler {
) {
if (!fetchCache) return null

if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited')
}
return null
}

let data = memoryCache?.get(key)

// memory cache data is only leveraged for up to 1 seconds
Expand Down Expand Up @@ -159,6 +181,11 @@ export default class FetchCache implements CacheHandler {
}
)

if (res.status === 429) {
const retryAfter = res.headers.get('retry-after') || '60000'
rateLimitedUntil = Date.now() + parseInt(retryAfter)
}

if (res.status === 404) {
if (this.debug) {
console.log(
Expand Down Expand Up @@ -241,6 +268,13 @@ export default class FetchCache implements CacheHandler {
) {
if (!fetchCache) return

if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited')
}
return
}

memoryCache?.set(key, {
value: data,
lastModified: Date.now(),
Expand Down Expand Up @@ -290,6 +324,11 @@ export default class FetchCache implements CacheHandler {
}
)

if (res.status === 429) {
const retryAfter = res.headers.get('retry-after') || '60000'
rateLimitedUntil = Date.now() + parseInt(retryAfter)
}

if (!res.ok) {
this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`)
Expand Down