Skip to content

Commit 25b4b03

Browse files
committed
Batch the URL deletion
1 parent 536a741 commit 25b4b03

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

workers/handler/src/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async function cachePurgeTag(batch: MessageBatch, env: Env) {
105105

106106
// Re-queue all of the tags as URLs.
107107
// sendBatch only allows for a maximum of 100 messages.
108-
const promises: ReturnType<typeof env.CACHE_PURGE_URL.sendBatch>[] = [];
108+
const promises: ReturnType<typeof env.DB.batch>[] = [];
109109
for (const urlChunk of chunks(results, 100)) {
110110
promises.push(
111111
env.CACHE_PURGE_URL.sendBatch(
@@ -118,23 +118,22 @@ async function cachePurgeTag(batch: MessageBatch, env: Env) {
118118
contentType: "json",
119119
}),
120120
),
121-
),
121+
).then(() => {
122+
const ids = urlChunk.map<string>(({ id }) => id);
123+
return env.DB.batch([
124+
env.DB.prepare(
125+
`DELETE FROM tag WHERE url IN (${ids.map(() => "?").join(", ")})`,
126+
).bind(...ids),
127+
env.DB.prepare(
128+
`DELETE FROM url WHERE id IN (${ids.map(() => "?").join(", ")})`,
129+
).bind(...ids),
130+
]);
131+
}),
122132
);
123133
}
124134

125135
await Promise.all(promises);
126136

127-
const ids = results.map<string>(({ id }) => id);
128-
129-
await env.DB.batch([
130-
env.DB.prepare(
131-
`DELETE FROM tag WHERE url IN (${ids.map(() => "?").join(", ")})`,
132-
).bind(...ids),
133-
env.DB.prepare(
134-
`DELETE FROM url WHERE id IN (${ids.map(() => "?").join(", ")})`,
135-
).bind(...ids),
136-
]);
137-
138137
for (const msg of msgs) {
139138
msg.ack();
140139
}

workers/handler/wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ queue = "cache-capture"
9292

9393
[[queues.consumers]]
9494
queue = "cache-purge-tag"
95-
max_batch_size = 90 # The queue limit is 100, but the D1 binding limit is 100 and sometimes we need a few more params.
95+
max_batch_size = 95 # The queue limit is 100, but the D1 binding limit is 100 and sometimes we need a few more (i.e. for the zone).
9696

9797
[[queues.consumers]]
9898
queue = "cache-purge-url"

0 commit comments

Comments
 (0)