Skip to content

fix(worker): cut CPU per request below the free plan limit - #100

Open
ajithakdev wants to merge 1 commit into
mainfrom
fix/worker-cpu
Open

ajithakdev wants to merge 1 commit into
mainfrom
fix/worker-cpu

Conversation

@ajithakdev

Copy link
Copy Markdown
Owner

The worker was using more CPU per request than the Cloudflare free plan allows (10 ms). Over the last 30 days, the 99th percentile reached 28 to 77 ms. Nothing has failed yet at today's traffic, but under load Cloudflare can cut those requests off mid-test.

Two causes, both fixed:

  • Downloads streamed 64 KB chunks, so a 25 MB file meant 400 trips through JavaScript. They now stream 4 MiB chunks (7 trips). The last chunk is trimmed, so 1, 5, 10 and 25 MB all come out exact; a new test checks every allowed size.
  • Uploads were read chunk by chunk in JavaScript just to count bytes. A body with a declared Content-Length is now refused up front if it's over the 16 MB cap, and otherwise read in one native arrayBuffer() call. Bodies without a length (chunked) still go through the old counting loop. The extension always sends a length, and never more than 8 MB.

Measured on Cloudflare with wrangler versions upload preview versions (production untouched), using the same 25 MB downloads and 5 MB uploads:

Version CPU p50 CPU p90 CPU p99
Production (d6779235) 16.1 ms 20.4 ms 20.4 ms
1 MiB chunks 6.6 ms 13.0 ms 13.0 ms
4 MiB chunks, old upload read 4.0 ms 26.6 ms 34.2 ms
4 MiB chunks + native upload read (afe4630d) 5.6 ms 7.2 ms 12.6 ms

Also: CI now lints and type-checks the worker, @meterx/shared and @meterx/reports (#86 asked for this). All three pass with no new findings.

Deploys with the next release (cd services/worker && npx wrangler deploy). Nothing in the API changes, so the extension needs no update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant