Skip to content

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

Simplify

Edward Cui37a99a477504
diff --git a/server/src/services/import/importQuotaManager.ts b/server/src/services/import/importQuotaManager.ts
index a40209445..49c4e4c7d 100644
--- a/server/src/services/import/importQuotaManager.ts
+++ b/server/src/services/import/importQuotaManager.ts
@@ -6,19 +6,15 @@ interface CachedTracker {
lastAccessed: number;
}
-interface ActiveImport {
- startedAt: number;
-}
-
class ImportQuotaManager {
- private trackers: Map<string, CachedTracker> = new Map();
- private activeImports: Map<string, Set<ActiveImport>> = new Map();
+ private trackers = new Map<string, CachedTracker>();
+ private activeImports = new Map<string, number>(); // stores startedAt timestamp
private readonly CONCURRENT_IMPORT_LIMIT = 1;
private readonly TRACKER_TTL_MS = 30 * 60 * 1000; // 30 minutes
private readonly IMPORT_TIMEOUT_MS = 2 * 60 * 60 * 1000; // 2 hours
- async getTracker(organizationId: string): Promise<ImportQuotaTracker> {
+ async getTracker(organizationId: string) {
const now = Date.now();
const cached = this.trackers.get(organizationId);
@@ -26,105 +22,55 @@ class ImportQuotaManager {
cached.lastAccessed = now;
return cached.tracker;
}
- // Create new tracker
+
const tracker = await ImportQuotaTracker.create(organizationId);
- this.trackers.set(organizationId, {
- tracker,
- lastAccessed: now,
- });
+ this.trackers.set(organizationId, { tracker, lastAccessed: now });
return tracker;
}
startImport(organizationId: string): boolean {
- if (!IS_CLOUD) {
- return true;
- }
+ if (!IS_CLOUD) return true;
- this.cleanupAbandonedImports(organizationId);
-
- let activeSet = this.activeImports.get(organizationId);
- if (!activeSet) {
- activeSet = new Set();
- this.activeImports.set(organizationId, activeSet);
- }
+ const now = Date.now();
+ const startedAt = this.activeImports.get(organizationId);
- if (activeSet.size >= this.CONCURRENT_IMPORT_LIMIT) {
+ // If an import exists and hasn't timed out, limit reached
+ if (startedAt && now - startedAt < this.IMPORT_TIMEOUT_MS && this.CONCURRENT_IMPORT_LIMIT === 1) {
return false;
}
- activeSet.add({
- startedAt: Date.now(),
- });
-
+ // Otherwise start a new import
+ this.activeImports.set(organizationId, now);
return true;
}
completeImport(organizationId: string): void {
- const activeSet = this.activeImports.get(organizationId);
- if (!activeSet) {
- return;
- }
-
- // Remove the first (and only, since limit=1) import
- const firstImport = activeSet.values().next().value;
- if (firstImport) {
- activeSet.delete(firstImport);
- }
-
- if (activeSet.size === 0) {
- this.activeImports.delete(organizationId);
- }
- }
-
- private cleanupAbandonedImports(organizationId: string): void {
- const activeSet = this.activeImports.get(organizationId);
- if (!activeSet) {
- return;
- }
-
- const now = Date.now();
- const toRemove: ActiveImport[] = [];
-
- for (const activeImport of activeSet) {
- if (now - activeImport.startedAt > this.IMPORT_TIMEOUT_MS) {
- toRemove.push(activeImport);
- }
- }
-
- for (const importToRemove of toRemove) {
- activeSet.delete(importToRemove);
- }
-
- if (activeSet.size === 0) {
- this.activeImports.delete(organizationId);
- }
+ this.activeImports.delete(organizationId);
}
cleanup(): void {
const now = Date.now();
- // Clean up stale trackers
- for (const [orgId, cached] of this.trackers.entries()) {
+ // Cleanup stale trackers
+ for (const [orgId, cached] of this.trackers) {
if (now - cached.lastAccessed > this.TRACKER_TTL_MS) {
this.trackers.delete(orgId);
}
}
- // Clean up abandoned imports across all organizations
- for (const orgId of this.activeImports.keys()) {
- this.cleanupAbandonedImports(orgId);
+ // Cleanup timed-out imports
+ for (const [orgId, startedAt] of this.activeImports) {
+ if (now - startedAt > this.IMPORT_TIMEOUT_MS) {
+ this.activeImports.delete(orgId);
+ }
}
}
}
export const importQuotaManager = new ImportQuotaManager();
+// Background cleanup loop
if (IS_CLOUD) {
- setInterval(
- () => {
- importQuotaManager.cleanup();
- },
- 15 * 60 * 1000
- );
+ setInterval(() => importQuotaManager.cleanup(), 15 * 60 * 1000);
}