Skip to content

Commit

Permalink
docs: add warning about concurrent safety in http example (#189)
Browse files Browse the repository at this point in the history
fixes keploy/keploy#542

Signed-off-by: Shubham Jain <shubhamkjain@outlook.com>
  • Loading branch information
slayerjain authored Apr 25, 2023
1 parent 32be257 commit d505b48
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,20 +715,6 @@ func main(){
Transport: interceptor,
}

r.HandleFunc("/mux/httpGet",func (w http.ResponseWriter, r *http.Request) {
// SetContext should always be called once in a http handler before http.Client's Get or Post or Head or PostForm method.
// Passing requests context as parameter.
interceptor.SetContext(r.Context())
// make Get, Post, etc request to external http service
resp, err := client.Get("https://example.com/getDocs")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
fmt.Println("BODY : ", body)
})

r.HandleFunc("/mux/httpDo", func(w http.ResponseWriter, r *http.Request){
putBody, _ := json.Marshal(map[string]interface{}{
"name": "Ash",
Expand All @@ -754,6 +740,22 @@ func main(){
fmt.Println(" response Body: ", string(body))

})

r.HandleFunc("/mux/httpGet",func (w http.ResponseWriter, r *http.Request) {
// ONLY USE THIS IF YOU CANT ADD CONTEXT TO YOUR REQUEST OBJECT AS ABOVE.
// THIS IS NOT CONCURRENT SAFE BECAUSE WE ARE SETTING GLOBAL CONTEXT.
// SetContext should always be called once in a http handler before http.Client's Get or Post or Head or PostForm method.
// Passing requests context as parameter.
interceptor.SetContext(r.Context())
// make Get, Post, etc request to external http service
resp, err := client.Get("https://example.com/getDocs")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
fmt.Println("BODY : ", body)
})

// gcp compute API integeration
client, err := google.DefaultClient(context.TODO(), compute.ComputeScope)
Expand Down

0 comments on commit d505b48

Please sign in to comment.