Skip to content

Commit a89adb4

Browse files
authored
Merge pull request #10 from smark-d/master
io.Copy() func has default buffer size 32k, it's too large ,change the size to 512
2 parents 1b58e5b + 3e74917 commit a89adb4

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,24 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
8686
w.WriteHeader(resp.StatusCode)
8787

8888
// 将响应实体写入到响应流中(支持流式响应)
89-
io.Copy(w, resp.Body)
89+
buf := make([]byte, 512)
90+
for {
91+
n, err := resp.Body.Read(buf)
92+
if n > 0 {
93+
_, writeErr := w.Write(buf[:n])
94+
if writeErr != nil {
95+
log.Println("Error writing to response: ", writeErr.Error())
96+
return
97+
}
98+
w.(http.Flusher).Flush()
99+
}
100+
101+
if err == io.EOF {
102+
break
103+
}
104+
if err != nil {
105+
log.Println("Error reading from response: ", err.Error())
106+
return
107+
}
108+
}
90109
}

0 commit comments

Comments
 (0)