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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
28 changes: 20 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ declare module "moleculer-web" {
Errors,
LogLevels,
Service,
ServiceBroker,
ServiceSchema,
} from "moleculer";
import { IncomingMessage, ServerResponse } from "http";
import {IncomingMessage, ServerResponse} from "http";

// RateLimit
type generateRateLimitKey = (req: IncomingMessage)=> string
export type generateRateLimitKey = (req: IncomingMessage)=> string

interface RateLimit {
export interface RateLimitSettings {
/**
* How long to keep record of requests in memory (in milliseconds).
* @default 60000 (1 min)
Expand Down Expand Up @@ -44,11 +45,22 @@ declare module "moleculer-web" {
* @default MemoryStore
* @see https://moleculer.services/docs/0.14/moleculer-web.html#Custom-Store-example
*/
StoreFactory?: MemoryStore
StoreFactory?: typeof RateLimitStore
}

class MemoryStore {
constructor(clearPeriod: number, opts: RateLimit)
export abstract class RateLimitStore {
public resetTime: number;
public constructor(clearPeriod: number, opts?: RateLimitSettings, broker?: ServiceBroker)
inc(key: string): number | Promise<number>
}

interface RateLimitStores {
MemoryStore: typeof MemoryStore
}

class MemoryStore extends RateLimitStore {

constructor(clearPeriod: number, opts?: RateLimitSettings, broker?: ServiceBroker)

/**
* Increment the counter by key
Expand Down Expand Up @@ -414,7 +426,7 @@ declare module "moleculer-web" {
* The Moleculer-Web has a built-in rate limiter with a memory store.
* @see https://moleculer.services/docs/0.14/moleculer-web.html#Rate-limiter
*/
rateLimit?: RateLimit
rateLimit?: RateLimitSettings
/**
* It supports Connect-like middlewares in global-level, route-level & alias-level.<br>
* Signature: function (req, res, next) {...}.<br>
Expand Down Expand Up @@ -671,6 +683,6 @@ declare module "moleculer-web" {
$service: Service;
}

const ApiGatewayService: ServiceSchema & { Errors: ApiGatewayErrors, IncomingRequest: IncomingRequest, GatewayResponse: GatewayResponse };
const ApiGatewayService: ServiceSchema & { Errors: ApiGatewayErrors, IncomingRequest: IncomingRequest, GatewayResponse: GatewayResponse, RateLimitStores: RateLimitStores };
export default ApiGatewayService;
}
Loading