Skip to content

Max requests per socket #40071

@artur-ma

Description

@artur-ma

Is your feature request related to a problem? Please describe.
We are scaling up pods in K8S on traffic spike, but since we are using keep alive, new pods are barley used since it takes too much time to rebalance the connections, as a result we have pods with high CPU usage, and pods with very low CPU usage.

Describe the solution you'd like
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive

HTTP standard support "max" parameter on "Keep-Alive" which defines that the connection can be used for up to "N" number of requests, then it will be closed.

The suggestion is to add this configuration on http.server that will return this parameter to the client, and also close the connection once it reached the maximum

Describe alternatives you've considered
This issue can be solved with side tools like service mesh (LinkerD or Istio) but they have their own issues and overhead.

We ended up doing it manually by creating some plugin for fastify that holds a WeakMap with a counter.

Some context: fastify/fastify#3260

The solution that we ended up with

  const counters = new WeakMap();
  ...
  let count = counters.get(socket)

  if (count === undefined) {
    count = 0;
  }

  counters.set(socket, ++count);

  if (count >= maxRequestsPerConnection) {
    reply.header('connection', 'close');
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions