Skip to content

Commit

Permalink
fix(worker): Remove rate-limiting (#14)
Browse files Browse the repository at this point in the history
Trying to rate-limit using global state, in a stateless context such as a worker,
is obviously not going to work.

There is an official Rate Limiting API in beta:
https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/

However, that requires lots of setup. For now, let's remove the broken code.
  • Loading branch information
meyfa authored Dec 1, 2024
1 parent eda6419 commit cd9aca8
Showing 1 changed file with 0 additions and 15 deletions.
15 changes: 0 additions & 15 deletions worker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ function validateEnvironment(env: Record<string, string | undefined>): Config {
}

const API = new URL('https://api.cloudflare.com/client/v4/')
const RATE_LIMIT_MS = 30_000

let lastRequest = 0

export default {
async fetch(req: Request, env: Record<string, string | undefined>, ctx: unknown): Promise<Response> {
Expand All @@ -42,18 +39,6 @@ export default {
return new Response('Unauthorized', { status: 401 })
}

// Throttle updates to not overload the Cloudflare API
const now = Date.now()
if (now - lastRequest < RATE_LIMIT_MS) {
return new Response('Too Many Requests', {
status: 429,
headers: {
'Retry-After': Math.ceil((lastRequest + RATE_LIMIT_MS - now) / 1000).toString()
}
})
}
lastRequest = now

// TOOD support IPv6
if (!/(\d+\.){3}(\d+)/.test(remoteAddress)) {
return new Response('Bad Request', { status: 400 })
Expand Down

0 comments on commit cd9aca8

Please sign in to comment.