NestJS - 增加特定 http 端点的响应超时
NestJS - Increase response timeout for particular http endpoint
我刚开始了解 NestJS,我想知道如何操作特定端点的响应超时?
我可以在服务器级别上执行此操作,例如:
const server = await app.listen(...);
server.setTimeout(1800000)
或在端点上,看起来很糟糕:
@Post('/test')
public async import(...props, @Res() res: Response): Promise<string> {
res.setTimeout(1800000)
}
但是我怎么能在控制器或方法级别上做到这一点呢?
我尝试使用如下拦截器增加端点超时:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, take, timeout } from 'rxjs/operators';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
timeout(1800000),
catchError(err => {
if (err instanceof TimeoutError) {
return throwError(() => new RequestTimeoutException());
}
return throwError(() => err);
}),
);
};
};
并将其应用于端点,例如:
@Post('/test')
@UseInterceptors(TimeoutInterceptor)
public async import(...props, @Res() res: Response): Promise<string> {
long running code...
}
虽然拦截器被触发所以我能够记录一些东西
超时似乎根本不起作用:/
好的,如果有人好奇这就是我所做的:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse();
response.setTimeout(600000)
return next.handle();
};
};
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
constructor(
private readonly reflector: Reflector,
) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse();
const timeout = this.reflector.get<number>('request-timeout', context.getHandler()) || 60000;
response.setTimeout(timeout )
return next.handle();
};
};
import { applyDecorators, SetMetadata, UseInterceptors } from '@nestjs/common';
const SetTimeout = (timeout: number) => SetMetadata('request-timeout', timeout);
export function SetRequestTimeout(timeout: number = 600000) {
return applyDecorators(
SetTimeout(timeout),
UseInterceptors(TimeoutInterceptor),
);
}
您可能需要尝试一下提供程序(向其添加拦截器)
但现在您可能只是为了方便而使用 @SetRequestTimeout()
或 @SetRequestTimeout(10000)
。
我刚开始了解 NestJS,我想知道如何操作特定端点的响应超时?
我可以在服务器级别上执行此操作,例如:
const server = await app.listen(...);
server.setTimeout(1800000)
或在端点上,看起来很糟糕:
@Post('/test')
public async import(...props, @Res() res: Response): Promise<string> {
res.setTimeout(1800000)
}
但是我怎么能在控制器或方法级别上做到这一点呢? 我尝试使用如下拦截器增加端点超时:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, take, timeout } from 'rxjs/operators';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
timeout(1800000),
catchError(err => {
if (err instanceof TimeoutError) {
return throwError(() => new RequestTimeoutException());
}
return throwError(() => err);
}),
);
};
};
并将其应用于端点,例如:
@Post('/test')
@UseInterceptors(TimeoutInterceptor)
public async import(...props, @Res() res: Response): Promise<string> {
long running code...
}
虽然拦截器被触发所以我能够记录一些东西 超时似乎根本不起作用:/
好的,如果有人好奇这就是我所做的:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse();
response.setTimeout(600000)
return next.handle();
};
};
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
constructor(
private readonly reflector: Reflector,
) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse();
const timeout = this.reflector.get<number>('request-timeout', context.getHandler()) || 60000;
response.setTimeout(timeout )
return next.handle();
};
};
import { applyDecorators, SetMetadata, UseInterceptors } from '@nestjs/common';
const SetTimeout = (timeout: number) => SetMetadata('request-timeout', timeout);
export function SetRequestTimeout(timeout: number = 600000) {
return applyDecorators(
SetTimeout(timeout),
UseInterceptors(TimeoutInterceptor),
);
}
您可能需要尝试一下提供程序(向其添加拦截器)
但现在您可能只是为了方便而使用 @SetRequestTimeout()
或 @SetRequestTimeout(10000)
。