nodejs / nodejs/undici

Allow passing request context to PoolOptions

Open
#3,979 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

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.

https://github.com/nodejs/undici/blob/03c98d5a683df54c47ee49b8db0ffd7d56f45525/lib/dispatcher/agent.js#L78-L88

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading lib/dispatcher/agent.js at the linked PoolOptions.factory call and trace how DispatchOptions reaches that entry point. Confirm the public API implications and existing behavior; done means the factory can receive the request context as a third argument without breaking existing callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.