feat: enhance timeout settings for AI endpoints in TimeoutInterceptor#1496
feat: enhance timeout settings for AI endpoints in TimeoutInterceptor#1496
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances the TimeoutInterceptor to support different timeout values for AI endpoints versus standard endpoints. AI endpoints require significantly longer timeouts (5-10 minutes) to accommodate longer processing times, while other endpoints maintain the existing 15-second timeout in production.
Changes:
- Added logic to detect AI endpoints by checking if the request URL includes '/ai/v2/request/'
- Configured AI endpoints with 300-second (5-minute) timeout in production and 600-second (10-minute) timeout in test environments
- Maintained existing 15-second and 200-second timeouts for non-AI endpoints in production and test environments respectively
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const timeoutMs = process.env.NODE_ENV !== 'test' ? 15000 : 200000; | ||
| intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
| const request = context.switchToHttp().getRequest(); | ||
| const isAIEndpoint = request.url.includes('/ai/v2/request/'); |
There was a problem hiding this comment.
Using string matching with includes() for route detection is fragile and may match unintended URLs. Consider using a more robust approach like checking the route path from the controller metadata or using a regular expression with word boundaries to ensure exact path matching.
| const isAIEndpoint = request.url.includes('/ai/v2/request/'); | |
| const isAIEndpoint = /^\/ai\/v2\/request(\/|$)/.test(request.url); |
| const timeoutMs = isAIEndpoint | ||
| ? process.env.NODE_ENV !== 'test' | ||
| ? 300000 | ||
| : 600000 | ||
| : process.env.NODE_ENV !== 'test' | ||
| ? 15000 | ||
| : 200000; |
There was a problem hiding this comment.
The nested ternary operators make this logic difficult to read and maintain. Consider extracting timeout values into named constants (e.g., AI_TIMEOUT_PROD, AI_TIMEOUT_TEST) and using a more straightforward if-else structure or a lookup object.
No description provided.