Commit 16ad3683 authored by JJ Kasper's avatar JJ Kasper

Ensure we chunk revalidate tag requests (#70189)

This ensures we chunk revalidate tag requests so we don't send too many
at once.

x-ref: [slack
thread](https://vercel.slack.com/archives/C03UR7US95F/p1726585656900449?thread_ts=1726049244.204009&cid=C03UR7US95F)

---------
Co-authored-by: 's avatarKelly Davis <kldavis4@users.noreply.github.com>
parent 77910c84
...@@ -176,9 +176,11 @@ export default class FetchCache implements CacheHandler { ...@@ -176,9 +176,11 @@ export default class FetchCache implements CacheHandler {
return return
} }
for (let i = 0; i < Math.ceil(tags.length / 64); i++) {
const currentTags = tags.slice(i * 64, i * 64 + 64)
try { try {
const res = await fetchRetryWithTimeout( const res = await fetchRetryWithTimeout(
`${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${tags `${this.cacheEndpoint}/v1/suspense-cache/revalidate?tags=${currentTags
.map((tag) => encodeURIComponent(tag)) .map((tag) => encodeURIComponent(tag))
.join(',')}`, .join(',')}`,
{ {
...@@ -198,7 +200,8 @@ export default class FetchCache implements CacheHandler { ...@@ -198,7 +200,8 @@ export default class FetchCache implements CacheHandler {
throw new Error(`Request failed with status ${res.status}.`) throw new Error(`Request failed with status ${res.status}.`)
} }
} catch (err) { } catch (err) {
console.warn(`Failed to revalidate tag ${tags}`, err) console.warn(`Failed to revalidate tag`, currentTags, err)
}
} }
} }
......
import { revalidateTag } from 'next/cache'
import { NextRequest, NextResponse } from 'next/server'
export const dynamic = 'force-dynamic'
export function GET(req: NextRequest) {
for (let i = 0; i < 130; i++) {
revalidateTag(`thankyounext-${i}`)
}
return NextResponse.json({ done: true })
}
...@@ -18,6 +18,7 @@ describe('fetch-cache', () => { ...@@ -18,6 +18,7 @@ describe('fetch-cache', () => {
let nextInstance: any let nextInstance: any
let fetchGetReqIndex = 0 let fetchGetReqIndex = 0
let revalidateReqIndex = 0 let revalidateReqIndex = 0
let revalidateReqShouldTimeout = false
let fetchGetShouldError = false let fetchGetShouldError = false
let fetchCacheServer: http.Server let fetchCacheServer: http.Server
let fetchCacheRequests: Array<{ let fetchCacheRequests: Array<{
...@@ -101,6 +102,7 @@ describe('fetch-cache', () => { ...@@ -101,6 +102,7 @@ describe('fetch-cache', () => {
fetchCacheRequests = [] fetchCacheRequests = []
storeCacheItems = false storeCacheItems = false
fetchGetShouldError = false fetchGetShouldError = false
revalidateReqShouldTimeout = false
fetchCacheServer = http.createServer(async (req, res) => { fetchCacheServer = http.createServer(async (req, res) => {
console.log(`fetch cache request ${req.url} ${req.method}`, req.headers) console.log(`fetch cache request ${req.url} ${req.method}`, req.headers)
const parsedUrl = new URL(req.url || '/', 'http://n') const parsedUrl = new URL(req.url || '/', 'http://n')
...@@ -114,7 +116,8 @@ describe('fetch-cache', () => { ...@@ -114,7 +116,8 @@ describe('fetch-cache', () => {
if (parsedUrl.pathname === '/v1/suspense-cache/revalidate') { if (parsedUrl.pathname === '/v1/suspense-cache/revalidate') {
revalidateReqIndex += 1 revalidateReqIndex += 1
// timeout unless it's 3rd retry // timeout unless it's 3rd retry
const shouldTimeout = revalidateReqIndex % 3 !== 0 const shouldTimeout =
revalidateReqShouldTimeout && revalidateReqIndex % 3 !== 0
if (shouldTimeout) { if (shouldTimeout) {
console.log('not responding for', req.url, { revalidateReqIndex }) console.log('not responding for', req.url, { revalidateReqIndex })
...@@ -220,6 +223,8 @@ describe('fetch-cache', () => { ...@@ -220,6 +223,8 @@ describe('fetch-cache', () => {
}) })
it('should retry 3 times when revalidate times out', async () => { it('should retry 3 times when revalidate times out', async () => {
revalidateReqShouldTimeout = true
try {
await fetchViaHTTP(appPort, '/api/revalidate') await fetchViaHTTP(appPort, '/api/revalidate')
await retry(() => { await retry(() => {
...@@ -227,6 +232,20 @@ describe('fetch-cache', () => { ...@@ -227,6 +232,20 @@ describe('fetch-cache', () => {
}) })
expect(cliOuptut).not.toContain('Failed to revalidate') expect(cliOuptut).not.toContain('Failed to revalidate')
expect(cliOuptut).not.toContain('Error') expect(cliOuptut).not.toContain('Error')
} finally {
revalidateReqShouldTimeout = false
}
})
it('should batch revalidate tag requests if > 64', async () => {
const revalidateReqIndexStart = revalidateReqIndex
await fetchViaHTTP(appPort, '/api/revalidate-alot')
await retry(() => {
expect(revalidateReqIndex).toBe(revalidateReqIndexStart + 3)
})
expect(cliOuptut).not.toContain('Failed to revalidate')
expect(cliOuptut).not.toContain('Error')
}) })
it('should not retry for failed fetch-cache GET', async () => { it('should not retry for failed fetch-cache GET', async () => {
......
...@@ -14633,7 +14633,8 @@ ...@@ -14633,7 +14633,8 @@
"fetch-cache should have correct fetchUrl field for fetches and unstable_cache", "fetch-cache should have correct fetchUrl field for fetches and unstable_cache",
"fetch-cache should not retry for failed fetch-cache GET", "fetch-cache should not retry for failed fetch-cache GET",
"fetch-cache should retry 3 times when revalidate times out", "fetch-cache should retry 3 times when revalidate times out",
"fetch-cache should update cache TTL even if cache data does not change" "fetch-cache should update cache TTL even if cache data does not change",
"fetch-cache should batch revalidate tag requests if > 64"
], ],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
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