Skip to content

Conversation

@JoshMock
Copy link
Contributor

@JoshMock JoshMock commented Jul 30, 2025

This relates to...

Addresses #3268, but only for Agent. Will address BalancedPool in a separate PR.

Rationale

Exposes an optional maxOrigins option on Agent to enforce a maximum number of origins that can have open connections. Throws AgentMaxOriginsReached error if attempting to dispatch a request to a new origin when maxOrigins has already been reached.

Changes

Tracks unique origins in Agent in a Set named kOrigins, alongside its internal kClients map, adding new origins during dispatch, and removing origins when the last connected client for an origin is disconnected. If dispatching the current request would exceed maxOrigins, throws AgentMaxOriginsReached.

Features

  • Optional parameter maxOrigins added to Agent to enforce an origin limit

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!

For Agent the situation is a bit more complicated.
As it manages pool/client per origin, might be better to re-shape the approach for it by limiting the number of origins if desired (defaulting to Inifinity).

If no more room for adding more origins, requests should be dropped; there's no way an space will be available unless explicitly preempted.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! It's a good start! I wonder if it would be better to maintain a counter in the Agent instead of sum it up at every request.

@JoshMock
Copy link
Contributor Author

For Agent the situation is a bit more complicated.
As it manages pool/client per origin, might be better to re-shape the approach for it by limiting the number of origins if desired (defaulting to Inifinity).

This approach makes sense to me, as controlling max connections would just become a downstream effect of limiting origins, with max connections effectively becoming origins * connections.

If no more room for adding more origins, requests should be dropped

When you say it's dropped, you mean that Agent shouldn't have a queue at all? If so, would it throw an exception like AgentMaxOriginsReached?

@JoshMock
Copy link
Contributor Author

Okay, I've simplified it to be an origins restriction in Agent, which simplified things quite a bit. Agent[kOrigins] tracks unique origins in its own Set now.

I kept the queue in Agent until I get clarification from @metcoder95 on my question about whether to queue or throw an error. If we do keep the queue, should I switch it from an array to a FixedQueue, to align it more closely with PoolBase as you suggested here?

@JoshMock JoshMock requested review from mcollina and metcoder95 July 31, 2025 22:12
@metcoder95
Copy link
Member

When you say it's dropped, you mean that Agent shouldn't have a queue at all? If so, would it throw an exception like AgentMaxOriginsReached?

Exactly; if a max limit of origins has been reached, I'm doubtful new connections to origins will be dropped in a timely manner (staling the request), as is complicated to ensure when a connection will be dropped to leave space for a new one.

@JoshMock JoshMock requested a review from metcoder95 August 1, 2025 19:33
@JoshMock
Copy link
Contributor Author

JoshMock commented Aug 1, 2025

@metcoder95 Agent now throws an error on dispatch when maxOrigins is reached, rather than managing its own queue. Please take a look!

@JoshMock JoshMock changed the title Support for capping the total number of connections across all origins Support for capping the number of origins in Agent Aug 1, 2025
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm already, just test for types is missed and should be good to go

@JoshMock JoshMock requested a review from metcoder95 August 4, 2025 19:23
@JoshMock
Copy link
Contributor Author

Merged main in to account for the conflict introduced by #4425, and updated the description to use the PR template.

@metcoder95 this is ready to merge when you approve the latest merge commit.

@JoshMock JoshMock requested a review from metcoder95 August 21, 2025 18:55
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the Error suffix please.

Other than that codewise looks ok.

@JoshMock JoshMock requested review from Uzlopak and metcoder95 August 22, 2025 18:24
@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 22, 2025

I am actually now a little bit hesitant. Should we really call it AgentMaxOriginsReachedError? Or should it be just MaxOriginsReachedError?

@metcoder95
@mcollina

Could this be also be used in Pools or Clients? If so, we should remove the Prefix "Agent".

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 22, 2025

@JoshMock

Dont change yet the code. Lets clarify first what the error is "doing", before you do extra work.

@JoshMock
Copy link
Contributor Author

JoshMock commented Aug 25, 2025

Lets clarify first what the error is "doing"

I'm not sure I understand what you mean. 🤔 Can you clarify?

I tend to agree with @Uzlopak that a more generic MaxOriginsReachedError is probably more useful, especially since it would be reusable if I add the same functionality to BalancedPool as I've planned. If we wanted to customize the error for each use case, the error message could be overridden rather than having a different Error class for each.

It's a quick change, so let me know if that's how we want to proceed.

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 25, 2025

If you consider to use it in various places, than you can generalize it of course. Please proceed ;)

@JoshMock
Copy link
Contributor Author

@Uzlopak updated in 5994344. 🙏

@JoshMock JoshMock requested a review from Uzlopak August 26, 2025 18:50
@JoshMock
Copy link
Contributor Author

JoshMock commented Sep 2, 2025

@Uzlopak @metcoder95 Any update on when we can merge this?

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We changed the Error class thing with instanceof and symbols.

LGTM

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 4, 2025

@metcoder95
@mcollina

can someone of you reapprove?

@codecov-commenter
Copy link

codecov-commenter commented Sep 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.53%. Comparing base (13b2874) to head (b98b816).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4365   +/-   ##
=======================================
  Coverage   93.53%   93.53%           
=======================================
  Files         103      103           
  Lines       32349    32378   +29     
=======================================
+ Hits        30257    30286   +29     
  Misses       2092     2092           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@metcoder95 metcoder95 merged commit 0395679 into nodejs:main Sep 5, 2025
25 of 29 checks passed
@metcoder95
Copy link
Member

Failures are unrelated to the PR itself

@github-actions github-actions bot mentioned this pull request Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants