This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ratelimit config from source (#1714)
* feat: extra ratelimit config from source * feat: refactor to a more general config structure * chore: address PR comments * chore: ratelimit -> rate_limit
- Loading branch information
Showing
8 changed files
with
184 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export enum RateLimitAlgorithm { | ||
SlidingWindow = 'sliding_window', | ||
} | ||
|
||
export enum RateLimitAggregator { | ||
Domain = 'domain', | ||
IP = 'ip', | ||
} | ||
|
||
export enum RateLimitAction { | ||
Limit = 'rate_limit', | ||
Rewrite = 'rewrite', | ||
} | ||
|
||
interface SlidingWindow { | ||
windowLimit: number | ||
windowSize: number | ||
} | ||
|
||
export type RewriteActionConfig = SlidingWindow & { | ||
to: string | ||
} | ||
|
||
interface RateLimitConfig { | ||
action?: RateLimitAction | ||
aggregateBy?: RateLimitAggregator | RateLimitAggregator[] | ||
algorithm?: RateLimitAlgorithm | ||
} | ||
|
||
export type RateLimit = RateLimitConfig & (SlidingWindow | RewriteActionConfig) |
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,14 @@ | ||
import { Config, Context } from "@netlify/functions"; | ||
|
||
export default async (req: Request, context: Context) => { | ||
return new Response(`Something!`); | ||
}; | ||
|
||
export const config: Config = { | ||
path: "/ratelimited", | ||
rateLimit: { | ||
windowLimit: 60, | ||
windowSize: 50, | ||
aggregateBy: ["ip", "domain"], | ||
} | ||
}; |
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,16 @@ | ||
import { Config, Context } from "@netlify/functions"; | ||
|
||
export default async (req: Request, context: Context) => { | ||
return new Response(`Something!`); | ||
}; | ||
|
||
export const config: Config = { | ||
path: "/rewrite", | ||
rateLimit: { | ||
action: "rewrite", | ||
to: "/rewritten", | ||
windowSize: 20, | ||
windowLimit: 200, | ||
aggregateBy: ["ip", "domain"], | ||
} | ||
}; |
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
f947eb3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results