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

http.ResponseWriter Flush not work #3347

Closed
puguojingm opened this issue Jun 14, 2023 · 7 comments · Fixed by #3388
Closed

http.ResponseWriter Flush not work #3347

puguojingm opened this issue Jun 14, 2023 · 7 comments · Fixed by #3388
Labels
answered Marks an issue as answered

Comments

@puguojingm
Copy link

Describe the bug

I want to return data with Transfer-Encoding: chunked. After calling the w.(http.Flusher). Flush() method, the client did not immediately receive the data, but waited until the entire response was finished before returning the data. The same code using the native net/http can return data in real-time.

net/http:

func httpServer() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		flusher, ok := w.(http.Flusher)
		if !ok {
			panic("expected http.ResponseWriter to be an http.Flusher")
		}
		w.Header().Set("X-Content-Type-Options", "nosniff")
		for i := 1; i <= 20; i++ {
			tmp := NlpResp{
				MatchID: i,
				Audio:   "abc",
				Hai:     "data",
				Text:    "test",
			}
			marshal, err := json.Marshal(tmp)
			if err != nil {
				return
			}
			_, err = w.Write(marshal)
			if err != nil {
				log.Print(err)
				return
			}

			flusher.Flush() // Trigger "chunked" encoding and send a chunk...
			time.Sleep(1 * time.Second)
		}
	})

	log.Print("Listening on localhost:8080")
	log.Fatal(http.ListenAndServe("0.0.0.0:8888", nil))
}

go-zero

func AudioHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		flusher, ok := w.(http.Flusher)
		if !ok {
			panic("expected http.ResponseWriter to be an http.Flusher")
		}
		w.Header().Set("X-Content-Type-Options", "nosniff")
		for i := 1; i <= 20; i++ {
			tmp := NlpResp{
				MatchID: i,
				Audio:   "aduio",
				Hai:     "data",
				Text:    "test",
			}
			marshal, err := json.Marshal(tmp)
			if err != nil {
				return
			}
			_, err = w.Write(marshal)
			if err != nil {

				return
			}
			flusher.Flush() // Trigger "chunked" encoding and send a chunk...
			time.Sleep(1 * time.Second)
		}
	}

}

Environments (please complete the following information):

  • OS: [Windows]
  • go-zero version [1.5.3]

More description

@kesonan
Copy link
Collaborator

kesonan commented Jun 14, 2023

I have done a test, the two results are the same, and will not return immediately, only after the data is returned.

@kesonan kesonan added the answered Marks an issue as answered label Jun 14, 2023
@u2nyakim
Copy link
Contributor

哈喽,你们这个问题解决了吗?我今天也碰到了这个。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Hello, have you solved this problem? I also ran into this today.

@linganmin
Copy link
Contributor

linganmin commented Jun 30, 2023

哈喽,你们这个问题解决了吗?我今天也碰到了这个。

先把Timeout的中间件关掉了,临时解决一下

配置文件里

Middlewares:
  Timeout: False

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Hello, have you solved this problem yet? I also ran into this today.

Turn off the Timeout middleware first, and temporarily solve it

configuration file

Middlewares: Timeout: False

@u2nyakim
Copy link
Contributor

哈喽,你们这个问题解决了吗?我今天也碰到了这个。

先把Timeout的中间件关掉了,临时解决一下

配置文件里

Middlewares:
  Timeout: False

Thank you. I have modified the code for the framework and resolved this issue. 🥰

@kevwan kevwan linked a pull request Jul 9, 2023 that will close this issue
@kevwan
Copy link
Contributor

kevwan commented Jul 9, 2023

Fixed.

@kevwan kevwan closed this as completed Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Marks an issue as answered
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants