Coder Social home page Coder Social logo

Comments (8)

vworld avatar vworld commented on May 28, 2024 1

@animir Sounds good - will check both of these. Thanks again for all the help and time.

from node-rate-limiter-flexible.

animir avatar animir commented on May 28, 2024 1

@vworld Yes, until you change number of points for the blocked key, it will be blocked.

from node-rate-limiter-flexible.

vworld avatar vworld commented on May 28, 2024 1

Sounds awesome, thank you for the support.

from node-rate-limiter-flexible.

animir avatar animir commented on May 28, 2024

@vworld Hi, thank you!

Unfortunately, there is no way to achieve that with a single consume call — it overwrites the duration for the key, so it will expire based on default limiter option.
You can call get(key) to check if key is blocked forever and if so, don't call consume.

from node-rate-limiter-flexible.

vworld avatar vworld commented on May 28, 2024

@animir Thank you for the quick response.

I was thinking about using blacklist but the docs say Blacklisted keys are blocked on code level not in store/memory. So I assume this will not work in a distributed system, where if a block is set in one process/node/machine it is honored by all processes/node/machine. Is that accurate?

And if we use RedisRateLimiter to block a key, the same will be propagated to redis and the block will be available across all machines that use the same key and same redis server. Is that correct?

from node-rate-limiter-flexible.

animir avatar animir commented on May 28, 2024

@vworld Yes, array of blocked keys stored in blackList is accessible for the current single process only. You could store blocked keys in any store and check it with isBlackListed function a way suitable for you. It is more work to store list of blocked keys somewhere else in database, but you could easily use Redis for that.

As of RateLimiterRedis, yes. Blocked keys are blocked on the Redis server level making it blocked for any process in distributed environment.

from node-rate-limiter-flexible.

animir avatar animir commented on May 28, 2024

@vworld Just another idea in addition to the answer for the first question. To store list of blocked keys you could easily store it with another instance of RateLimiterFlexible with options {points: 1, duration: 0}. If key should be blocked you can use set method, then check it with get method inside isBlackListed function of RLWrapperBlackAndWhite. But it is still one more request to the store, which shouldn't any problem though.

from node-rate-limiter-flexible.

vworld avatar vworld commented on May 28, 2024

Just a heads up - Just like you suggested, I ended up using get to check if the key is blocked before using consume.
The assumption here is that once a key is blocked with secDuration=0 the key is blocked forever with msBeforeNext = -1 until we try to consume the key again.

So something like this works for me while maintaining availability of the block thorough out the distributed setup. This basically goes with the paradigm of my codes that expect an error whenever a key is blocked.

    /**
     * Consumes the specified amount of points for a key
     * @throws {RateLimiterRes} - msBeforeNext > -1 if not blocked and msBeforeNext = -1 if key is blocked
     * @param {string} ip
     * @param {number} pointsToConsume
     * @returns {Promise<void>}
     * @private
     */
    private async _attemptToConsumePoints(ip: string, pointsToConsume: number = 1){
        const rLKey = this._getLimiterKey(ip);

        // check if key is blocked
        const res = await this.rateLimiter.get(rLKey);
        if (res && res.msBeforeNext === -1) {
            throw res;
        }
        await this.rateLimiter.consume(rLKey, pointsToConsume);
    }

While I have not tested the assumptions fully but initial tests look promising.

@animir is it safe to assume a blocked key will always return msBeforeNext = -1 until we consume

from node-rate-limiter-flexible.

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.