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
proposal: net/http: NewRequest should take url.URL as target #38583
Comments
We can’t change
|
@andybons I can provide a PR for one of the two solutions (IMO exporting the |
Let’s let the proposal review committee take an initial look first. |
We've been very careful not to export context.
Both of these are smart enough not to allocate two requests: only the result of Clone is actually allocated, as the benchmark shows. We already have too much API in net/http. I am skeptical this comes up enough to want/need more here. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
NewRequestWithContext
andNewRequest
takestring
astarget
parameter. Then thetarget
is internally parsed tourl.URL
usingurl.Parse()
.Because a lot of applications out there pre-parse the url for several reasons, this API tends to have a negative impact to the applications. Some examples when this parsing is useless:
In my personal opinion is also more "type safe" to require an url for the target parameter.
What did you expect to see?
A way to build a
http.Request
with a context without the overhead of stringifying the url and parsing it back.Trying to work around the issue
I've tried to build manually the
Request
struct, but to provide a context i need to invokeWithContext
that triggers a copy of the struct.This is a benchmark of the two methods to build the
Request
:NewRequest
obviously allocates less because it doesn't trigger a copy of himself, but it's definitely slower. The reason why it's slower is dominated by theurl.Parse
, as these flamegraphs show:Possible solutions
ctx
of thehttp.Request
should be exported, in order to allow people to build manually thehttp.Request
struct withurl.URL
and avoid the copyingNewRequest
should takeurl.URL
as target parameter, or a new method should be created that takesurl.URL
as target parameterThe text was updated successfully, but these errors were encountered: