valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.71k stars 1.75k forks source link

Prebuilding the request #1388

Open comboed opened 2 years ago

comboed commented 2 years ago

I am curious about whether prebuilding the request would make a difference in performance than having to acquire it call it every request.

Example:

func (conn *HTTP) createConnection() {
    conn.Response = fasthttp.AcquireResponse()
    conn.Request = fasthttp.AcquireRequest()

Then simply reusing these vars concurrently. I did attempt to try out this idea, but I failed in return as the response body would come back either empty or with the proper response. The higher the goroutines, the more the response fails. So as said before, would it make a difference if just kept acquiring the request.

erikdubbelboer commented 2 years ago

I'm not sure what you mean? You want to reuse a single Request and Response multiple times? If so, then yes you can do this, but you have to call .Reset() and re-fill the fields before you re-use it. And of course you can't use it concurrently by multiple goroutines.