Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rate limiter #317

Merged
merged 4 commits into from
Nov 7, 2022
Merged

Conversation

thib3113
Copy link
Contributor

@thib3113 thib3113 commented Nov 5, 2022

This PR will allow to create an async rate limiter .

This allow to use an async rate limiter (for example with redis, to use a centralized store).

Also, I update some typings / exporting some more types .

Also, in the same time, I update the dev dependencies, repair a test (force LF ending, else the req.body test fail on windows) .

To test this code, I create this rateLimiter that will use my broker cacher (or memory store)

import { ServiceBroker } from 'moleculer';
import moleculer, { RateLimitSettings, RateLimitStore } from 'moleculer-web';

export abstract class RateLimiterStore implements RateLimitStore {
    private readonly keyPrefix = 'rate-limiter';
    private broker: ServiceBroker;
    private memoryStore: RateLimitStore;
    private readonly clearPeriod: number;
    private opts: RateLimitSettings;
    public resetTime: number;

    public constructor(clearPeriod: number, opts: RateLimitSettings, broker: ServiceBroker) {
        this.broker = broker;
        this.resetTime = Date.now() + clearPeriod;
        this.clearPeriod = clearPeriod;
        this.opts = opts;

        this.memoryStore = new moleculer.RateLimitStores.MemoryStore(clearPeriod, opts);
    }

    /**
     * Increment the counter by key
     */
    async inc(key: string): Promise<number> {
        this.resetTime = Date.now() + this.clearPeriod;
        if (this.broker.cacher) {
            let counter = Number(await this.broker.cacher.get(this.keyPrefix + key)) || 0;
            counter += 1;
            await this.broker.cacher.set(this.keyPrefix + key, counter, this.clearPeriod / 1000);
            return counter;
        }

        return this.memoryStore.inc(key);
    }
}

@thib3113 thib3113 changed the title [draft] Improve rate limiter Improve rate limiter Nov 5, 2022
Copy link
Member

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't upgrade dependencies because it can cause breaking changes (E.g. jest 29 doesn't support Node 10, but the current moleculer-web version should support it.)

@thib3113
Copy link
Contributor Author

thib3113 commented Nov 6, 2022

Please don't upgrade dependencies because it can cause breaking changes (E.g. jest 29 doesn't support Node 10, but the current moleculer-web version should support it.)

npm failed to resolve dependencies, thats why I updated them .
But, it seems that updating eslint-plugin-promise is enough ( [email protected] ask for eslint@^7.0.0 )

@thib3113 thib3113 requested a review from icebob November 6, 2022 21:14
Copy link
Member

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@icebob icebob merged commit 9275038 into moleculerjs:master Nov 7, 2022
@thib3113 thib3113 deleted the async-rate-limiter branch November 7, 2022 19:50
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.

2 participants