Created
March 30, 2021 10:45
-
-
Save costela/bae6460b4c802f11387bcb10a77e8b07 to your computer and use it in GitHub Desktop.
counter-example for the duscission in golang/go#16100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log" | |
"net/http" | |
"sync/atomic" | |
"time" | |
) | |
var counter int64 | |
func handler(w http.ResponseWriter, r *http.Request) { | |
log.Print("Request received.") | |
defer log.Print("Request done.") | |
ctx := r.Context() // If we generate error consider context.WithCancel | |
publisherChan := streamPublisher(ctx) | |
resultChan := make(chan []byte) | |
go writeResult(resultChan, w) | |
for value := range publisherChan { | |
select { | |
case resultChan <- value: | |
case <-r.Context().Done(): // Client has closed the socket. | |
log.Print("Request is Done.") | |
close(resultChan) // Close the result channel to exit the writer. | |
return | |
case <-time.After(1 * time.Second): // The socket is not writeable in given timeout, quit | |
log.Print("Output channel is not writable for 1 second. Close and exit.") | |
close(resultChan) // Close the result channel to exit the writer. | |
return | |
} | |
} | |
} | |
func writeResult(resultChan chan []byte, w http.ResponseWriter) { | |
atomic.AddInt64(&counter, 1) | |
defer atomic.AddInt64(&counter, -1) | |
for r := range resultChan { | |
log.Printf("writing %s", r) | |
w.Write(r) | |
} | |
log.Print("Write Result is done.") | |
} | |
func streamPublisher(ctx context.Context) chan []byte { | |
dst := make(chan []byte) | |
randomContent := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.") | |
go func() { // publisher of sequential numbers | |
defer log.Print("Exit publisher routine.") | |
for { | |
select { | |
case <-ctx.Done(): // the request is closed. | |
log.Print("publisher detected ctx.Done.") | |
close(dst) | |
return | |
case dst <- randomContent: | |
// log.Print("writing") | |
} | |
} | |
}() | |
return dst | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", handler) | |
s := &http.Server{ | |
Addr: ":8080", | |
Handler: mux, | |
ReadTimeout: 10 * time.Second, | |
// WriteTimeout: 10 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
go func() { | |
for ; ; <-time.Tick(2 * time.Second) { | |
log.Printf("#writeResult = %d", counter) | |
} | |
}() | |
log.Fatal(s.ListenAndServe()) | |
} |
I does not work on Windows.
So it is simply luck I tested on MacOS first.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok on Linux neither variant works. I guess it will be stuck on the slow consumer until one chunk of data is consumed and the buffers have room for one more.
It is OS dependent behavior.
PS Confirmed. Linux waits until some data gets confirmed and the next write cycles comes around. This is pretty bad.
PSS out of curiosity I will check Windows. I guess this is the underlying implementation of the IO which evidently is not good on Linux. It will be interesting to see http/2 where application layer may handle this