Skip to content

Support for setting keepAliveTimeout #103

Description

@joao-rebelo

Hi,

I was making some tests with the Echo container, including ensuring connection pool logics / configuration.
Currently this is taking the default 5s Node keepAliveTimeout which will then start dropping connections. If we would be able to stablish the keepAliveTimeout we would be able to allow the consumer side to manage the connections.

I've hacked a solution as:

// How long an idle keep-alive connection is held before the server closes it.
// Node's default is 5s, so an upstream keep-alive pool in front of this server can never be observed intact. 
// Raising it past the drain time lets the pool settle and leaves the reverse proxy in charge of trimming it.
const keepAliveTimeoutMs = parseInt(process.env.KEEPALIVE_TIMEOUT_MS, 10);
if (Number.isFinite(keepAliveTimeoutMs) && keepAliveTimeoutMs > 0) {
    for (const server of [httpServer, httpsServer]) {
        server.keepAliveTimeout = keepAliveTimeoutMs;
        // headersTimeout bounds how long a started request may take to send its headers; 
        // keeping it above keepAliveTimeout stops Node from closing a pooled connection just as it is being reused.
        server.headersTimeout = keepAliveTimeoutMs + 5000;
    }
    console.log(`Keep-alive timeout set to ${keepAliveTimeoutMs}ms (headers timeout ${keepAliveTimeoutMs + 5000}ms).`);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions