Hey guys, so I have an issue with a webhook from an external service, the problem is that sometimes I get duplicated events that hit the endpoint at the exact same time, causing the updates that I need to be duplicated.
One possible fix I thought of is to create a queue, and do a first in first out (FIFO).
The problem is that when I deploy it to vercel it doesnt work, the approach I took is to create a variable that will hold the array with requests:
const queue: { req: NextApiRequest; res: NextApiResponse }[] = []
let isProcessing = false
export async function handleApiFunction (req: NextApiRequest, res: NextApiResponse) {
queue.push({ req, res })
if (!isProcessing) {
console.log({ isProcessing })
await processQueue()
}
}
And for some reason this locally seemed to work, but not when deployed to Vercel.
The reason I suppose is that each call of that function is "stateless" in a way, and consecutive calls dont really have the same queue array.
The question then becomes, how can I solve that problem within the realm of Nextjs/Vercel, or do I have to look for other solutions?