context: setup timeout context
This commit is contained in:
@ -7,10 +7,24 @@ import (
|
||||
|
||||
type Store interface {
|
||||
Fetch() string
|
||||
Cancel()
|
||||
}
|
||||
|
||||
func Server(store Store) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, store.Fetch())
|
||||
ctx := r.Context()
|
||||
|
||||
data := make(chan string, 1)
|
||||
|
||||
go func() {
|
||||
data <- store.Fetch()
|
||||
}()
|
||||
|
||||
select {
|
||||
case d := <-data:
|
||||
fmt.Fprint(w, d)
|
||||
case <-ctx.Done():
|
||||
store.Cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user