-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
FR: Callback when response is written #1671
Comments
Are we talking about a server handler or a client response? |
Server handler Groetjes, |
I think this is good feature, for some server applications that handle large of requests and the buffer was cached, using If support callback or closer interface,... when write the response body completed get will helpful in this case. |
I've looked at the code for a bit and I think that my use-case is satisfied by using Would that work for you too @pisken? |
I see |
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. 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. |
Sounds like a good idea, a pull request is welcome! |
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:
which then gets called after the request has completed (and it is thus safe for me to free the body)
The text was updated successfully, but these errors were encountered: