Home > other >  NestJS - Increase response timeout for particular http endpoint
NestJS - Increase response timeout for particular http endpoint

Time:05-20

I just started to learn about NestJS and I am wondering how could I manipulate response timeout for particular endpoints?

I could do it on a server level like:

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

or on endpoint, which looks bad:

  @Post('/test')
  public async import(...props, @Res() res: Response): Promise<string> {
    res.setTimeout(1800000)
  }

But how could I do that on controller or method level? I have tried to increase timeout on endpoint using interceptors like:

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);
      }),
    );
  };
};

And applying it on endpoint like:

  @Post('/test')
  @UseInterceptors(TimeoutInterceptor)
  public async import(...props, @Res() res: Response): Promise<string> {
    long running code...
  }

Although interceptor is triggered so I am able to log something the timeout does not seems to work at all :/

CodePudding user response:

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),
  );
}

You might have to play a bit with providers (add the interceptor to it) But now you might only use @SetRequestTimeout() or @SetRequestTimeout(10000) for convenience.

CodePudding user response:

Okay, if someone is curious this is what I have done:

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();
  };
};
  • Related