NestJS句柄超时



我有一个繁重的请求,需要5分钟以上才能执行。在我的日志中,我看到NestJS每5分钟就会抛出一个新请求,但我没有看到来自浏览器的请求。按照建议,我设置了

const app = await app.listen();
app.setTimeout(1800000);

在我的路线

@Get('/foo')
async foo(@Req() req) {
req.setTimeout(1800000);
//...
}

但它不起作用,我每隔5分钟就会在日志中看到新的请求。我知道,最好的解决方案是创建一个队列并异步处理它,但目前我只需要以某种方式增加超时时间。有可能吗?

您应该使用nginx或apache。

proxy_read_timeout 5;
proxy_connect_timeout 5;
proxy_send_timeout 5;

但如果你想把它设置在express上,试试这个:http://expressjs.com/en/resources/middleware/timeout.html

你试过这个吗?

  • 嵌套Js:https://docs.nestjs.com/interceptors#more-操作员
  • 对于快递:https://github.com/expressjs/timeout

示例:

import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
timeout(5000),
catchError(err => {
if (err instanceof TimeoutError) {
return throwError(() => new RequestTimeoutException());
}
return throwError(() => err);
}),
);
};
};

问题就在这里:

nginx.ingress.kubernetes.io/proxy-read-timeout: 300

最新更新