Git Product home page Git Product logo

Comments (7)

Jblew avatar Jblew commented on July 26, 2024 1

@robertvorthman — please update to [email protected].
Changes:

  • The function isQuotaAlreadyExceeded is introduced
  • The function isQuotaExceeded is renamed to isQuotaExceededOrRecordUsage (the old function is available but marked as deprecated in jsdoc)
  • The function rejectOnQuotaExceeded is renamed to rejectOnQuotaExceededOrRecordUsage (the old function is available but marked as deprecated in jsdoc)
  • Code coverage has been increased to 100% :D

Please check and tell me if it works as expected. I've already deployed the new version to my functions.

from firebase-functions-rate-limiter.

Jblew avatar Jblew commented on July 26, 2024

Hi Robert!
You are right, this would be a useful feature. I think, I will implement it today. Stay tuned, I'll mention you here when this is finished.

from firebase-functions-rate-limiter.

robertvorthman avatar robertvorthman commented on July 26, 2024

Thanks. I implemented your library on several of my firebase cloud functions yesterday and it works great.

If you are separating the logic for checking quota and increment usage, it would also be great to have a function that just increments the usage without checking quota. That would reduce firebase reads, right? For example, near the end of my previous code example, if the user authenticates with the wrong password, I should count 1 usage against their quota but there is no need to check if they have exceeded the quota since the cloud function already performed all the work by that point.

from firebase-functions-rate-limiter.

Jblew avatar Jblew commented on July 26, 2024

The logic between read and write is coupled on a purpose of atomicity and it is coupled on the level of PersistenceProvider interface. In both backends get-and-update is performed inside a database-provided transaction mechanism.

Also, the data model that is used for storing usages requires a read before update, so there would be no difference between isQuotaExceeded and incrementUsage.

/+ I like the name checkQuotaAlreadyExceeded. I think it is meaningful enough :)

from firebase-functions-rate-limiter.

robertvorthman avatar robertvorthman commented on July 26, 2024

Thanks for the quick work. The new functions are working great. My app UI is more responsive because the rate limited cloud function replies in 60ms if isQuotaAlreadyExceeded == true, instead of doing a bunch of unnecessary work and replying in 600ms.

It would be awesome if the numOfRecentUsages variable in the isQuotaExceeded private method was available so that I could present the user with warnings like "you have 1 attempt remaining..."

I suppose I could just talk directly to Firebase, and I don't need this feature right now, but your library would be more mature with a way to reset usage back to zero for a given identifier.

from firebase-functions-rate-limiter.

Jblew avatar Jblew commented on July 26, 2024

It is fairly easy to be done. The easiest way to do this it to replace isQuotaExceededOrRecordCall in GenericRateLimiter class with getNumOfCallsLeftAndRecordCall (resultHolder should hold number instead of boolean) — the value is generated in the runTransactionForAnswer method. Of course tests should be updated and test if the number returned by the getNumOfCallsLeftAndRecordCall is correct. Then the isQuotaExceededOrRecordUsage in the FirebaseFunctionsRateLimiter class should do the check if the number provided by getNumOfCallsLeftAndRecordCall is equal zero, and a separate method that is a facade for getNumOfCallsLeftAndRecordCall should be added.

If you have time you could create a PR, I would guide you and help you with any problems. If not, I will maybe do it in a few days.

from firebase-functions-rate-limiter.

Jblew avatar Jblew commented on July 26, 2024

And — please open a separate issue for this :)

from firebase-functions-rate-limiter.

Related Issues (20)

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.