Created
July 12, 2020 09:56
-
-
Save monmohan/d59151de1826adcf319d1ac827788c5d to your computer and use it in GitHub Desktop.
Context propagation from client to server
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
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