-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ratelimiting, add leaky bucket ratelimiter plugin
- Loading branch information
1 parent
c9e7632
commit adf6bdb
Showing
12 changed files
with
119 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export abstract class BaseRatelimiterPlugin<R, S> { | ||
abstract get name(): string; | ||
|
||
abstract action(request: R, response: S, secondsOverdue: number): S | void; | ||
|
||
abstract get ttl(): number; | ||
|
||
abstract get capacity(): number; | ||
} |
5 changes: 5 additions & 0 deletions
5
...ratelimiter/plugins/leaky-bucket/interfaces/leaky-bucket-ratelimiter-options.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface LeakyBucketRatelimiterPluginOptions<R, S> { | ||
capacity: number; | ||
rate: number; | ||
action: (request: R, response: S, secondsOverdue: number) => void; | ||
} |
32 changes: 32 additions & 0 deletions
32
src/ratelimiter/plugins/leaky-bucket/leaky-bucket-ratelimiter.plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { LeakyBucketRatelimiterPluginOptions } from './interfaces/leaky-bucket-ratelimiter-options.interface'; | ||
import { BaseRatelimiterPlugin } from '../base-adapter'; | ||
|
||
export class LeakyBucketRatelimiterPlugin<R, S> implements BaseRatelimiterPlugin<R, S> { | ||
#capacity: number; | ||
|
||
#rate: number; | ||
|
||
#action: typeof this.action; | ||
|
||
constructor(options: LeakyBucketRatelimiterPluginOptions<R, S>) { | ||
this.#capacity = options.capacity; | ||
this.#rate = options.rate; | ||
this.#action = options.action; | ||
} | ||
|
||
get name(): string { | ||
return this.constructor.name; | ||
} | ||
|
||
get ttl(): number { | ||
return this.#capacity * 1000; | ||
} | ||
|
||
get capacity(): number { | ||
return this.#capacity; | ||
} | ||
|
||
action(request: R, response: S, secondsOverdue: number): void | S { | ||
return this.#action(request, response, secondsOverdue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { RatelimiterService } from './ratelimiter.service'; | ||
|
||
export const ratelimiterServiceInstance = new RatelimiterService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Cache } from 'cache-manager'; | ||
|
||
import { BaseRatelimiterPlugin } from './plugins/base-adapter'; | ||
import { BasePluginService } from '../base-plugin/base-plugin.service'; | ||
|
||
export class RatelimiterService<R, S> implements BasePluginService<R, S> { | ||
static cacheKeyPrefix = 'leaky-bucket-ratelimiter'; | ||
|
||
#buildCacheKey(ipAddress: string): string { | ||
return `${RatelimiterService.cacheKeyPrefix}-${ipAddress}`; | ||
} | ||
|
||
async execute( | ||
request: R, | ||
response: S, | ||
plugin: BaseRatelimiterPlugin<R, S>, | ||
ipAddress: string, | ||
store: Cache, | ||
): Promise<void | S> { | ||
const now = Date.now(); | ||
const cacheKey = this.#buildCacheKey(ipAddress); | ||
const cached = await store.get<string>(cacheKey); | ||
|
||
if (cached) { | ||
const parsed = JSON.parse(cached) as number[]; | ||
const leaked = parsed.filter((timestamp) => timestamp < now + plugin.ttl); | ||
|
||
if (leaked.length > plugin.capacity) { | ||
plugin.action(request, response, leaked.length - plugin.capacity); | ||
} | ||
|
||
await this.#save(store, cacheKey, [...leaked, now], plugin.ttl); | ||
} else { | ||
await this.#save(store, cacheKey, [now], plugin.ttl); | ||
} | ||
} | ||
|
||
async #save(store: Cache, cacheKey: string, timestamps: number[], ttl: number): Promise<void> { | ||
await store.set(cacheKey, JSON.stringify(timestamps), ttl); | ||
} | ||
} |