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

Question regarding streaming large bodies #1647

Open pablolagos opened 1 year ago

pablolagos commented 1 year ago

Hello everyone. I know this has been asked several times, but it's been a while and there have been many changes in fasthttp.

Is there a way to copy a body directly from the source to the user without having to get it completely in memory?

This involves two aspects:

  1. client: Is it able to make a request and not download the body automatically?
  2. server: I have seen that there are some options, but none within the context of RequestCtx.
pablolagos commented 1 year ago

Hello again, We currently need this functionality and I think it could be beneficial to everyone. Basically, the idea is:

In server

func handle(ctx *fasthttp.RequestCtx) {
     // DisableBuffering modifies fasthttp to disable headers and body buffering for this request
     ctx.DisableBuffering()

     // Write headers
     ctx.Response.Header.Set(.....)
     ctx.Response.Header.Set(.....)
     ctx.Response.Header.Set(.....)

     // Write body. Headers will be sent and cannot be modified once the body begins to be dispatched.
     // src can be any io.Reader or io.ReaderFrom or any object implementing io.WriteTo
     // The process would be unbuffered, well, actually using at most a 32KB buffer 
     // according to the current io.Copy implementation.
     // Ideal for serving large files without storing them on RAM. 
     io.Copy(ctx, src)
}

It would automatically set the Transfer-Encoding header to "chunked" and chunk the content on the fly.

I can write a PR if you think it makes sense for fasthttp

erikdubbelboer commented 1 year ago

It could make sense but I'm afraid it will be a lot harder to implement correctly than you think. If you can try to make a PR please do.