@upstash/ratelimit 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,10 @@
1
- # Upstash Redis
1
+ # Upstash Ratelimit
2
2
 
3
3
  An HTTP/REST based Redis client built on top of Upstash REST API.
4
4
  [Upstash REST API](https://docs.upstash.com/features/restapi).
5
5
 
6
6
  [![Tests](https://github.com/upstash/ratelimit/actions/workflows/tests.yaml/badge.svg)](https://github.com/upstash/ratelimit/actions/workflows/tests.yaml)
7
7
  ![npm (scoped)](https://img.shields.io/npm/v/@upstash/ratelimit)
8
- ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@upstash/ratelimit)
9
8
 
10
9
  It is the only connectionless (HTTP based) ratelimiter and designed for:
11
10
 
@@ -17,6 +16,38 @@ It is the only connectionless (HTTP based) ratelimiter and designed for:
17
16
  - WebAssembly
18
17
  - and other environments where HTTP is preferred over TCP.
19
18
 
19
+ <!-- toc -->
20
+
21
+ - [Quick Start](#quick-start)
22
+ - [Install](#install)
23
+ - [npm](#npm)
24
+ - [Deno](#deno)
25
+ - [Create database](#create-database)
26
+ - [Use it](#use-it)
27
+ - [Block until ready](#block-until-ready)
28
+ - [Globally replicated ratelimiting](#globally-replicated-ratelimiting)
29
+ - [Usage](#usage)
30
+ - [Example](#example)
31
+ - [Ratelimiting algorithms](#ratelimiting-algorithms)
32
+ - [Fixed Window](#fixed-window)
33
+ - [Pros:](#pros)
34
+ - [Cons:](#cons)
35
+ - [Usage:](#usage)
36
+ - [Sliding Window](#sliding-window)
37
+ - [Pros:](#pros-1)
38
+ - [Cons:](#cons-1)
39
+ - [Usage:](#usage-1)
40
+ - [Token Bucket](#token-bucket)
41
+ - [Pros:](#pros-2)
42
+ - [Cons:](#cons-2)
43
+ - [Usage:](#usage-2)
44
+ - [Contributing](#contributing)
45
+ - [Install Deno](#install-deno)
46
+ - [Database](#database)
47
+ - [Running tests](#running-tests)
48
+
49
+ <!-- tocstop -->
50
+
20
51
  ## Quick Start
21
52
 
22
53
  ### Install
@@ -30,7 +61,7 @@ npm install @upstash/ratelimit
30
61
  #### Deno
31
62
 
32
63
  ```ts
33
- import { Redis } from "https://deno.land/x/upstash_ratelimit/mod.ts";
64
+ import { Ratelimit } from "https://deno.land/x/upstash_ratelimit/mod.ts";
34
65
  ```
35
66
 
36
67
  ### Create database
@@ -68,29 +99,50 @@ return "Here you go!";
68
99
 
69
100
  The `limit` method returns some more metadata that might be useful to you:
70
101
 
71
- ```ts
102
+ ````ts
72
103
  export type RatelimitResponse = {
73
104
  /**
74
105
  * Whether the request may pass(true) or exceeded the limit(false)
75
106
  */
76
107
  success: boolean;
77
-
78
108
  /**
79
109
  * Maximum number of requests allowed within a window.
80
110
  */
81
111
  limit: number;
82
-
83
112
  /**
84
113
  * How many requests the user has left within the current window.
85
114
  */
86
115
  remaining: number;
87
-
88
116
  /**
89
117
  * Unix timestamp in milliseconds when the limits are reset.
90
118
  */
91
119
  reset: number;
120
+
121
+ /**
122
+ * For the MultiRegion setup we do some synchronizing in the background, after returning the current limit.
123
+ * In most case you can simply ignore this.
124
+ *
125
+ * On Vercel Edge or Cloudflare workers, you need to explicitely handle the pending Promise like this:
126
+ *
127
+ * **Vercel Edge:**
128
+ * https://nextjs.org/docs/api-reference/next/server#nextfetchevent
129
+ *
130
+ * ```ts
131
+ * const { pending } = await ratelimit.limit("id")
132
+ * event.waitUntil(pending)
133
+ * ```
134
+ *
135
+ * **Cloudflare Worker:**
136
+ * https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker
137
+ *
138
+ * ```ts
139
+ * const { pending } = await ratelimit.limit("id")
140
+ * context.waitUntil(pending)
141
+ * ```
142
+ */
143
+ pending: Promise<unknown>;
92
144
  };
93
- ```
145
+ ````
94
146
 
95
147
  ### Block until ready
96
148
 
@@ -124,6 +176,80 @@ doExpensiveCalculation();
124
176
  return "Here you go!";
125
177
  ```
126
178
 
179
+ ## MultiRegionly replicated ratelimiting
180
+
181
+ Using a single redis instance has the downside of providing low latencies to the
182
+ part of your userbase closest to the deployed db. That's why we also built
183
+ `MultiRegionRatelimit` which replicates the state across multiple redis
184
+ databases as well as offering lower latencies to more of your users.
185
+
186
+ `MultiRegionRatelimit` does this by checking the current limit in the closest db
187
+ and returning immediately. Only afterwards will the state be asynchronously
188
+ replicated to the other datbases leveraging
189
+ [CRDTs](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type). Due
190
+ to the nature of distributed systems, there is no way to guarantee the set
191
+ ratelimit is not exceeded by a small margin. This is the tradeoff for reduced
192
+ global latency.
193
+
194
+ ### Usage
195
+
196
+ The api is the same, except for asking for multiple redis instances:
197
+
198
+ ```ts
199
+ import { MultiRegionRatelimit } from "@upstash/ratelimit"; // for deno: see above
200
+ import { Redis } from "@upstash/redis";
201
+
202
+ // Create a new ratelimiter, that allows 10 requests per 10 seconds
203
+ const ratelimit = new MultiRegionRatelimit({
204
+ redis: [
205
+ new Redis({
206
+ /* auth */
207
+ }),
208
+ new Redis({
209
+ /* auth */
210
+ }),
211
+ new Redis({
212
+ /* auth */
213
+ }),
214
+ ],
215
+ limiter: Ratelimit.slidingWindow(10, "10 s"),
216
+ });
217
+
218
+ // Use a constant string to limit all requests with a single ratelimit
219
+ // Or use a userID, apiKey or ip address for individual limits.
220
+ const identifier = "api";
221
+ const { success } = await ratelimit.limit(identifier);
222
+ ```
223
+
224
+ ### Asynchronous synchronization between databases
225
+
226
+ The MultiRegion setup will do some synchronization between databases after
227
+ returning the current limit. This can lead to problems on Cloudflare Workers and
228
+ therefore Vercel Edge functions, because dangling promises must be taken care
229
+ of:
230
+
231
+ **Vercel Edge:**
232
+ [docs](https://nextjs.org/docs/api-reference/next/server#nextfetchevent)
233
+
234
+ ```ts
235
+ const { pending } = await ratelimit.limit("id");
236
+ event.waitUntil(pending);
237
+ ```
238
+
239
+ **Cloudflare Worker:**
240
+ [docs](https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker)
241
+
242
+ ```ts
243
+ const { pending } = await ratelimit.limit("id");
244
+ context.waitUntil(pending);
245
+ ```
246
+
247
+ ### Example
248
+
249
+ Let's assume you have customers in the US and Europe. In this case you can
250
+ create 2 regional redis databases on [Upastash](https://console.upstash.com) and
251
+ your users will enjoy the latency of whichever db is closest to them.
252
+
127
253
  ## Ratelimiting algorithms
128
254
 
129
255
  We provide different algorithms to use out of the box. Each has pros and cons.
@@ -199,6 +325,8 @@ const ratelimit = new Ratelimit({
199
325
 
200
326
  ### Token Bucket
201
327
 
328
+ _Not yet supported for `MultiRegionRatelimit`_
329
+
202
330
  Consider a bucket filled with `{maxTokens}` tokens that refills constantly at
203
331
  `{refillRate}` per `{interval}`. Every request will remove one token from the
204
332
  bucket and if there is no token to take, the request is rejected.
package/esm/mod.js CHANGED
@@ -1 +1,2 @@
1
- export { RegionRatelimit as Ratelimit } from "./region.js";
1
+ export { RegionRatelimit as Ratelimit } from "./single.js";
2
+ export { MultiRegionRatelimit } from "./multi.js";
package/esm/multi.js ADDED
@@ -0,0 +1,228 @@
1
+ import { ms } from "./duration.js";
2
+ import { Ratelimit } from "./ratelimit.js";
3
+ /**
4
+ * Ratelimiter using serverless redis from https://upstash.com/
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const { limit } = new MultiRegionRatelimit({
9
+ * redis: Redis.fromEnv(),
10
+ * limiter: MultiRegionRatelimit.fixedWindow(
11
+ * 10, // Allow 10 requests per window of 30 minutes
12
+ * "30 m", // interval of 30 minutes
13
+ * )
14
+ * })
15
+ *
16
+ * ```
17
+ */
18
+ export class MultiRegionRatelimit extends Ratelimit {
19
+ /**
20
+ * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
21
+ */
22
+ constructor(config) {
23
+ super({
24
+ prefix: config.prefix,
25
+ limiter: config.limiter,
26
+ ctx: { redis: config.redis },
27
+ });
28
+ }
29
+ /**
30
+ * Each requests inside a fixed time increases a counter.
31
+ * Once the counter reaches a maxmimum allowed number, all further requests are
32
+ * rejected.
33
+ *
34
+ * **Pro:**
35
+ *
36
+ * - Newer requests are not starved by old ones.
37
+ * - Low storage cost.
38
+ *
39
+ * **Con:**
40
+ *
41
+ * A burst of requests near the boundary of a window can result in a very
42
+ * high request rate because two windows will be filled with requests quickly.
43
+ *
44
+ * @param tokens - How many requests a user can make in each time window.
45
+ * @param window - A fixed timeframe
46
+ */
47
+ static fixedWindow(
48
+ /**
49
+ * How many requests are allowed per window.
50
+ */
51
+ tokens,
52
+ /**
53
+ * The duration in which `tokens` requests are allowed.
54
+ */
55
+ window) {
56
+ const windowDuration = ms(window);
57
+ const script = `
58
+ local key = KEYS[1]
59
+ local id = ARGV[1]
60
+ local window = ARGV[2]
61
+
62
+ redis.call("SADD", key, id)
63
+ local members = redis.call("SMEMBERS", key)
64
+ if #members == 1 then
65
+ -- The first time this key is set, the value will be 1.
66
+ -- So we only need the expire command once
67
+ redis.call("PEXPIRE", key, window)
68
+ end
69
+
70
+ return members
71
+ `;
72
+ return async function (ctx, identifier) {
73
+ const requestID = crypto.randomUUID();
74
+ const bucket = Math.floor(Date.now() / windowDuration);
75
+ const key = [identifier, bucket].join(":");
76
+ const dbs = ctx.redis.map((redis) => ({
77
+ redis,
78
+ request: redis.eval(script, [key], [requestID, windowDuration]),
79
+ }));
80
+ const firstResponse = await Promise.any(dbs.map((s) => s.request));
81
+ const usedTokens = firstResponse.length;
82
+ const remaining = tokens - usedTokens - 1;
83
+ /**
84
+ * If the length between two databases does not match, we sync the two databases
85
+ */
86
+ async function sync() {
87
+ const individualIDs = await Promise.all(dbs.map((s) => s.request));
88
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
89
+ for (const db of dbs) {
90
+ const ids = await db.request;
91
+ /**
92
+ * If the bucket in this db is already full, it doesn't matter which ids it contains.
93
+ * So we do not have to sync.
94
+ */
95
+ if (ids.length >= tokens) {
96
+ continue;
97
+ }
98
+ const diff = allIDs.filter((id) => !ids.includes(id));
99
+ /**
100
+ * Don't waste a request if there is nothing to send
101
+ */
102
+ if (diff.length === 0) {
103
+ continue;
104
+ }
105
+ await db.redis.sadd(key, ...allIDs);
106
+ }
107
+ }
108
+ /**
109
+ * Do not await sync. This should not run in the critical path.
110
+ */
111
+ return {
112
+ success: remaining > 0,
113
+ limit: tokens,
114
+ remaining,
115
+ reset: (bucket + 1) * windowDuration,
116
+ pending: sync(),
117
+ };
118
+ };
119
+ }
120
+ /**
121
+ * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
122
+ * costs than `slidingLogs` and improved boundary behavior by calcualting a
123
+ * weighted score between two windows.
124
+ *
125
+ * **Pro:**
126
+ *
127
+ * Good performance allows this to scale to very high loads.
128
+ *
129
+ * **Con:**
130
+ *
131
+ * Nothing major.
132
+ *
133
+ * @param tokens - How many requests a user can make in each time window.
134
+ * @param window - The duration in which the user can max X requests.
135
+ */
136
+ static slidingWindow(
137
+ /**
138
+ * How many requests are allowed per window.
139
+ */
140
+ tokens,
141
+ /**
142
+ * The duration in which `tokens` requests are allowed.
143
+ */
144
+ window) {
145
+ const windowSize = ms(window);
146
+ const script = `
147
+ local currentKey = KEYS[1] -- identifier including prefixes
148
+ local previousKey = KEYS[2] -- key of the previous bucket
149
+ local tokens = tonumber(ARGV[1]) -- tokens per window
150
+ local now = ARGV[2] -- current timestamp in milliseconds
151
+ local window = ARGV[3] -- interval in milliseconds
152
+ local requestID = ARGV[4] -- uuid for this request
153
+
154
+
155
+ local currentMembers = redis.call("SMEMBERS", currentKey)
156
+ local requestsInCurrentWindow = #currentMembers
157
+ local previousMembers = redis.call("SMEMBERS", previousKey)
158
+ local requestsInPreviousWindow = #previousMembers
159
+
160
+ local percentageInCurrent = ( now % window) / window
161
+ if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
162
+ return {currentMembers, previousMembers}
163
+ end
164
+
165
+ redis.call("SADD", currentKey, requestID)
166
+ table.insert(currentMembers, requestID)
167
+ if requestsInCurrentWindow == 0 then
168
+ -- The first time this key is set, the value will be 1.
169
+ -- So we only need the expire command once
170
+ redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
171
+ end
172
+ return {currentMembers, previousMembers}
173
+ `;
174
+ const windowDuration = ms(window);
175
+ return async function (ctx, identifier) {
176
+ const requestID = crypto.randomUUID();
177
+ const now = Date.now();
178
+ const currentWindow = Math.floor(now / windowSize);
179
+ const currentKey = [identifier, currentWindow].join(":");
180
+ const previousWindow = currentWindow - windowSize;
181
+ const previousKey = [identifier, previousWindow].join(":");
182
+ const dbs = ctx.redis.map((redis) => ({
183
+ redis,
184
+ request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID]),
185
+ }));
186
+ const percentageInCurrent = (now % windowDuration) / windowDuration;
187
+ const [current, previous] = await Promise.any(dbs.map((s) => s.request));
188
+ const usedTokens = previous.length * (1 - percentageInCurrent) +
189
+ current.length;
190
+ const remaining = tokens - usedTokens;
191
+ /**
192
+ * If a database differs from the consensus, we sync it
193
+ */
194
+ async function sync() {
195
+ const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
196
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
197
+ for (const db of dbs) {
198
+ const [ids] = await db.request;
199
+ /**
200
+ * If the bucket in this db is already full, it doesn't matter which ids it contains.
201
+ * So we do not have to sync.
202
+ */
203
+ if (ids.length >= tokens) {
204
+ continue;
205
+ }
206
+ const diff = allIDs.filter((id) => !ids.includes(id));
207
+ /**
208
+ * Don't waste a request if there is nothing to send
209
+ */
210
+ if (diff.length === 0) {
211
+ continue;
212
+ }
213
+ await db.redis.sadd(currentKey, ...allIDs);
214
+ }
215
+ }
216
+ /**
217
+ * Do not await sync. This should not run in the critical path.
218
+ */
219
+ return {
220
+ success: remaining > 0,
221
+ limit: tokens,
222
+ remaining,
223
+ reset: (currentWindow + 1) * windowDuration,
224
+ pending: sync(),
225
+ };
226
+ };
227
+ }
228
+ }
package/esm/ratelimit.js CHANGED
@@ -92,7 +92,7 @@ export class Ratelimit {
92
92
  * An identifier per user or api.
93
93
  * Choose a userID, or api token, or ip address.
94
94
  *
95
- * If you want to globally limit your api, you can set a constant string.
95
+ * If you want to limit your api across all users, you can set a constant string.
96
96
  */
97
97
  identifier,
98
98
  /**
@@ -76,6 +76,7 @@ export class RegionRatelimit extends Ratelimit {
76
76
  limit: tokens,
77
77
  remaining: tokens - usedTokensAfterUpdate,
78
78
  reset: (bucket + 1) * windowDuration,
79
+ pending: Promise.resolve(),
79
80
  };
80
81
  };
81
82
  }
@@ -147,6 +148,7 @@ export class RegionRatelimit extends Ratelimit {
147
148
  limit: tokens,
148
149
  remaining,
149
150
  reset: (currentWindow + 1) * windowSize,
151
+ pending: Promise.resolve(),
150
152
  };
151
153
  };
152
154
  }
@@ -226,7 +228,13 @@ export class RegionRatelimit extends Ratelimit {
226
228
  const now = Date.now();
227
229
  const key = [identifier, Math.floor(now / intervalDuration)].join(":");
228
230
  const [remaining, reset] = (await ctx.redis.eval(script, [key], [maxTokens, intervalDuration, refillRate, now]));
229
- return { success: remaining > 0, limit: maxTokens, remaining, reset };
231
+ return {
232
+ success: remaining > 0,
233
+ limit: maxTokens,
234
+ remaining,
235
+ reset,
236
+ pending: Promise.resolve(),
237
+ };
230
238
  };
231
239
  }
232
240
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/mod.js",
4
4
  "types": "./types/mod.d.ts",
5
5
  "name": "@upstash/ratelimit",
6
- "version": "v0.1.1",
6
+ "version": "v0.1.3",
7
7
  "description": "A serverless ratelimiter built on top of Upstash REST API.",
8
8
  "repository": {
9
9
  "type": "git",
package/script/mod.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Ratelimit = void 0;
4
- var region_js_1 = require("./region.js");
5
- Object.defineProperty(exports, "Ratelimit", { enumerable: true, get: function () { return region_js_1.RegionRatelimit; } });
3
+ exports.MultiRegionRatelimit = exports.Ratelimit = void 0;
4
+ var single_js_1 = require("./single.js");
5
+ Object.defineProperty(exports, "Ratelimit", { enumerable: true, get: function () { return single_js_1.RegionRatelimit; } });
6
+ var multi_js_1 = require("./multi.js");
7
+ Object.defineProperty(exports, "MultiRegionRatelimit", { enumerable: true, get: function () { return multi_js_1.MultiRegionRatelimit; } });
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MultiRegionRatelimit = void 0;
4
+ const duration_js_1 = require("./duration.js");
5
+ const ratelimit_js_1 = require("./ratelimit.js");
6
+ /**
7
+ * Ratelimiter using serverless redis from https://upstash.com/
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const { limit } = new MultiRegionRatelimit({
12
+ * redis: Redis.fromEnv(),
13
+ * limiter: MultiRegionRatelimit.fixedWindow(
14
+ * 10, // Allow 10 requests per window of 30 minutes
15
+ * "30 m", // interval of 30 minutes
16
+ * )
17
+ * })
18
+ *
19
+ * ```
20
+ */
21
+ class MultiRegionRatelimit extends ratelimit_js_1.Ratelimit {
22
+ /**
23
+ * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
24
+ */
25
+ constructor(config) {
26
+ super({
27
+ prefix: config.prefix,
28
+ limiter: config.limiter,
29
+ ctx: { redis: config.redis },
30
+ });
31
+ }
32
+ /**
33
+ * Each requests inside a fixed time increases a counter.
34
+ * Once the counter reaches a maxmimum allowed number, all further requests are
35
+ * rejected.
36
+ *
37
+ * **Pro:**
38
+ *
39
+ * - Newer requests are not starved by old ones.
40
+ * - Low storage cost.
41
+ *
42
+ * **Con:**
43
+ *
44
+ * A burst of requests near the boundary of a window can result in a very
45
+ * high request rate because two windows will be filled with requests quickly.
46
+ *
47
+ * @param tokens - How many requests a user can make in each time window.
48
+ * @param window - A fixed timeframe
49
+ */
50
+ static fixedWindow(
51
+ /**
52
+ * How many requests are allowed per window.
53
+ */
54
+ tokens,
55
+ /**
56
+ * The duration in which `tokens` requests are allowed.
57
+ */
58
+ window) {
59
+ const windowDuration = (0, duration_js_1.ms)(window);
60
+ const script = `
61
+ local key = KEYS[1]
62
+ local id = ARGV[1]
63
+ local window = ARGV[2]
64
+
65
+ redis.call("SADD", key, id)
66
+ local members = redis.call("SMEMBERS", key)
67
+ if #members == 1 then
68
+ -- The first time this key is set, the value will be 1.
69
+ -- So we only need the expire command once
70
+ redis.call("PEXPIRE", key, window)
71
+ end
72
+
73
+ return members
74
+ `;
75
+ return async function (ctx, identifier) {
76
+ const requestID = crypto.randomUUID();
77
+ const bucket = Math.floor(Date.now() / windowDuration);
78
+ const key = [identifier, bucket].join(":");
79
+ const dbs = ctx.redis.map((redis) => ({
80
+ redis,
81
+ request: redis.eval(script, [key], [requestID, windowDuration]),
82
+ }));
83
+ const firstResponse = await Promise.any(dbs.map((s) => s.request));
84
+ const usedTokens = firstResponse.length;
85
+ const remaining = tokens - usedTokens - 1;
86
+ /**
87
+ * If the length between two databases does not match, we sync the two databases
88
+ */
89
+ async function sync() {
90
+ const individualIDs = await Promise.all(dbs.map((s) => s.request));
91
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
92
+ for (const db of dbs) {
93
+ const ids = await db.request;
94
+ /**
95
+ * If the bucket in this db is already full, it doesn't matter which ids it contains.
96
+ * So we do not have to sync.
97
+ */
98
+ if (ids.length >= tokens) {
99
+ continue;
100
+ }
101
+ const diff = allIDs.filter((id) => !ids.includes(id));
102
+ /**
103
+ * Don't waste a request if there is nothing to send
104
+ */
105
+ if (diff.length === 0) {
106
+ continue;
107
+ }
108
+ await db.redis.sadd(key, ...allIDs);
109
+ }
110
+ }
111
+ /**
112
+ * Do not await sync. This should not run in the critical path.
113
+ */
114
+ return {
115
+ success: remaining > 0,
116
+ limit: tokens,
117
+ remaining,
118
+ reset: (bucket + 1) * windowDuration,
119
+ pending: sync(),
120
+ };
121
+ };
122
+ }
123
+ /**
124
+ * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
125
+ * costs than `slidingLogs` and improved boundary behavior by calcualting a
126
+ * weighted score between two windows.
127
+ *
128
+ * **Pro:**
129
+ *
130
+ * Good performance allows this to scale to very high loads.
131
+ *
132
+ * **Con:**
133
+ *
134
+ * Nothing major.
135
+ *
136
+ * @param tokens - How many requests a user can make in each time window.
137
+ * @param window - The duration in which the user can max X requests.
138
+ */
139
+ static slidingWindow(
140
+ /**
141
+ * How many requests are allowed per window.
142
+ */
143
+ tokens,
144
+ /**
145
+ * The duration in which `tokens` requests are allowed.
146
+ */
147
+ window) {
148
+ const windowSize = (0, duration_js_1.ms)(window);
149
+ const script = `
150
+ local currentKey = KEYS[1] -- identifier including prefixes
151
+ local previousKey = KEYS[2] -- key of the previous bucket
152
+ local tokens = tonumber(ARGV[1]) -- tokens per window
153
+ local now = ARGV[2] -- current timestamp in milliseconds
154
+ local window = ARGV[3] -- interval in milliseconds
155
+ local requestID = ARGV[4] -- uuid for this request
156
+
157
+
158
+ local currentMembers = redis.call("SMEMBERS", currentKey)
159
+ local requestsInCurrentWindow = #currentMembers
160
+ local previousMembers = redis.call("SMEMBERS", previousKey)
161
+ local requestsInPreviousWindow = #previousMembers
162
+
163
+ local percentageInCurrent = ( now % window) / window
164
+ if requestsInPreviousWindow * ( 1 - percentageInCurrent ) + requestsInCurrentWindow >= tokens then
165
+ return {currentMembers, previousMembers}
166
+ end
167
+
168
+ redis.call("SADD", currentKey, requestID)
169
+ table.insert(currentMembers, requestID)
170
+ if requestsInCurrentWindow == 0 then
171
+ -- The first time this key is set, the value will be 1.
172
+ -- So we only need the expire command once
173
+ redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second
174
+ end
175
+ return {currentMembers, previousMembers}
176
+ `;
177
+ const windowDuration = (0, duration_js_1.ms)(window);
178
+ return async function (ctx, identifier) {
179
+ const requestID = crypto.randomUUID();
180
+ const now = Date.now();
181
+ const currentWindow = Math.floor(now / windowSize);
182
+ const currentKey = [identifier, currentWindow].join(":");
183
+ const previousWindow = currentWindow - windowSize;
184
+ const previousKey = [identifier, previousWindow].join(":");
185
+ const dbs = ctx.redis.map((redis) => ({
186
+ redis,
187
+ request: redis.eval(script, [currentKey, previousKey], [tokens, now, windowDuration, requestID]),
188
+ }));
189
+ const percentageInCurrent = (now % windowDuration) / windowDuration;
190
+ const [current, previous] = await Promise.any(dbs.map((s) => s.request));
191
+ const usedTokens = previous.length * (1 - percentageInCurrent) +
192
+ current.length;
193
+ const remaining = tokens - usedTokens;
194
+ /**
195
+ * If a database differs from the consensus, we sync it
196
+ */
197
+ async function sync() {
198
+ const [individualIDs] = await Promise.all(dbs.map((s) => s.request));
199
+ const allIDs = Array.from(new Set(individualIDs.flatMap((_) => _)).values());
200
+ for (const db of dbs) {
201
+ const [ids] = await db.request;
202
+ /**
203
+ * If the bucket in this db is already full, it doesn't matter which ids it contains.
204
+ * So we do not have to sync.
205
+ */
206
+ if (ids.length >= tokens) {
207
+ continue;
208
+ }
209
+ const diff = allIDs.filter((id) => !ids.includes(id));
210
+ /**
211
+ * Don't waste a request if there is nothing to send
212
+ */
213
+ if (diff.length === 0) {
214
+ continue;
215
+ }
216
+ await db.redis.sadd(currentKey, ...allIDs);
217
+ }
218
+ }
219
+ /**
220
+ * Do not await sync. This should not run in the critical path.
221
+ */
222
+ return {
223
+ success: remaining > 0,
224
+ limit: tokens,
225
+ remaining,
226
+ reset: (currentWindow + 1) * windowDuration,
227
+ pending: sync(),
228
+ };
229
+ };
230
+ }
231
+ }
232
+ exports.MultiRegionRatelimit = MultiRegionRatelimit;
@@ -95,7 +95,7 @@ class Ratelimit {
95
95
  * An identifier per user or api.
96
96
  * Choose a userID, or api token, or ip address.
97
97
  *
98
- * If you want to globally limit your api, you can set a constant string.
98
+ * If you want to limit your api across all users, you can set a constant string.
99
99
  */
100
100
  identifier,
101
101
  /**
@@ -79,6 +79,7 @@ class RegionRatelimit extends ratelimit_js_1.Ratelimit {
79
79
  limit: tokens,
80
80
  remaining: tokens - usedTokensAfterUpdate,
81
81
  reset: (bucket + 1) * windowDuration,
82
+ pending: Promise.resolve(),
82
83
  };
83
84
  };
84
85
  }
@@ -150,6 +151,7 @@ class RegionRatelimit extends ratelimit_js_1.Ratelimit {
150
151
  limit: tokens,
151
152
  remaining,
152
153
  reset: (currentWindow + 1) * windowSize,
154
+ pending: Promise.resolve(),
153
155
  };
154
156
  };
155
157
  }
@@ -229,7 +231,13 @@ class RegionRatelimit extends ratelimit_js_1.Ratelimit {
229
231
  const now = Date.now();
230
232
  const key = [identifier, Math.floor(now / intervalDuration)].join(":");
231
233
  const [remaining, reset] = (await ctx.redis.eval(script, [key], [maxTokens, intervalDuration, refillRate, now]));
232
- return { success: remaining > 0, limit: maxTokens, remaining, reset };
234
+ return {
235
+ success: remaining > 0,
236
+ limit: maxTokens,
237
+ remaining,
238
+ reset,
239
+ pending: Promise.resolve(),
240
+ };
233
241
  };
234
242
  }
235
243
  }
package/types/mod.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- export { RegionRatelimit as Ratelimit } from "./region.js";
2
- export type { RegionRatelimitConfig as RatelimitConfig } from "./region.js";
1
+ export { RegionRatelimit as Ratelimit } from "./single.js";
2
+ export type { RegionRatelimitConfig as RatelimitConfig } from "./single.js";
3
+ export { MultiRegionRatelimit } from "./multi.js";
4
+ export type { MultiRegionRatelimitConfig } from "./multi.js";
3
5
  export type { Algorithm } from "./types.js";
@@ -0,0 +1,98 @@
1
+ import type { Duration } from "./duration.js";
2
+ import type { Algorithm, MultiRegionContext } from "./types.js";
3
+ import { Ratelimit } from "./ratelimit.js";
4
+ import type { Redis } from "./types.js";
5
+ export declare type MultiRegionRatelimitConfig = {
6
+ /**
7
+ * Instances of `@upstash/redis`
8
+ * @see https://github.com/upstash/upstash-redis#quick-start
9
+ */
10
+ redis: Redis[];
11
+ /**
12
+ * The ratelimiter function to use.
13
+ *
14
+ * Choose one of the predefined ones or implement your own.
15
+ * Available algorithms are exposed via static methods:
16
+ * - MultiRegionRatelimit.fixedWindow
17
+ */
18
+ limiter: Algorithm<MultiRegionContext>;
19
+ /**
20
+ * All keys in redis are prefixed with this.
21
+ *
22
+ * @default `@upstash/ratelimit`
23
+ */
24
+ prefix?: string;
25
+ };
26
+ /**
27
+ * Ratelimiter using serverless redis from https://upstash.com/
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const { limit } = new MultiRegionRatelimit({
32
+ * redis: Redis.fromEnv(),
33
+ * limiter: MultiRegionRatelimit.fixedWindow(
34
+ * 10, // Allow 10 requests per window of 30 minutes
35
+ * "30 m", // interval of 30 minutes
36
+ * )
37
+ * })
38
+ *
39
+ * ```
40
+ */
41
+ export declare class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
42
+ /**
43
+ * Create a new Ratelimit instance by providing a `@upstash/redis` instance and the algorithn of your choice.
44
+ */
45
+ constructor(config: MultiRegionRatelimitConfig);
46
+ /**
47
+ * Each requests inside a fixed time increases a counter.
48
+ * Once the counter reaches a maxmimum allowed number, all further requests are
49
+ * rejected.
50
+ *
51
+ * **Pro:**
52
+ *
53
+ * - Newer requests are not starved by old ones.
54
+ * - Low storage cost.
55
+ *
56
+ * **Con:**
57
+ *
58
+ * A burst of requests near the boundary of a window can result in a very
59
+ * high request rate because two windows will be filled with requests quickly.
60
+ *
61
+ * @param tokens - How many requests a user can make in each time window.
62
+ * @param window - A fixed timeframe
63
+ */
64
+ static fixedWindow(
65
+ /**
66
+ * How many requests are allowed per window.
67
+ */
68
+ tokens: number,
69
+ /**
70
+ * The duration in which `tokens` requests are allowed.
71
+ */
72
+ window: Duration): Algorithm<MultiRegionContext>;
73
+ /**
74
+ * Combined approach of `slidingLogs` and `fixedWindow` with lower storage
75
+ * costs than `slidingLogs` and improved boundary behavior by calcualting a
76
+ * weighted score between two windows.
77
+ *
78
+ * **Pro:**
79
+ *
80
+ * Good performance allows this to scale to very high loads.
81
+ *
82
+ * **Con:**
83
+ *
84
+ * Nothing major.
85
+ *
86
+ * @param tokens - How many requests a user can make in each time window.
87
+ * @param window - The duration in which the user can max X requests.
88
+ */
89
+ static slidingWindow(
90
+ /**
91
+ * How many requests are allowed per window.
92
+ */
93
+ tokens: number,
94
+ /**
95
+ * The duration in which `tokens` requests are allowed.
96
+ */
97
+ window: Duration): Algorithm<MultiRegionContext>;
98
+ }
File without changes
package/types/types.d.ts CHANGED
@@ -5,10 +5,10 @@ export interface Redis {
5
5
  export declare type RegionContext = {
6
6
  redis: Redis;
7
7
  };
8
- export declare type GlobalContext = {
8
+ export declare type MultiRegionContext = {
9
9
  redis: Redis[];
10
10
  };
11
- export declare type Context = RegionContext | GlobalContext;
11
+ export declare type Context = RegionContext | MultiRegionContext;
12
12
  export declare type RatelimitResponse = {
13
13
  /**
14
14
  * Whether the request may pass(true) or exceeded the limit(false)
@@ -26,5 +26,28 @@ export declare type RatelimitResponse = {
26
26
  * Unix timestamp in milliseconds when the limits are reset.
27
27
  */
28
28
  reset: number;
29
+ /**
30
+ * For the MultiRegion setup we do some synchronizing in the background, after returning the current limit.
31
+ * In most case you can simply ignore this.
32
+ *
33
+ * On Vercel Edge or Cloudflare workers, you need to explicitely handle the pending Promise like this:
34
+ *
35
+ * **Vercel Edge:**
36
+ * https://nextjs.org/docs/api-reference/next/server#nextfetchevent
37
+ *
38
+ * ```ts
39
+ * const { pending } = await ratelimit.limit("id")
40
+ * event.waitUntil(pending)
41
+ * ```
42
+ *
43
+ * **Cloudflare Worker:**
44
+ * https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker
45
+ *
46
+ * ```ts
47
+ * const { pending } = await ratelimit.limit("id")
48
+ * context.waitUntil(pending)
49
+ * ```
50
+ */
51
+ pending: Promise<unknown>;
29
52
  };
30
53
  export declare type Algorithm<TContext> = (ctx: TContext, identifier: string) => Promise<RatelimitResponse>;