Zack Bloom
Zack Bloom

Reputation: 8417

Can I use Cloudflare Workers to batch requests?

I would like to log requests made to my site which travel through Cloudflare. I think I can use Cloudflare Workers to do this, but I don't want to DoS my logging service by making a request to it with every request made to my site. Can I have the Worker bundle the logging reports up and only make a request to me with every 10 or 100?

Upvotes: 2

Views: 680

Answers (1)

Zack Bloom
Zack Bloom

Reputation: 8417

To answer my own question, yes you can do this! You have to use event.waitUntil to add a task which will run after the original request has been responded to. The one bug with this is if the Worker script gets evicted from memory, we will lose our batchedRequests, but that doesn’t seem to happen very often.

addEventListener('fetch', event => {
  event.respondWith(fetchAndLog(event))
})

let batchedRequests = []

function sendRequests(){
  let promises = []
  for (var i=batchedRequests.length; i--;){
    promises.push(fetch(...batchedRequests[i]))
  }

  batchedRequests = []

  return Promise.all(promises)
}
/**
 * Fetch and log a given request object, uploading a logging request only when five are stored
 * @param {Request} request
 */
async function fetchAndLog(event) {
  batchedRequests.push(["https://hookb.in/Kb88Ybq8", {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      timestamp: +new Date
    })
  }])

  if (batchedRequests.length > 4){
    event.waitUntil(sendRequests())
  }

  return fetch(event.request)
}

Upvotes: 2

Related Questions