Unverified Commit 8ed2b275 authored by Luba Kravchenko's avatar Luba Kravchenko Committed by GitHub

Handle 409s in fetch cache (#51652)

### What?
Adds 409 response handling for fetch cache

### Why?
So we stop making data cache requests when it returns 409 for a
specified amount of time

### How?
Set a module scope variable `rateLimitedUntil` that we check against
before making a request. If we receive a 409 then set `rateLimitedUntil`
to now() + either a specified retry-after value returned from the api or
60s (can make this something else)

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent bd02b4db
...@@ -6,6 +6,8 @@ import { getDerivedTags } from './utils' ...@@ -6,6 +6,8 @@ import { getDerivedTags } from './utils'
let memoryCache: LRUCache<string, CacheHandlerValue> | undefined let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
let rateLimitedUntil = 0
interface NextFetchCacheParams { interface NextFetchCacheParams {
internal?: boolean internal?: boolean
fetchType?: string fetchType?: string
...@@ -102,6 +104,14 @@ export default class FetchCache implements CacheHandler { ...@@ -102,6 +104,14 @@ export default class FetchCache implements CacheHandler {
if (this.debug) { if (this.debug) {
console.log('revalidateTag', tag) console.log('revalidateTag', tag)
} }
if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited ', rateLimitedUntil)
}
return
}
try { try {
const res = await fetch( const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${tag}`, `${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${tag}`,
...@@ -113,6 +123,11 @@ export default class FetchCache implements CacheHandler { ...@@ -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)
}
if (!res.ok) { if (!res.ok) {
throw new Error(`Request failed with status ${res.status}.`) throw new Error(`Request failed with status ${res.status}.`)
} }
...@@ -129,6 +144,13 @@ export default class FetchCache implements CacheHandler { ...@@ -129,6 +144,13 @@ export default class FetchCache implements CacheHandler {
) { ) {
if (!fetchCache) return null if (!fetchCache) return null
if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited')
}
return null
}
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
// memory cache data is only leveraged for up to 1 seconds // memory cache data is only leveraged for up to 1 seconds
...@@ -159,6 +181,11 @@ export default class FetchCache implements CacheHandler { ...@@ -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 (res.status === 404) {
if (this.debug) { if (this.debug) {
console.log( console.log(
...@@ -241,6 +268,13 @@ export default class FetchCache implements CacheHandler { ...@@ -241,6 +268,13 @@ export default class FetchCache implements CacheHandler {
) { ) {
if (!fetchCache) return if (!fetchCache) return
if (Date.now() < rateLimitedUntil) {
if (this.debug) {
console.log('rate limited')
}
return
}
memoryCache?.set(key, { memoryCache?.set(key, {
value: data, value: data,
lastModified: Date.now(), lastModified: Date.now(),
...@@ -290,6 +324,11 @@ export default class FetchCache implements CacheHandler { ...@@ -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) { if (!res.ok) {
this.debug && console.log(await res.text()) this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`) throw new Error(`invalid response ${res.status}`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment