Skip to content
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

SetBodyStreamWriter doesn't set EOF #1591

Open
burgesQ opened this issue Jul 7, 2023 · 1 comment
Open

SetBodyStreamWriter doesn't set EOF #1591

burgesQ opened this issue Jul 7, 2023 · 1 comment

Comments

@burgesQ
Copy link

burgesQ commented Jul 7, 2023

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]))
	}
@erikdubbelboer
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants