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.91k stars 1.76k forks source link

FR: Callback when response is written #1671

Open Jille opened 12 months ago

Jille commented 12 months ago

I want to use Response.SetBodyRaw(), but I need to know when it's safe to deallocate the buffer. I'm writing a cache server and doing memory management manually.

Would you be opposed to adding a callback that I can configure? Something like:

func (r *Response) SetCompletionCallback(callback func()) {
  r.completionCallback = callback
}

which then gets called after the request has completed (and it is thus safe for me to free the body)

erikdubbelboer commented 12 months ago

Are we talking about a server handler or a client response?

Jille commented 12 months ago

Server handler

Groetjes,

pisken commented 11 months ago

I think this is good feature, for some server applications that handle large of requests and the buffer was cached, using SetBodyRaw to get high performance but currently, will unsafe release the buffer. Quick to workaround push the buffer that need release into batcher and process in the after.

If support callback or closer interface,... when write the response body completed get will helpful in this case.

Jille commented 11 months ago

I've looked at the code for a bit and I think that my use-case is satisfied by using SetBodyStream() and passing an io.Closer, which gets called when I can free my underlying data. As far as I can see (by reading the source) both cause exactly one copy when my data gets copied into the writeBuffer to be sent to the client.

Would that work for you too @pisken?

pisken commented 11 months ago

I see SetBodyStream not resolve for large of requests when data body available (caching), and SetBodyRaw can resolve for this case. In this case large of request and data body response was cached, I need write body direct to connection client, no copy to other buffer to keep low memory and cpu used.

Jille commented 11 months ago

AAUI it actually copies in exactly the same scenarios.

There is always a *bufio.Writer (managed by a sync.Pool): https://github.com/valyala/fasthttp/blob/master/server.go#L2407 So during the writing of the request, that memory is allocated.

Write gets that bufio.Writer which already has the headers written to it. bodyBytes returns the []byte that you passed to SetBodyRaw, and then writes it to the bufio.Writer. Unless 1) the *bufio.Writer is empty, and 2) your []byte is larger than its size, it'll copy the data.

You can ensure the buffer is empty by setting ImmediateHeaderFlush on the response, but that's at the cost of an extra syscall (for non-HTTPS), which might or might not be more expensive than copying the bytes.

For SetBodyStream (assuming you've set the contentLength), you'll end up in writeBodyStream here which will call io.CopyBuffer(), which will call YourBodyStream.WriteTo, which can do a single large write, just like the SetBodyRaw case.

erikdubbelboer commented 10 months ago

Sounds like a good idea, a pull request is welcome!