GithubHelp home page GithubHelp logo

honzabit / durable-limiter Goto Github PK

View Code? Open in Web Editor NEW
48.0 3.0 5.0 964 KB

A rate-limiter using Durable Objects on CF Workers that actually doesn't rate limit anything.

License: MIT License

JavaScript 6.44% TypeScript 93.56%
cloudflare cloudflare-workers durable-objects rate-limiting

durable-limiter's People

Contributors

dependabot[bot] avatar honzabit avatar lauragift21 avatar mrbbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

durable-limiter's Issues

cache rate-limited responses from DO

Responses can be stored/returned from the cache when the fixed window algorithm is used. Any rate-limited response can be safely stored in and returned from the cache, until the timestamp returned in the resets property.

On the other hand, the sliding window algorithm is not so cache-friendly, but I guess an approximation of cache time can possibly be computed here too using the distance of rate from limit and the interval.

For your durable object alarm are you calculating ts_less_than correctly?

I maybe completely misunderstanding this so forgive me if this is the case, but say we take:

ts_less_than: Math.floor(now / 1000 / parseInt(interval)) - parseInt(interval) * 2

This is number of intervals - the interval seconds * 2.

Shouldn't you be doing number of intervals -2 intervals aka:

Math.floor(now / 1000 / parseInt(interval)) - 2

cleanup old keys from DO storage

Old keys need to be cleaned up to avoid unneeded storage charges.

The keys need to be restructured to also include the interval, and together with the window, stale keys can be identified and removed.

cost effectiveness

There are scenarios where this code is more cost-effective than CF's rate limiter and other scenarios where it could kill your budget.

Maybe using some on-the-fly calculations (like the cost calculator) on the traffic patterns, we could identify these scenarios effectively and integrate with the CF API to push the rate-limit rule closer to the edge. An alarm also could be used to trigger the rule's deletion every 1h, as if needed it would be automatically re-provisioned.

Caching breaks the rate limiting

I'm pretty sure the caching expiration logic used is incorrect and causes requests to not be counted or rate limited

I had to remove the caching for it to actually work once deployed. I don't see how you can cache anything and count responses to be rate limited at the same time but I could be misunderstanding it ๐Ÿค”

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.