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: /limits endpoint #543

Merged
merged 21 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions config/custom-environment-variables.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ const df = require('./default.cjs');

function mapEnvConfig (object, prefix = '') {
return _.mapValues(object, (value, key) => {
const currentKey = (prefix ? `${prefix}_` : '') + _.snakeCase(key).toUpperCase();

if (_.isObject(value)) {
return mapEnvConfig(value, (prefix ? `${prefix}_` : '') + _.snakeCase(key).toUpperCase());
return mapEnvConfig(value, currentKey);
}

if (typeof value === 'number' || typeof value === 'boolean') {
return {
__name: currentKey,
__format: typeof value,
};
}

return (prefix ? `${prefix}_` : '') + _.snakeCase(key).toUpperCase();
return currentKey;
});
}

Expand Down
1 change: 1 addition & 0 deletions config/default.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ module.exports = {
SA: 10,
},
},
reconnectProbesDelay: 2 * 60 * 1000,
sigtermDelay: 15000,
};
6 changes: 6 additions & 0 deletions config/development.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module.exports = {
server: {
session: {
cookieSecret: 'xxx',
},
},
redis: {
url: 'redis://localhost:16379',
socket: {
Expand All @@ -16,4 +21,5 @@ module.exports = {
systemApi: {
key: 'system',
},
reconnectProbesDelay: 0,
};
33 changes: 33 additions & 0 deletions public/v1/components/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,36 @@ components:
}
]
}
nonAuthenticatedLimits:
summary: 'non authenticated'
value:
{
"rateLimit": {
"measurements": {
"create": {
"type": "ip",
"limit": 100,
"remaining": 95,
"reset": 3599
}
}
}
}
authenticatedLimits:
summary: 'authenticated'
value:
{
"rateLimit": {
"measurements": {
"create": {
"type": "user",
"limit": 250,
"remaining": 240,
"reset": 3599
}
}
},
"credits": {
"remaining": 1000
}
}
12 changes: 12 additions & 0 deletions public/v1/components/responses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ components:
examples:
'0':
$ref: 'examples.yaml#/components/examples/probes'
limits:
description: |
A successful request returns status `200 OK` and a body containing information about the current rate limits and user credits.
content:
application/json:
schema:
$ref: 'schemas.yaml#/components/schemas/Limits'
examples:
noAuthLimits:
$ref: 'examples.yaml#/components/examples/nonAuthenticatedLimits'
authLimits:
$ref: 'examples.yaml#/components/examples/authenticatedLimits'
51 changes: 51 additions & 0 deletions public/v1/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1360,3 +1360,54 @@ components:
alt:
type: string
description: The subject's alternative names.
Limits:
type: object
required:
- rateLimit
properties:
rateLimit:
type: object
description: Object containing rate limits information.
required:
- measurements
properties:
measurements:
type: object
description: Measurements rate limits.
required:
- create
properties:
create:
allOf:
- $ref: 'schemas.yaml#/components/schemas/RateLimitDetails'
- description: Rate limit for creating measurements.
credits:
type: object
description: Object containing credits information (only for authenticated requests).
properties:
remaining:
type: integer
description: The number of user's remaining credits.
RateLimitDetails:
type: object
required:
- type
- limit
- remaining
- reset
properties:
type:
type: string
description: Type of the rate limit.
enum:
- ip
- user
limit:
type: integer
description: The number of rate limit points available in a given time window.
remaining:
type: integer
description: The number of rate limit points remaining in the current time window.
reset:
type: integer
description: The number of seconds until the limit resets.
15 changes: 15 additions & 0 deletions public/v1/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ servers:
tags:
- name: Measurements
- name: Probes
- name: Limits
paths:
/v1/measurements:
post:
Expand Down Expand Up @@ -120,6 +121,20 @@ paths:
$ref: 'components/responses.yaml#/components/responses/probes200'
tags:
- Probes
/v1/limits:
get:
summary: Get current rate limits
operationId: getLimits
description: |
Returns rate limits for the current user (if authenticated) or IP address (if not authenticated).
responses:
'200':
$ref: 'components/responses.yaml#/components/responses/limits'
tags:
- Limits
security:
- {}
- bearerAuth: []
components:
securitySchemes:
bearerAuth:
Expand Down
11 changes: 2 additions & 9 deletions src/lib/credits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ export class Credits {
}

async getRemainingCredits (userId: string): Promise<number> {
const result = await this.sql(CREDITS_TABLE).where({ user_id: userId }).select<[{ amount: number }]>('amount');

const remainingCredits = result[0]?.amount;

if (remainingCredits || remainingCredits === 0) {
return remainingCredits;
}

throw new Error('Credits data for the user not found.');
const result = await this.sql(CREDITS_TABLE).where({ user_id: userId }).first<{ amount: number } | undefined>('amount');
return result?.amount || 0;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import domainRedirect from './middleware/domain-redirect.js';
import { docsLink } from './middleware/docs-link.js';
import type { CustomContext } from '../../types.js';
import { registerAlternativeIpRoute } from '../../alternative-ip/route/alternative-ip.js';
import { registerLimitsRoute } from '../../limits/route/get-limits.js';

const app = new Koa();
const publicPath = url.fileURLToPath(new URL('.', import.meta.url)) + '/../../../public';
Expand Down Expand Up @@ -64,6 +65,8 @@ registerGetProbesRoute(apiRouter);
registerSendCodeRoute(apiRouter);
// POST /alternative-ip
registerAlternativeIpRoute(apiRouter);
// GET /limits
registerLimitsRoute(apiRouter);

const healthRouter = new Router({ strict: true, sensitive: true });
// GET /health
Expand Down
60 changes: 49 additions & 11 deletions src/lib/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,32 @@ export const authenticatedRateLimiter = new RateLimiterRedis({
duration: config.get<number>('measurement.rateLimitReset'),
});

export const rateLimit = async (ctx: ExtendedContext, numberOfProbes: number) => {
if (ctx['isAdmin']) {
return;
const getRateLimiter = (ctx: ExtendedContext): {
type: 'user'| 'ip',
id: string,
rateLimiter: RateLimiterRedis
} => {
if (ctx.state.user?.id) {
return {
type: 'user',
id: ctx.state.user.id,
rateLimiter: authenticatedRateLimiter,
};
}

let rateLimiter: RateLimiterRedis;
let id: string;
return {
type: 'ip',
id: requestIp.getClientIp(ctx.req) ?? '',
rateLimiter: anonymousRateLimiter,
};
};

if (ctx.state.user?.id) {
rateLimiter = authenticatedRateLimiter;
id = ctx.state.user.id;
} else {
rateLimiter = anonymousRateLimiter;
id = requestIp.getClientIp(ctx.req) ?? '';
export const rateLimit = async (ctx: ExtendedContext, numberOfProbes: number) => {
if (ctx['isAdmin']) {
return;
}

const { rateLimiter, id } = getRateLimiter(ctx);
setRequestCostHeaders(ctx, numberOfProbes);

try {
Expand Down Expand Up @@ -67,6 +77,34 @@ export const rateLimit = async (ctx: ExtendedContext, numberOfProbes: number) =>
}
};

export const getRateLimitState = async (ctx: ExtendedContext) => {
const { rateLimiter, id, type } = getRateLimiter(ctx);
const rateLimiterRes = await rateLimiter.get(id);

if (rateLimiterRes) {
return {
type,
limit: rateLimiter.points,
remaining: rateLimiterRes.remainingPoints,
reset: Math.round(rateLimiterRes.msBeforeNext / 1000),
};
} else if (type === 'user') {
return {
type,
limit: config.get<number>('measurement.authenticatedRateLimit'),
remaining: config.get<number>('measurement.authenticatedRateLimit'),
reset: 0,
};
}

return {
type,
limit: config.get<number>('measurement.anonymousRateLimit'),
remaining: config.get<number>('measurement.anonymousRateLimit'),
reset: 0,
};
};

const consumeCredits = async (userId: string, rateLimiterRes: RateLimiterRes, numberOfProbes: number) => {
const freeCredits = config.get<number>('measurement.authenticatedRateLimit');
const requiredCredits = Math.min(rateLimiterRes.consumedPoints - freeCredits, numberOfProbes);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/ws/helper/reconnect-probes.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import config from 'config';
import { scopedLogger } from '../../logger.js';
import { fetchRawSockets } from '../server.js';

const logger = scopedLogger('reconnect-probes');
const reconnectProbesDelay = config.get<number>('reconnectProbesDelay');

const TIME_UNTIL_VM_BECOMES_HEALTHY = 8000;
const TIME_TO_RECONNECT_PROBES = 2 * 60 * 1000;

const disconnectProbes = async () => {
const sockets = await fetchRawSockets();

for (const socket of sockets) {
setTimeout(() => socket.disconnect(), Math.random() * TIME_TO_RECONNECT_PROBES);
setTimeout(() => socket.disconnect(), Math.random() * reconnectProbesDelay);
}
};

export const reconnectProbes = () => {
if (!reconnectProbesDelay) {
return;
}

setTimeout(() => {
disconnectProbes().catch(error => logger.error(error));
}, TIME_UNTIL_VM_BECOMES_HEALTHY);
Expand Down
28 changes: 28 additions & 0 deletions src/limits/route/get-limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type Router from '@koa/router';
import { getRateLimitState } from '../../lib/rate-limiter.js';
import type { ExtendedContext } from '../../types.js';
import { credits } from '../../lib/credits.js';
import { authenticate } from '../../lib/http/middleware/authenticate.js';
import { corsAuthHandler } from '../../lib/http/middleware/cors.js';

const handle = async (ctx: ExtendedContext): Promise<void> => {
const [ rateLimitState, remainingCredits ] = await Promise.all([
getRateLimitState(ctx),
ctx.state.user?.id && credits.getRemainingCredits(ctx.state.user.id),
]);

ctx.body = {
rateLimit: {
measurements: {
create: rateLimitState,
},
},
...(ctx.state.user?.id && { credits: {
remaining: remainingCredits,
} }),
};
};

export const registerLimitsRoute = (router: Router): void => {
router.get('/limits', '/limits', corsAuthHandler(), authenticate(), handle);
};
Loading