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

feat: token bucket rate limiting #91

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"jose": "^5.9.3",
"pg-promise": "^11.9.1",
"pino": "^9.4.0",
"rate-limiter-flexible": "^5.0.4",
"redis": "^4.7.0"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/@types/express/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare global {
// biome-ignore lint/style/noNamespace: <It is the only way to do this. If we use module Biome also complains>
namespace Express {
interface Request {
tokenConsumedOnFormId?: string;
serviceAccountId: string;
serviceUserId: string;
}
Expand Down
17 changes: 17 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export enum EnvironmentMode {
Production = "production",
}

export type TokenBucketConfiguration = {
capacity: number;
numberOfSecondsBeforeRefill: number;
};

// AWS SDK

export const AWS_REGION: string = "ca-central-1";
Expand All @@ -30,6 +35,18 @@ export const LOCALSTACK_ENDPOINT: string | undefined = loadOptionalEnvVar(
"LOCALSTACK_ENDPOINT",
);

// Rate limiting

export const lowRateLimiterConfiguration: TokenBucketConfiguration = {
capacity: 500,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A maximum of 500 requests per minute is going to be what we ask our prototyping partners to follow for V1. When the Rate limiter is released we will actually be able to see whether it is a good starting point for our users.

numberOfSecondsBeforeRefill: 60,
};

export const highRateLimiterConfiguration: TokenBucketConfiguration = {
capacity: 1000,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The high capacity token bucket is not going to be used for now as the web app feature to enable it is not a priority at the moment.

numberOfSecondsBeforeRefill: 60,
};

// Redis

export const REDIS_URL: string = loadRequiredEnvVar("REDIS_URL");
Expand Down
9 changes: 6 additions & 3 deletions src/lib/formsClient/getPublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DatabaseConnectorClient } from "@lib/integration/databaseConnector.js";
import { getValueFromCache, cacheValue } from "@lib/utils/cache.js";
import {
getValueFromRedis,
setValueInRedis,
} from "@lib/integration/redis/redisClientAdapter.js";
import { logMessage } from "@lib/logging/logger.js";

const REDIS_PUBLIC_KEY_PREFIX: string = "api:publicKey";
Expand Down Expand Up @@ -52,14 +55,14 @@ function retrievePublicKeyFromDatabase(
function getPublicKeyFromCache(
serviceAccountId: string,
): Promise<string | undefined> {
return getValueFromCache(`${REDIS_PUBLIC_KEY_PREFIX}:${serviceAccountId}`);
return getValueFromRedis(`${REDIS_PUBLIC_KEY_PREFIX}:${serviceAccountId}`);
}

function cachePublicKey(
publicKey: string,
serviceAccountId: string,
): Promise<void> {
return cacheValue(
return setValueInRedis(
`${REDIS_PUBLIC_KEY_PREFIX}:${serviceAccountId}`,
publicKey,
CACHE_EXPIRY_DELAY_IN_SECONDS,
Expand Down
9 changes: 6 additions & 3 deletions src/lib/idp/verifyAccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { introspectAccessToken } from "@lib/integration/zitadelConnector.js";

Check failure on line 1 in src/lib/idp/verifyAccessToken.ts

View workflow job for this annotation

GitHub Actions / test

organizeImports

Import statements differs from the output
import { cacheValue, getValueFromCache } from "@lib/utils/cache.js";
import {
setValueInRedis,
getValueFromRedis,
} from "@lib/integration/redis/redisClientAdapter.js";
import { createHash } from "node:crypto";
import { logMessage } from "@lib/logging/logger.js";

Expand Down Expand Up @@ -60,7 +63,7 @@
): Promise<VerifiedAccessToken | undefined> {
const key = getVerifiedAccessTokenCacheKey(accessToken);

return getValueFromCache(key).then((value) => {
return getValueFromRedis(key).then((value) => {
return value ? (JSON.parse(value) as VerifiedAccessToken) : undefined;
});
}
Expand All @@ -71,7 +74,7 @@
): Promise<void> {
const key = getVerifiedAccessTokenCacheKey(accessToken);

return cacheValue(
return setValueInRedis(
key,
JSON.stringify(introspectionResult),
CACHE_EXPIRY_DELAY_IN_SECONDS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { RedisConnector } from "@lib/integration/redisConnector.js";
import { RedisConnector } from "@lib/integration/redis/redisConnector.js";

export function getValueFromCache(key: string): Promise<string | undefined> {
export function getValueFromRedis(key: string): Promise<string | undefined> {
return RedisConnector.getInstance()
.then((redisConnector) => redisConnector.client.get(key))
.then((value) => {
return value ?? undefined;
});
}

export function cacheValue(
export function setValueInRedis(
key: string,
value: string,
expiryDelayInSeconds?: number,
Expand Down
53 changes: 53 additions & 0 deletions src/lib/rateLimiting/tokenBucketLimiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getTokenBucketRateLimiterAssociatedToForm } from "@lib/rateLimiting/tokenBucketProvider.js";
import type { RateLimiterRes } from "rate-limiter-flexible";

export type BucketStatus = {
bucketCapacity: number;
remainingTokens: number;
numberOfMillisecondsBeforeRefill: number;
};

export type ConsumeTokenResult = {
wasAbleToConsumeToken: boolean;
bucketStatus: BucketStatus;
};

export async function consumeTokenIfAvailable(
formId: string,
): Promise<ConsumeTokenResult> {
const tokenBucket = await getTokenBucketRateLimiterAssociatedToForm(formId);

try {
const consumptionResult = await tokenBucket.consume(formId);

return {
wasAbleToConsumeToken: true,
bucketStatus: buildBucketStatus(tokenBucket.points, consumptionResult),
};
} catch (rateLimiterRes) {
// Since our token buckets have an `insuranceLimiter` set up, the consume function promise can only be rejected with a `RateLimiterRes` object
return {
wasAbleToConsumeToken: false,
bucketStatus: buildBucketStatus(
tokenBucket.points,
rateLimiterRes as RateLimiterRes,
),
};
}
}

export async function refundConsumedToken(formId: string): Promise<void> {
const tokenBucket = await getTokenBucketRateLimiterAssociatedToForm(formId);
await tokenBucket.reward(formId);
}

function buildBucketStatus(
bucketCapacity: number,
rateLimiterResponse: RateLimiterRes,
): BucketStatus {
return {
bucketCapacity: bucketCapacity,
remainingTokens: rateLimiterResponse.remainingPoints,
numberOfMillisecondsBeforeRefill: rateLimiterResponse.msBeforeNext,
};
}
77 changes: 77 additions & 0 deletions src/lib/rateLimiting/tokenBucketProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {
type RateLimiterAbstract,
RateLimiterMemory,
RateLimiterRedis,
} from "rate-limiter-flexible";
import { RedisConnector } from "@lib/integration/redis/redisConnector.js";
import {
highRateLimiterConfiguration,
lowRateLimiterConfiguration,
} from "@config";
import { getValueFromRedis } from "@lib/integration/redis/redisClientAdapter.js";
import { logMessage } from "@lib/logging/logger.js";

const REDIS_RATE_LIMIT_KEY_PREFIX: string = "rate-limit";

const redisClient = await RedisConnector.getInstance().then(
(instance) => instance.client,
);

/**
* From the official documentation:
* `inMemoryBlockOnConsumed`: Can be used against DDoS attacks. In-memory blocking works in current process memory and for consume method only.
* It blocks a key in memory for msBeforeNext milliseconds from the last consume result, if inMemoryBlockDuration is
* not set. This helps to avoid extra requests. inMemoryBlockOnConsumed value is supposed to be equal or more than
* points option. Sometimes it is not necessary to increment counter on store, if all points are consumed already.
* `insuranceLimiter`: Instance of RateLimiterAbstract extended object to store limits, when database comes up with any error.
*/

const lowCapacityTokenBucket = new RateLimiterRedis({
keyPrefix: "low-capacity-token-bucket",
storeClient: redisClient,
useRedisPackage: true,
points: lowRateLimiterConfiguration.capacity,
duration: lowRateLimiterConfiguration.numberOfSecondsBeforeRefill,
inMemoryBlockOnConsumed: lowRateLimiterConfiguration.capacity,
insuranceLimiter: new RateLimiterMemory({
keyPrefix: "backup-low-capacity-token-bucket",
points: lowRateLimiterConfiguration.capacity,
duration: lowRateLimiterConfiguration.numberOfSecondsBeforeRefill,
}),
});

const highCapacityTokenBucket = new RateLimiterRedis({
keyPrefix: "high-capacity-token-bucket",
storeClient: redisClient,
useRedisPackage: true,
points: highRateLimiterConfiguration.capacity,
duration: highRateLimiterConfiguration.numberOfSecondsBeforeRefill,
inMemoryBlockOnConsumed: highRateLimiterConfiguration.capacity,
insuranceLimiter: new RateLimiterMemory({
keyPrefix: "backup-high-capacity-token-bucket",
points: highRateLimiterConfiguration.capacity,
duration: highRateLimiterConfiguration.numberOfSecondsBeforeRefill,
}),
});

export function getTokenBucketRateLimiterAssociatedToForm(
formId: string,
): Promise<RateLimiterAbstract> {
return getValueFromRedis(`${REDIS_RATE_LIMIT_KEY_PREFIX}:${formId}`)
.then((value) => {
switch (value) {
case "high":
return highCapacityTokenBucket;
default:
return lowCapacityTokenBucket;
}
})
.catch((error) => {
logMessage.warn(
error,
`[token-bucket-provider] Failed to retrieve token bucket capacity for form ${formId}. Will use low capacity bucket by default`,
);

return lowCapacityTokenBucket;
});
}
12 changes: 9 additions & 3 deletions src/middleware/globalErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Request, Response, NextFunction } from "express";
import { logMessage } from "@lib/logging/logger.js";
import { refundConsumedToken } from "@lib/rateLimiting/tokenBucketLimiter.js";

export function globalErrorHandlerMiddleware(
export async function globalErrorHandlerMiddleware(
error: Error,
_request: Request,
request: Request,
response: Response,
_next: NextFunction,
): void {
): Promise<void> {
logMessage.error(error, "Global unhandled error");

if (request.tokenConsumedOnFormId !== undefined) {
craigzour marked this conversation as resolved.
Show resolved Hide resolved
await refundConsumedToken(request.tokenConsumedOnFormId);
}

response.sendStatus(500);
}
50 changes: 45 additions & 5 deletions src/middleware/rateLimiter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import type { NextFunction, Request, Response } from "express";
import { consumeTokenIfAvailable } from "@lib/rateLimiting/tokenBucketLimiter.js";
import { logMessage } from "@lib/logging/logger.js";

export function rateLimiterMiddleware(
_request: Request,
_response: Response,
export async function rateLimiterMiddleware(
request: Request,
response: Response,
next: NextFunction,
): void {
next();
): Promise<void> {
try {
const formId = request.params.formId;

const consumeTokenResult = await consumeTokenIfAvailable(formId);

response.header({
"X-RateLimit-Limit": consumeTokenResult.bucketStatus.bucketCapacity,
"X-RateLimit-Remaining": consumeTokenResult.bucketStatus.remainingTokens,
"X-RateLimit-Reset": new Date(
Date.now() +
consumeTokenResult.bucketStatus.numberOfMillisecondsBeforeRefill,
),
});

if (consumeTokenResult.wasAbleToConsumeToken === false) {
response.header({
"Retry-After":
consumeTokenResult.bucketStatus.numberOfMillisecondsBeforeRefill /
1000,
});

logMessage.info(
`[rate-limiter] Form ${formId} consumed all ${consumeTokenResult.bucketStatus.bucketCapacity} tokens. Bucket will be refilled in ${consumeTokenResult.bucketStatus.numberOfMillisecondsBeforeRefill / 1000} seconds`,
);

response.sendStatus(429);
return;
}

request.tokenConsumedOnFormId = formId;

next();
} catch (error) {
next(
new Error("[middleware] Internal error with rate limiter", {
cause: error,
}),
);
}
}
9 changes: 6 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export function buildRouter(): Router {
.use("/template", templateRoute)
.use("/submission", submissionRoute);

const formsRoute = Router()
.use(rateLimiterMiddleware)
.use("/:formId(c[a-z0-9]{24})", authenticationMiddleware, formIdRoute);
const formsRoute = Router().use(
"/:formId(c[a-z0-9]{24})",
authenticationMiddleware,
rateLimiterMiddleware,
formIdRoute,
);

const statusRoute = Router().get(
"/",
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express, { type Express } from "express";
import { SERVER_PORT } from "@config";
import { buildRouter } from "./router.js";
import { getApiAuditLogSqsQueueUrl } from "@lib/integration/awsSqsQueueLoader.js";
import { RedisConnector } from "@lib/integration/redisConnector.js";
import { RedisConnector } from "@lib/integration/redis/redisConnector.js";
import { logMessage } from "@lib/logging/logger.js";

const server: Express = express();
Expand Down
Loading
Loading