Skip to content

Allow passing request context to PoolOptions #3979

Open
@blinsay

Description

This would solve...

When creating a new connection, I'd like to do a host lookup based on multiple parts of a request and create a new TLS connection. Sometimes the name of the cert is based on the origin in the URL, sometimes request headers need to be taken into account, and so on.

The implementation should look like...

PoolOptions.factory should take DispatchOptions as a third argument. It could pass the entire request in as a third argument here.

if (!dispatcher) {
dispatcher = this[kFactory](opts.origin, this[kOptions])
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
.on('connectionError', this[kOnConnectionError])
// This introduces a tiny memory leak, as dispatchers are never removed from the map.
// TODO(mcollina): remove te timer when the client/pool do not have any more
// active connections.
this[kClients].set(key, dispatcher)

This is a public API change that exposes a lot more data to PoolOptions.factory, so I didn't want to open a PR without checking with folks first. The diff to agent.js could be nice and small, but I'm not sure what else I'd have to do to make the change.

    if (!dispatcher) {
-     dispatcher = this[kFactory](opts.origin, this[kOptions])
+     dispatcher = this[kFactory](opts.origin, this[kOptions], opts)
        .on('drain', this[kOnDrain])
        .on('connect', this[kOnConnect])
        .on('disconnect', this[kOnDisconnect])
        .on('connectionError', this[kOnConnectionError])

      // This introduces a tiny memory leak, as dispatchers are never removed from the map.
      // TODO(mcollina): remove te timer when the client/pool do not have any more
      // active connections.
      this[kClients].set(key, dispatcher)
    }

I have also considered...

I've considered overriding Agent with a completely custom class to do this - it'd be a lot of work, but it would be nice to write less code and use the hook provided on PoolOptions.

Thanks!

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions