We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I'm trying to "live stream" the content of file that I'm live generating form within the server.
When running the following code, the client would hang indefinitely once all the data chunks read, as if the Read method never returned EOF ?
Maybe I'm missing something ? thanks for any help 🙏🏻
Currently, the server approach looks like (simplified for reproducibility):
ctx.Response.SetBodyStreamWriter(func(w *bufio.Writer) { defer w.Flush() for i := 0; i < 10; i++ { _, err := w.Write([]byte(fmt.Sprintf("chunk %d\n", i))) if err != nil { // handling error return } w.Flush() } })
And the client read the body with a simple:
buf := make([]byte, 1) for { _, err := resp.Body.Read(buf) if err != nil { if err != io.EOF { fmt.Println("failed to read response body:", err) } break } fmt.Print(string(buf[0])) }
The text was updated successfully, but these errors were encountered:
Do you have a standalone reproducible example?
For example the following code works fine:
package main import ( "bufio" "fmt" "github.com/valyala/fasthttp" ) func handler(ctx *fasthttp.RequestCtx) { ctx.Response.SetBodyStreamWriter(func(w *bufio.Writer) { defer w.Flush() for i := 0; i < 10; i++ { _, err := w.Write([]byte(fmt.Sprintf("chunk %d\n", i))) if err != nil { // handling error return } w.Flush() } }) } func main() { if err := fasthttp.ListenAndServe(":8080", handler); err != nil { panic(err) } }
% curl localhost:8080 -v * Trying 127.0.0.1:8080... * Connected to localhost (127.0.0.1) port 8080 (#0) > GET / HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.88.1 > Accept: */* > < HTTP/1.1 200 OK < Server: fasthttp < Date: Sat, 08 Jul 2023 07:49:17 GMT < Content-Type: text/plain; charset=utf-8 < Transfer-Encoding: chunked < chunk 0 chunk 1 chunk 2 chunk 3 chunk 4 chunk 5 chunk 6 chunk 7 chunk 8 chunk 9 * Connection #0 to host localhost left intact
Sorry, something went wrong.
No branches or pull requests
I'm trying to "live stream" the content of file that I'm live generating form within the server.
When running the following code, the client would hang indefinitely once all the data chunks read, as if the Read method never returned EOF ?
Maybe I'm missing something ? thanks for any help 🙏🏻
Currently, the server approach looks like (simplified for reproducibility):
And the client read the body with a simple:
The text was updated successfully, but these errors were encountered: