Created
August 25, 2017 09:09
-
-
Save davidklassen/243d305e090be4c760d2884cbffaa2c0 to your computer and use it in GitHub Desktop.
closenotifier.go
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 ( | |
"net/http" | |
"log" | |
"time" | |
) | |
func main() { | |
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { | |
finished := make(chan struct{}) | |
defer close(finished) | |
disconnected := w.(http.CloseNotifier).CloseNotify() | |
go func () { | |
select { | |
case <-finished: | |
log.Println("finished 1") | |
return | |
case <-disconnected: | |
log.Println("disconnected 1") | |
return | |
} | |
}() | |
go func () { | |
select { | |
case <-finished: | |
log.Println("finished 2") | |
return | |
case <-disconnected: | |
log.Println("disconnected 2") | |
return | |
} | |
}() | |
time.Sleep(3*time.Second) | |
w.Write([]byte("OK")) | |
}) | |
http.ListenAndServe(":9876", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment