Skip to content

Introduce a ConnectionTarget enum #501

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

Merged
merged 2 commits into from
Nov 30, 2021
Merged

Conversation

karwa
Copy link
Contributor

@karwa karwa commented Nov 26, 2021

I added this as part of the WebURL port, because it can provide specific host kinds and doesn't need to re-parse the hostname, but I think it is generally a useful thing to model.

Currently, ConnectionPool.Key stores a host, port, and socketPath string, even though only one of (host + port) and (socketPath) can be populated. This lends itself well to an enum. Additionally, we can extract what kind of host we're dealing with, so we don't need to re-parse the hostname later.

One thing I'd like comments on is whether this should perhaps be moved up to Request. This would also allows us to stop pretending that unix domain sockets have ports. As I see it, this should really be Request's business; a part of how it extracts information from the URL should be parsing the hostname string (perhaps using APIs from the URL type, if it provides that).

The async redesign simplifies Request somewhat, but I couldn't see any mention of hosts or hostnames on the new request type. One idea worth exploring would be making this public API as part of that redesign.

@karwa
Copy link
Contributor Author

karwa commented Nov 26, 2021

OK, I just did it and moved it up in to Request. I couldn't think of a good reason not to.

@karwa karwa force-pushed the a-good-host branch 2 times, most recently from 0a7f3f7 to 2b58022 Compare November 26, 2021 16:31
@Lukasa Lukasa requested review from fabianfett and dnadoba November 26, 2021 17:01
@karwa
Copy link
Contributor Author

karwa commented Nov 26, 2021

One idea I'd like to float is potentially naming this ConnectionTarget, since I'm not sure unix sockets really count as "hosts" in the typical sense (Request.host currently returns an empty string for them, even though HTTP requests to empty hosts are technically invalid).

Also, it would avoid overloading the term "host" too much. It already clashes with Request.host, meaning it requiring an underscored or other suboptimal name. ("request target" can also be ambiguous, it has a lot of meanings in the HTTP spec).

@Lukasa
Copy link
Collaborator

Lukasa commented Nov 26, 2021

ConnectionTarget is a good name, I'm in favour of that amendment.

@karwa
Copy link
Contributor Author

karwa commented Nov 26, 2021

OK, I've changed it to ConnectionTarget, and added tests for the quirky embedded IPv4 addresses.

@karwa karwa changed the title [RFC] Introduce a Host enum Introduce a ConnectionTarget enum Nov 26, 2021
@dnadoba
Copy link
Collaborator

dnadoba commented Nov 26, 2021

Did a quick read through the changes and it is a nice cleanup. Thanks! Will do a proper review on monday.
I'm also refactoring some related stuff like scheme and moving the destructured URL into a separate type. If you are interested you can have a look at this branch https://github.com/dnadoba/async-http-client/tree/dn-scheme
Still work in progress and naming is not final.
Nevertheless, I think we should land your changes first.

Copy link
Collaborator

@dnadoba dnadoba left a comment

Choose a reason for hiding this comment

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

Great patch! Looks good to me modulo one nit. We should still wait for @fabianfett review until we merge it.

w.r.t.:

The async redesign simplifies Request somewhat, but I couldn't see any mention of hosts or hostnames on the new request type. One idea worth exploring would be making this public API as part of that redesign.

The new Request type, called HTTPClientRequest, does not do any parsing or validation. It also just accepts a url as a String. Parsing, destructering and validation of the url is all delayed until execution. The advantage is that the initialiser of HTTPClientRequest is non-throwing and all properties are vars. Therefore we will not even have ConnectionTarget as an internal property on HTTPClientRequest.

My plan is to introduce a new internal type called PreparedRequest which is created as part of executing the request. This type will be the new home for all properties which are computed as part of the current initialiser of HTTPClient.Request and the validation step which is currently also executed before sending out a HTTPClient.Request.

path = self.unixPath
} else {
path = "\(self.host):\(self.port)"
var hostDescription = ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Swift should see that we set hostDescription exactly once on every code path. If we remove the default value "" we should therefore be able to make this a let.

@fabianfett fabianfett requested a review from Lukasa November 29, 2021 17:19
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

One nit, and I'd like to see @dnadoba's nits addressed, but otherwise this is great. Nice patch @karwa!

@@ -45,6 +45,30 @@ final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHand
return self.proxyEstablishedPromise?.futureResult
}

convenience
init(target: ConnectionTarget,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor nit: I'd like to put convenience init on the same line.

@dnadoba
Copy link
Collaborator

dnadoba commented Nov 30, 2021

@karwa I merge it now and fix the nits in a follow up PR quickly because I want to use them in another PR. Thanks again for this patch!

@dnadoba dnadoba merged commit f1a9187 into swift-server:main Nov 30, 2021
dnadoba added a commit to dnadoba/async-http-client that referenced this pull request Nov 30, 2021
@dnadoba dnadoba mentioned this pull request Nov 30, 2021
dnadoba added a commit that referenced this pull request Nov 30, 2021
@fabianfett fabianfett added the 🔨 semver/patch No public API change. label Dec 1, 2021
@karwa
Copy link
Contributor Author

karwa commented Dec 1, 2021

Thanks @dnadoba! I was planning to fix in the evening yesterday, but was too tired 😴

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants