Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor rate limiting

Edward Cui2203796d5454
diff --git a/server/src/api/import/importData.ts b/server/src/api/import/importData.ts
index d5358b986..8cbe4245e 100644
--- a/server/src/api/import/importData.ts
+++ b/server/src/api/import/importData.ts
@@ -50,11 +50,11 @@ export async function importData(
return reply.status(403).send({ error: "Forbidden" });
}
- const orgLimitResult = await ImportRateLimiter.checkRateLimit(organization);
- if (!orgLimitResult.allowed) {
+ const concurrentImportLimitResult = await ImportRateLimiter.checkConcurrentImportLimit(organization);
+ if (!concurrentImportLimitResult.allowed) {
return reply.status(429).send({
error: "Organization limit exceeded",
- message: orgLimitResult.reason,
+ message: concurrentImportLimitResult.reason,
});
}
diff --git a/server/src/lib/rateLimiter.ts b/server/src/lib/rateLimiter.ts
index 677f6fec4..072a7cfb5 100644
--- a/server/src/lib/rateLimiter.ts
+++ b/server/src/lib/rateLimiter.ts
@@ -56,13 +56,17 @@ if (IS_CLOUD) {
}
export class ImportRateLimiter {
- private static readonly IMPORTED_ROWS_LIMIT = 1_000_000;
+ private static readonly IMPORTED_EVENTS_LIMIT = 1_000_000;
private static readonly CONCURRENT_LIMIT = 1;
- static async checkRateLimit(organizationId: string): Promise<{
+ static async checkConcurrentImportLimit(organizationId: string): Promise<{
allowed: boolean;
reason?: string;
}> {
+ if (!IS_CLOUD) {
+ return { allowed: true };
+ }
+
const [concurrentCountResult] = await db
.select({ count: count() })
.from(importStatus)
@@ -80,7 +84,15 @@ export class ImportRateLimiter {
};
}
- const [rowsSumResult] = await db
+ return { allowed: true };
+ }
+
+ static async countImportableEvents(organizationId: string): Promise<number> {
+ if (!IS_CLOUD) {
+ return Infinity;
+ }
+
+ const [importedEvents] = await db
.select({ total: sum(importStatus.totalRows) })
.from(importStatus)
.where(
@@ -90,13 +102,6 @@ export class ImportRateLimiter {
)
);
- if (rowsSumResult.total ?? 0 >= this.IMPORTED_ROWS_LIMIT) {
- return {
- allowed: false,
- reason: `Import row limit of ${this.IMPORTED_ROWS_LIMIT.toLocaleString()} exceeded.`,
- };
- }
-
- return { allowed: true };
+ return this.IMPORTED_EVENTS_LIMIT - Number(importedEvents.total ?? 0);
}
}