Skip to content

Commit

Permalink
fix: PR comments, unneeded end() call and variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Oct 18, 2023
1 parent 3b57369 commit d92a752
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
24 changes: 12 additions & 12 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,27 @@ function loadClientCachingOptions(
function loadMetricsRateLimitingConfig(
options: IUnleashOptions,
): IMetricsRateLimiting {
const clientMetricsMax = parseEnvVarNumber(
process.env.REGISTER_CLIENT_RATE_LIMIT,
const clientMetricsMaxPerMinute = parseEnvVarNumber(
process.env.REGISTER_CLIENT_RATE_LIMIT_PER_MINUTE,
6000,
);
const clientRegisterMax: number = parseEnvVarNumber(
process.env.CLIENT_METRICS_RATE_LIMIT,
const clientRegisterMaxPerMinute = parseEnvVarNumber(
process.env.CLIENT_METRICS_RATE_LIMIT_PER_MINUTE,
6000,
);
const frontendRegisterMax = parseEnvVarNumber(
process.env.REGISTER_FRONTEND_RATE_LIMIT,
const frontendRegisterMaxPerMinute = parseEnvVarNumber(
process.env.REGISTER_FRONTEND_RATE_LIMIT_PER_MINUTE,
6000,
);
const frontendMetricsMax = parseEnvVarNumber(
process.env.FRONTEND_METRICS_RATE_LIMIT,
const frontendMetricsMaxPerMinute = parseEnvVarNumber(
process.env.FRONTEND_METRICS_RATE_LIMIT_PER_MINUTE,
6000,
);
const defaultRateLimitOptions: IMetricsRateLimiting = {
clientMetricsMax,
clientRegisterMax,
frontendRegisterMax,
frontendMetricsMax,
clientMetricsMaxPerMinute: clientMetricsMaxPerMinute,
clientRegisterMaxPerMinute: clientRegisterMaxPerMinute,
frontendRegisterMaxPerMinute: frontendRegisterMaxPerMinute,
frontendMetricsMaxPerMinute: frontendMetricsMaxPerMinute,
};

return mergeAll([
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/client-api/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class ClientMetricsController extends Controller {
}),
rateLimit({
windowMs: minutesToMilliseconds(1),
max: config.metricsRateLimiting.clientMetricsMax,
max: config.metricsRateLimiting.clientMetricsMaxPerMinute,
validate: false,
standardHeaders: true,
legacyHeaders: false,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/client-api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class RegisterController extends Controller {
}),
rateLimit({
windowMs: minutesToMilliseconds(1),
max: config.metricsRateLimiting.clientRegisterMax,
max: config.metricsRateLimiting.clientRegisterMaxPerMinute,
validate: false,
standardHeaders: true,
legacyHeaders: false,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/routes/proxy-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class ProxyController extends Controller {
}),
rateLimit({
windowMs: minutesToMilliseconds(1),
max: config.metricsRateLimiting.frontendMetricsMax,
max: config.metricsRateLimiting.frontendMetricsMaxPerMinute,
validate: false,
standardHeaders: true,
legacyHeaders: false,
Expand Down Expand Up @@ -144,7 +144,8 @@ export default class ProxyController extends Controller {
}),
rateLimit({
windowMs: minutesToMilliseconds(1),
max: config.metricsRateLimiting.frontendRegisterMax,
max: config.metricsRateLimiting
.frontendRegisterMaxPerMinute,
validate: false,
standardHeaders: true,
legacyHeaders: false,
Expand Down
8 changes: 5 additions & 3 deletions src/lib/routes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export const handleErrors: (
) => void = (res, logger, error) => {
if (createError.isHttpError(error)) {
return res
.status(error.status ?? 400)
.json({ message: error.message })
.end();
.status(
// @ts-expect-error - The error object here is not guaranteed to contain status
error.status ?? 400,
)
.json({ message: error.message });
}

const finalError =
Expand Down
8 changes: 4 additions & 4 deletions src/lib/types/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ interface IFrontendApi {
}

export interface IMetricsRateLimiting {
clientMetricsMax: number;
clientRegisterMax: number;
frontendMetricsMax: number;
frontendRegisterMax: number;
clientMetricsMaxPerMinute: number;
clientRegisterMaxPerMinute: number;
frontendMetricsMaxPerMinute: number;
frontendRegisterMaxPerMinute: number;
}

export interface IUnleashConfig {
Expand Down
8 changes: 4 additions & 4 deletions website/docs/reference/deploy/configuring-unleash.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ unleash.start(unleashOptions);
- **keepAliveTimeout** - Use this to tweak connection keepalive timeout in seconds. Useful for hosted situations where you need to make sure your connections are closed before terminating the instance. Defaults to `15`. Overridable with the `SERVER_KEEPALIVE_TIMEOUT` environment variable.
You can also set the environment variable `ENABLED_ENVIRONMENTS` to a comma delimited string of environment names to override environments.
- **metricsRateLimiting** - Use the following to tweak the rate limits for `/api/client/register`, `/api/client/metrics`, `/api/frontend/register` and `/api/frontend/metrics` POST endpoints
- `clientMetricsMax` - How many requests per minute is allowed against POST `/api/client/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_CLIENT_RATE_LIMIT` environment variable
- `clientRegisterMax` - How many requests per minute is allowed against POST `/api/client/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `CLIENT_METRICS_RATE_LIMIT` environment variable
- `frontendMetricsMax` - How many requests per minute is allowed against POST `/api/frontend/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `FRONTEND_METRICS_RATE_LIMIT` environment variable
- `frontendRegisterMax` - How many requests per minute is allowed against POST `/api/frontend/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_FRONTEND_RATE_LIMIT` environment variable
- `clientMetricsMaxPerMinute` - How many requests per minute is allowed against POST `/api/client/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_CLIENT_RATE_LIMIT_PER_MINUTE` environment variable
- `clientRegisterMaxPerMinute` - How many requests per minute is allowed against POST `/api/client/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `CLIENT_METRICS_RATE_LIMIT_PER_MINUTE` environment variable
- `frontendMetricsMaxPerMinute` - How many requests per minute is allowed against POST `/api/frontend/metrics` before returning 429. Set to 6000 by default (100rps) - Overridable with `FRONTEND_METRICS_RATE_LIMIT_PER_MINUTE` environment variable
- `frontendRegisterMaxPerMinute` - How many requests per minute is allowed against POST `/api/frontend/register` before returning 429. Set to 6000 by default (100rps) - Overridable with `REGISTER_FRONTEND_RATE_LIMIT_PER_MINUTE` environment variable
### Disabling Auto-Start {#disabling-auto-start}

If you're using Unleash as part of a larger express app, you can disable the automatic server start by calling `server.create`. It takes the same options as `server.start`, but will not begin listening for connections.
Expand Down

0 comments on commit d92a752

Please sign in to comment.