Skip to content
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
23 changes: 23 additions & 0 deletions src/analytics/services/batch-size-guard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class BatchSizeGuardService {
private readonly maxSize: number = 10000;
private droppedCount: number = 0;

canAdd(currentSize: number): boolean {
if (currentSize >= this.maxSize) {
this.droppedCount++;
return false;
}
return true;
}

getDroppedCount(): number {
return this.droppedCount;
}

resetDroppedCount(): void {
this.droppedCount = 0;
}
}
33 changes: 33 additions & 0 deletions src/common/interceptors/request-dedup.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class RequestDedupInterceptor implements NestInterceptor {
private cache = new Map<string, { response: any; timestamp: number }>();
private readonly ttlMs = 60000;

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const req = context.switchToHttp().getRequest();
if (req.method !== 'POST') return next.handle();

const key = this.fingerprint(req);
const cached = this.cache.get(key);
if (cached && Date.now() - cached.timestamp < this.ttlMs) {
const res = context.switchToHttp().getResponse();
res.setHeader('X-Duplicate-Request', 'true');
return of(cached.response);
}

return next.handle().pipe(
tap((response) => {
this.cache.set(key, { response, timestamp: Date.now() });
}),
);
}

private fingerprint(req: any): string {
const id = req.user ? req.user.id : 'anon';
return `${req.method}:${req.path}:${JSON.stringify(req.body)}:${id}`;
}
}
Loading