-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
client: URI is always parsed #1141
Comments
To be honest I'm not sure why it was implemented like that. Yes I would accept a PR to support such functionality. |
stokito
added a commit
to stokito/fasthttp
that referenced
this issue
Nov 7, 2021
Currently, the only way to set URI for a request is to call SetRequestURI(string). Then when a request performed the string will be parsed into a fasthttp.URI struct. If there are many requests with the same URI then we'll waste CPU for a parsing of the same URI string. With the new SetURI(*URI) method we can once parse a URI string into a fasthttp.URI struct and then reuse it for many requests. Unfortunately the URI will be copied because may be modified inside the request. But anyway this will be more lightweight than parsing.
Thank you for merging, this should be useful if we have a base URI that can be copied and some part can be changed.
var requestPool sync.Pool
func main() {
reqBody := []byte("{\"id\": 1}")
// AcquireRequest
reqV := requestPool.Get()
var req *fasthttp.Request
if reqV != nil {
req = reqV.(*fasthttp.Request)
} else {
req = &fasthttp.Request{}
req.SetRequestURIBytes("http://example.com/api/v1/")
req.Header.SetMethod(fasthttp.MethodPost)
req.Header.SetContentType("application/json")
}
resp := fasthttp.AcquireResponse()
req.SetBodyRaw(reqBody)
err := fasthttp.Do(req, resp)
// Same as fasthttp.ReleaseRequest but without reset
requestPool.Put(req)
fasthttp.ReleaseResponse(resp)
}
I hope this solution may be useful for someone. Interesting if there is any way to make it easier to use.
Maybe we remove the `Reset()` on release and leave this for users. Also we may create a separate type for pools so users may create their own instead of using a global pool.
This is just some thoughts and I don't think that fasthhtp should be changed for now. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To perform an HTTP requests each time the
SetRequestURIBytes()
must be called. But internally the method just saves an URL and it is then parsed inside ofparseURI()
:My application has a bunch of fixed URLs like
http://example.com/api/
and I can call thereq.uri.parse()
only once on initialization. And then I would like to just set thisfasthttp.URI
objects before sending the request with a new methodSetRequestURI(preparsedUri)
.Is any reason why this wasn't implemented? Will you implement this or accept a PR with such functionality?
The text was updated successfully, but these errors were encountered: