Skip to content

Commit 10a0124

Browse files
committed
Update AWS Textract configuration and integrate rate limiting functionality
- Increased the document requests per minute limit in backend/src/config/configuration.ts from 10 to 20. - Imported RateLimiter from security.utils in backend/src/services/aws-textract.service.ts to enhance request management. - Removed the RateLimiter class definition from aws-textract.service.ts as it is now imported from the utility module.
1 parent 49099ad commit 10a0124

File tree

2 files changed

+2
-38
lines changed

2 files changed

+2
-38
lines changed

backend/src/config/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default () => ({
2626
},
2727
textract: {
2828
maxBatchSize: parseInt(process.env.AWS_TEXTRACT_MAX_BATCH_SIZE || '10', 10),
29-
documentRequestsPerMinute: parseInt(process.env.AWS_TEXTRACT_DOCS_PER_MINUTE || '10', 10),
29+
documentRequestsPerMinute: parseInt(process.env.AWS_TEXTRACT_DOCS_PER_MINUTE || '20', 20),
3030
},
3131
},
3232
perplexity: {

backend/src/services/aws-textract.service.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable, Logger, BadRequestException } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { TextractClient, AnalyzeDocumentCommand, Block } from '@aws-sdk/client-textract';
4-
import { validateFileSecurely } from '../utils/security.utils';
4+
import { validateFileSecurely, RateLimiter } from '../utils/security.utils';
55
import { createHash } from 'crypto';
66

77
export interface ExtractedTextResult {
@@ -544,39 +544,3 @@ export class AwsTextractService {
544544
return results;
545545
}
546546
}
547-
548-
/**
549-
* Rate limiting implementation using a rolling window
550-
*/
551-
class RateLimiter {
552-
private requests: Map<string, number[]> = new Map();
553-
private readonly windowMs: number;
554-
private readonly maxRequests: number;
555-
556-
constructor(windowMs = 60000, maxRequests = 20) {
557-
this.windowMs = windowMs;
558-
this.maxRequests = maxRequests;
559-
}
560-
561-
public tryRequest(identifier: string): boolean {
562-
const now = Date.now();
563-
const windowStart = now - this.windowMs;
564-
565-
// Get or initialize request timestamps for this identifier
566-
let timestamps = this.requests.get(identifier) || [];
567-
568-
// Remove old timestamps
569-
timestamps = timestamps.filter(time => time > windowStart);
570-
571-
// Check if limit is reached
572-
if (timestamps.length >= this.maxRequests) {
573-
return false;
574-
}
575-
576-
// Add new request timestamp
577-
timestamps.push(now);
578-
this.requests.set(identifier, timestamps);
579-
580-
return true;
581-
}
582-
}

0 commit comments

Comments
 (0)