Nestjs / Axios HttpAgent({keepAlive: true}) causes memory leak

Viewed 2166

Hello i'm trying to figure out the problem that is cause this memory leak

memory usage of ecs task u can see thee increase of memory when we use keepAlive

the nestApp Runs inside a Express App because the old Express app is deprecated and we can migrate an refactor all existing routes to nest without breaking changes for the Frontend.

const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });

export const DEFAULT_CONFIG: AxiosRequestConfig = {
  timeout: 1000,
  httpAgent,
  httpsAgent,
};

export const SOME_CONFIG: AxiosRequestConfig = {
  ...DEFAULT_CONFIG,
  timeout: +process.env.TIMEOUT,
  baseURL: process.env.UPSTREAM,
};

@Module({
  imports: [HttpModule.register(SOME_CONFIG)],
  providers: [SomeUpstreamService],
  exports: [SomeUpstreamService],
})
export class SomeAdapterModule {}

this is the way we init different feature Modules for Upstream APIs

In Legacy Express App we use Axios as well with Http(s)Agent KeepAlive true since 2 years no issues with memory only within the nestjs Part

when I run a small load test (20req/sec) on local I can see a lot of socket Hangup Errors on requests that are made by nest to Upstream APis

I Also tried maxSockets, maxSocketsMs, keepAliveMsecs nothing helped.

I hope Sombody can give me a hint what I missed

0 Answers
Related