Polling in Workers

Since having a streaming response remain open for quite sometime, I started looking into how long TCP responses will stay open and how long a worker will stay “alive”.

This gave me the idea to use workers for “old school” polling.

i.e. some serverless form of a short lived connection, < 5 min, that polls your origin server and returns to the client only if the response from the origin satisfies some criteria.

Here’s what I found:

Successfully managed 4 minute delayed responses, checking origin every 5s, 48 times (subrequest limit is 50). There is no longer a limit that subrequests must be sent in the first 15s of the original worker request (this is in the old documentation, but clearly isn’t true and not in the new docs).

Anyone else tried something like this? Long response times, etc?


const poll = async(event) => {
    const delay = 5000
	const count = 48
        //not sure if this is neccessary but it's "advised" in the docs to keep the worker alive until this promise resolves
	event.waitUntil(new Promise((resolve) => setTimeout(resolve, delay * count)))
	let results
	for (let i = 0; i < count; i++) {
		console.log('script waiting for', delay)
		await new Promise((resolve) => setTimeout(resolve, delay))
		const res = fetch(...originServerRequestURL...).then((res) => res.json())
		results = JSON.parse(res.results)
        if (requests.length > 0) break; //found something, return it
		console.log(delay + delay * i)
	return new Response(JSON.stringify(results))

Open Questions:

  1. Using the worker editor, you can’t respond after about 1-1.5 min
  2. Most browsers will have a TCP connection limit of 5 min (needs a bit more research, can you send a keep alive?)
  3. Unsure if workers can guarantee they will be always be alive for the whole period, the FAQ is not clear

Bumping, curious on the open questions as well.