Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dns interceptor with Pool #3957

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

luddd3
Copy link
Contributor

@luddd3 luddd3 commented Dec 17, 2024

This relates to...

Rationale

The DNS interceptor didn't work with Pool/Client since it didn't get the origin unless included with each request.

Changes

Features

Bug Fixes

  • fix dns interceptor with Client/Pool

Breaking Changes and Deprecations

Status

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
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

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

The origin should always be in origDispatchOpts. This seems to be a hack.

@luddd3
Copy link
Contributor Author

luddd3 commented Dec 17, 2024

The origin should always be in origDispatchOpts. This seems to be a hack.

I kinda agree, but didn't find where else to put it?

lib/interceptor/dns.js Outdated Show resolved Hide resolved
lib/interceptor/dns.js Outdated Show resolved Hide resolved
lib/interceptor/dns.js Outdated Show resolved Hide resolved
@luddd3
Copy link
Contributor Author

luddd3 commented Dec 18, 2024

I made an alternative solution. It currently breaks a test for test/interceptors/retry.js and I'm unsure if it is an actual error or not? Should I just change the expected value?

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.

Just adjust the retry tests and should be ok to go.

The change is kind of expected as the origin is being explicitly passed and is the first origin the client is facing

lib/interceptor/dns.js Outdated Show resolved Hide resolved
lib/interceptor/dns.js Outdated Show resolved Hide resolved
@luddd3 luddd3 requested review from metcoder95 and ronag December 19, 2024 13:34
@ronag
Copy link
Member

ronag commented Dec 19, 2024

This still looks weird... where/how does the origin get lost?

@luddd3
Copy link
Contributor Author

luddd3 commented Dec 19, 2024

The interceptor does only have access to the options called by .request(). The request method has access to all variables in this (e.g Pool) since it is bound to this here:

let dispatch = this.dispatch.bind(this)

It could probably be solved in the same manner for the interceptor by changing:

dispatch = interceptor(dispatch)

Into:

dispatch = interceptor.bind(this)(dispatch)

or something like this (which is a bit like my original approach):

dispatch = interceptor(dispatch, this)

The last method has the benefit of allowing the interceptors to return arrow functions as well as ordinary functions.

@mcollina
Copy link
Member

THis now conflicts, can you rebase?

@luddd3 luddd3 force-pushed the fix-pool-dns-interceptor branch from 43a4023 to da237d9 Compare December 20, 2024 05:59
Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

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

This is wrong... not sure when I have time to look into it. Christmas...

@metcoder95
Copy link
Member

metcoder95 commented Dec 20, 2024

Let me try to check it during the weekend; if there's a workaround, let's take it, otherwise this should be ok for now and we can revisit later so dns is unblocked

ronag

This comment was marked as resolved.

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

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

The problem here is the assumption that you don't have to pass origin in the request options if used with Client or Pool, which IMHO is incorrect and it's working as intended.

If anything maybe the dns interceptor should check for origin and throw an invalid arg error. Alternatively, if no origin is passed then the dns interceptor is a noop.

@ronag
Copy link
Member

ronag commented Dec 20, 2024

diff --git a/lib/interceptor/dns.js b/lib/interceptor/dns.js
index c8d56c2c..c6a1a480 100644
--- a/lib/interceptor/dns.js
+++ b/lib/interceptor/dns.js
@@ -342,6 +342,10 @@ module.exports = interceptorOpts => {
 
   return dispatch => {
     return function dnsInterceptor (origDispatchOpts, handler) {
+      if (origDispatchOpts.origin == null) {
+        return dispatch(origDispatchOpts, handler)
+      }
+
       const origin =

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

@luddd3
Copy link
Contributor Author

luddd3 commented Dec 21, 2024

The problem here is the assumption that you don't have to pass origin in the request options if used with Client or Pool, which IMHO is incorrect and it's working as intended.

@ronag That was my assumption. Just to be clear, do you think it should be like this?

const client = new Pool('https://google.com', {
    connections: 10
})
await client.request({
    origin: 'https://google.com', // origin must be included in each request
    method: 'GET',
    path: '/'
})

If that is the case, then I think there are a lot of inconsistencies and contradictions in both the code and documentation.

@ronag
Copy link
Member

ronag commented Dec 21, 2024

If that is the case, then I think there are a lot of inconsistencies and contradictions in both the code and documentation.

Possibly. Then let's just make the dns interceptor a noop when origin is missing as I suggested with the patch.

@metcoder95
Copy link
Member

I do agree that the documentation has some statements that are either not fully true or confusing for the reader that can lead to a wrong assumption; but that's at an overall take.

The last being said; I'm not 100% in sync with the statement that passing the origin should be mandated on every request call. I'm in sync when use through dynamic dispatchers like Agent or the BalancedPool, but not fully in sync for Client and Pool which are tight to a single origin.

For the latter use-cases, they should have the origin already stick to their state and do not ask the caller to pass it as its goal is to be tight to a single downstream.

I'm ok with the dns interceptor doing a noop if no origin is passed or if it is already an IP address; but the fact that Pool and Client does not forward the origin seems a bit of miss alignment with its purpose

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.

4 participants