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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: