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.66k stars 1.75k forks source link

[Question] The Reason Why Fasthttp doesn't support http.Handler method? #1618

Closed xieyuschen closed 1 year ago

xieyuschen commented 1 year ago

The fasthttp.Serve receives a net.Listener and handlers, and then if there are bytes in the listener, fasthttp will read and construct a request, then trigger the handler and finally write the response back to the socket.

My question is, why don't we implement the http.Handler? This question was raised because I want to trigger fasthttp handlers by an http.Request object but I haven't found this method.

The readme gives me a hint. Based on this explanation, I acknowledge it's reasonable that if we support the http.Handler, fasthttp itself will lose its strength because the small to medium requests cannot be reused anymore.

fasthttp was designed for some high performance edge cases. Unless your server/client needs to handle thousands of small to medium requests per second and needs a consistent low millisecond response time fasthttp might not be for you. For most cases net/http is much better as it's easier to use and can handle more cases. For most cases you won't even notice the performance difference.

As I am not familiar to fasthttp much, could I ask you guys this question? Why at the beginning haven't we decided to support it?

Regards

erikdubbelboer commented 1 year ago

If you don't understand things like this you really shouldn't be using fasthttp! There will be no benefit to you, only downsides. You're going to run into all kinds of fasthttp related bugs if you don't completely understand the code. fashttp only has benefits if you really understand how to use it, otherwise it will just be buggy and slower than net/http for you.

Please use net/http!

There is https://pkg.go.dev/github.com/valyala/fasthttp/fasthttpadaptor if you really need it, but if you're going to use that you might as well completely use net/http as all speed benefits are gone.