Skip to content

Instantly share code, notes, and snippets.

@monmohan
Created July 12, 2020 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monmohan/d59151de1826adcf319d1ac827788c5d to your computer and use it in GitHub Desktop.
Save monmohan/d59151de1826adcf319d1ac827788c5d to your computer and use it in GitHub Desktop.
Context propagation from client to server
unc runTestServer() string {
slowServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
incoming, _ := httputil.DumpRequest(r, false)
fmt.Printf("Server: Incoming Request %s", string(incoming))
ctx := r.Context()
echan := make(chan error)
go func() {
time.Sleep(12 * time.Second) // Do difficult Job
_, err := w.Write([]byte("Hello There!"))
echan <- err
}()
select {
case <-ctx.Done():
fmt.Printf("Error %s", ctx.Err().Error())
case err := <-echan:
if err != nil {
w.WriteHeader(500)
}
}
}))
return slowServer.URL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment