Open
Description
Chunked encoding allows HTTP endpoint to stream the results back to the client incrementally, which works well with our Query Service v2 streaming interface. The http.Server
should support that automatically through the use of http.Flusher
API
func handler(w http.ResponseWriter, r *http.Request) {
// Simulate a long-running process that produces data in chunks
for i := 0; i < 5; i++ {
chunk := fmt.Sprintf("Chunk %d\n", i)
fmt.Fprintln(w, chunk)
w.(http.Flusher).Flush() // Flush the buffer immediately
time.Sleep(1 * time.Second) // Simulate work between chunks
}
}
However, this ^ example may be wrong and we may need to do something special for chunked encoding, which according to spec should look like
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/plain
1a
This is the first chunk.
1c
This is the second chunk.
20
This is the third chunk.
0
(i.e. each chunk is preceeded by a hex-encoded length on a new line).
The example of reading the correct chunked encoding:
resp, err := http.Get("http://localhost:8080/")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Read the response body in chunks
buf := make([]byte, 1024)
for {
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
log.Fatal(err)
}
if n == 0 {
break
}
fmt.Print(string(buf[:n]))
}
Additional consideration: our HTTP server supports compression, so need to figure out how that affects the response protocol.