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

Streaming模式并不实时 #15

Open
vaxin opened this issue Apr 7, 2023 · 2 comments
Open

Streaming模式并不实时 #15

vaxin opened this issue Apr 7, 2023 · 2 comments

Comments

@vaxin
Copy link

vaxin commented Apr 7, 2023

io.Copy无法保证实时转发streaming流,需要改进

`buf := make([]byte, 1024*1024) // 1MB buffer
for {

n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}
if n == 0 {
    break
}
if _, err := w.Write(buf[:n]); err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}
w.(http.Flusher).Flush()

}`

@nnnpa31
Copy link

nnnpa31 commented Jun 29, 2023

请教 io.Copy 不能实时转发的原因可能是什么?
我注意到这里代码有一条 w.(http.Flusher).Flush() ,这是修复实时性的关键吗?

@geekr-dev
Copy link
Owner

请教 io.Copy 不能实时转发的原因可能是什么? 我注意到这里代码有一条 w.(http.Flusher).Flush() ,这是修复实时性的关键吗?

io.Copy 有内存缓冲区的 缓冲区满了才会返回

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