Skip to content

Commit

Permalink
fixup! chore: close http responses in a way to allow the Transport to…
Browse files Browse the repository at this point in the history
… re-use the TCP connection
  • Loading branch information
atzoum committed Nov 23, 2022
1 parent 70b8b9b commit f116d49
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
68 changes: 35 additions & 33 deletions cmd/devtool/commands/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,43 @@ func EventSend(c *cli.Context) error {
}

for i := 0; i < c.Int("count"); i++ {
anonymousId := uuid.New().String()

buf := bytes.NewBuffer(nil)
err = t.Execute(buf, map[string]string{
"AnonymousId": anonymousId,
"Timestamp": time.Now().Format(time.RFC3339),
})
if err != nil {
return err
}

req, err := http.NewRequestWithContext(c.Context, "POST", url, buf)
if err != nil {
if err := func() error {
anonymousId := uuid.New().String()
buf := bytes.NewBuffer(nil)
err = t.Execute(buf, map[string]string{
"AnonymousId": anonymousId,
"Timestamp": time.Now().Format(time.RFC3339),
})
if err != nil {
return err
}

req, err := http.NewRequestWithContext(c.Context, "POST", url, buf)
if err != nil {
return err
}

req.SetBasicAuth(c.String("write-key"), "")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("AnonymousId", anonymousId)

resp, err := client.Do(req)
if err != nil {
return err
}
defer func() { httputil.CloseResponse(resp) }()
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("%s\n%s\n", resp.Status, b)
return fmt.Errorf("status code: %d", resp.StatusCode)
}
return nil
}(); err != nil {
return err
}

req.SetBasicAuth(c.String("write-key"), "")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("AnonymousId", anonymousId)

resp, err := client.Do(req)
if err != nil {
return err
}
defer func() { httputil.CloseResponse(resp) }()
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
fmt.Printf("%s\n%s\n", resp.Status, b)

return fmt.Errorf("status code: %d", resp.StatusCode)
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion testhelper/health/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func WaitUntilReady(
if err != nil {
continue
}
defer func() { httputil.CloseResponse(resp) }()
func() { httputil.CloseResponse(resp) }()
if resp.StatusCode == http.StatusOK {
t.Log("Application ready")
return
Expand Down
4 changes: 2 additions & 2 deletions warehouse/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func GetRequestWithTimeout(ctx context.Context, url string, timeout time.Duratio
var respBody []byte
if resp != nil && resp.Body != nil {
respBody, _ = io.ReadAll(resp.Body)
defer func() { httputil.CloseResponse(resp) }()
func() { httputil.CloseResponse(resp) }()
}

return respBody, nil
Expand All @@ -1050,7 +1050,7 @@ func PostRequestWithTimeout(ctx context.Context, url string, payload []byte, tim
var respBody []byte
if resp != nil && resp.Body != nil {
respBody, _ = io.ReadAll(resp.Body)
defer func() { httputil.CloseResponse(resp) }()
func() { httputil.CloseResponse(resp) }()
}

return respBody, nil
Expand Down

0 comments on commit f116d49

Please sign in to comment.