How is CPU time calculated?

caching

#1

Hi,

I am sorry if this has been asked before is a very obvious question, but I am currently thinking about using workers and wanted to know if my scenario could work before actually starting to develop.

I have seen many examples where resources are fetched from a foreign server, and after the resources have been fetched, a result is returned (e.g., this example). Now I am wondering how the CPU time is calculated, is the “timer stopped” when fetching a resource (e.g., fetch('https://api.coinbase.com/v2/prices/BTC-USD/spot', init)) or is the timer started when calling the worker and stopped once the worker is done?

This would mean that fetching foreign resources is not possible on the free or pro plan, and only very limited on the business plan? But then, fetching network resources is not really “CPU” time.

A brief overview what I wanted to do:

  • Fetch a medium size resource (<1mb), (e.g., example.com/1.png)
  • If the request results in a 200, this would be returned.
  • If the request results in a 404, the worker would request a fallback (e.g., fallback.com/1.png) and return this.

As far as I understand workers, this would be a good use case for workers?

Cheers,
Robin


#2

From what I’ve experienced, CPU time is not affected by the time it takes to fetch a resource.

In some instances where I was using fetch, it would take a few seconds if a website is slow, so the worker would take a few seconds to return a response. This is of course well above their 5-50 millisecond limit, but it still worked fine.

So in this case, I think your use case is perfectly fine.

Hopefully someone who’s more familiar with workers or Cloudflare staff can provide a more definitive answer.


#3

Do not have the time to look more into it, but CPU time is exactly what it says, the time spent by the CPU doing stuff. Request wait times are not counted (there cannot be new requests after 15s real time, but established ones will continue).

It could be useful to see the code, maybe there is some quirk that is forcing some calculations that is not needed.


#4

Thanks for your kind feedback. I think this answers my question, I was just kind of concerned and before I start digging deeper I wanted to make sure that it would at least work in theory :slight_smile:.