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

client doesn't support text/event-stream #998

Closed
youfu-fun opened this issue Mar 17, 2021 · 5 comments
Closed

client doesn't support text/event-stream #998

youfu-fun opened this issue Mar 17, 2021 · 5 comments

Comments

@youfu-fun
Copy link

I want to request an API with text / event stream return. but I don't get any data by resp.Body(),resp.Body() len = 0.
Actually, the correct return is shown in Charles, what should I do?
Code:

req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.Header.SetMethod("GET")  
req.SetRequestURI("http://x/x?cmd=reg_hot")
client:=fasthttp.Client{}
client.Dial = fasthttpproxy.FasthttpHTTPDialer("127.0.0.1:8888")
go func() {
  err := fasthttp.Do(req,resp)
  if err != nil {
	  log.Fatal(err)
  }
}()
for {
  fmt.Println(resp.Body())
  time.Sleep(time.Second)
}

output:
[]
[]
[]
[]

Charles:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/event-stream;charset=UTF-8
Connection: keep-alive

data:{"returncode":0,"message":null,"result":null}

data:{"returncode":0,"message":null,"result":null}

data:{"returncode":0,"message":null,"result":null}

data:{"returncode":0,"message":null,"result":null}
@erikdubbelboer
Copy link
Collaborator

This is not supported because the fasthttp Client always waits for the whole response before it returns. With an event stream there is no end to the body so it would never return. You'll have to use net/http for this.

@dkuerner
Copy link
Contributor

@erikdubbelboer Is there a possibility to support text/event-stream content type? Or what are the reasons for fasthttp not supporting this ? Thank you

@erikdubbelboer
Copy link
Collaborator

I'm afraid it's still not possible for the same reason above, that fasthttp always reads the full body.

What is your usecase that you need to do this but need the performance of fasthttp?

@dkuerner
Copy link
Contributor

dkuerner commented Aug 2, 2021

Well, we integrated fasthttp in favour of net/http in a reverse proxy for two main reasons. The main reason is, that it does support preserving the header case. An additional benefit is, as you already mentioned, the performance.
However, fasthttp cannot be used as a drop-in replacement for net/http and that was unclear to us from just going through the documentation.

For us, there are two major caveats with fasthttp, which are:

  • no support for HTTP/2 (we would like to proxy GRPC which would require HTTP/2)
  • no support for Server-Sent-Events (this very issue)

On the former, there seems to be some progress, but it does not seem to be ready for production use-cases.
On the latter, you are arguing that performance outweighs features required for certain use cases.
That is all understandable, but I think these two caveats should be boldly highlighted somewhere in the Readme/docs, to make people considering fasthttp aware, that it is not a drop-in replacement for net/http.
What do you think?

@erikdubbelboer
Copy link
Collaborator

For a while now we have had body streaming for Server. In theory this could be modified to be used on the Client as well in which case it could support text/event-stream. But I'm afraid I don't have any time to work on this. Any help would be welcome!

A pull request that adds documentation for this is also welcome.

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

3 participants