Skip to content

Commit

Permalink
fix context cancel error (#260)
Browse files Browse the repository at this point in the history
* Update api-v2.go

* fix context flaky problem

* more fix

* remove log

* Update api-v2.go
  • Loading branch information
yachang authored Jun 6, 2019
1 parent 01f7a64 commit 738341b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions api/v2/api-v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ var (
)

func post(ctx context.Context, url string, encodedData []byte) (*http.Response, error) {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

httpReq, err := http.NewRequest("POST", url, bytes.NewReader(encodedData))
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -119,6 +116,7 @@ func postWithRetry(ctx context.Context, url string, encodedData []byte) (*http.R
return resp, err
}
if resp.StatusCode != http.StatusServiceUnavailable {
log.Println("Statuscode: ", resp.StatusCode)
RequestTimeHistogram.WithLabelValues(resp.Status).Observe(float64(time.Since(start).Nanoseconds()) / 1e6)
return resp, ErrStatusNotOK
}
Expand Down Expand Up @@ -156,7 +154,10 @@ func GetAnnotations(ctx context.Context, url string, date time.Time, ips []strin
return nil, err
}

httpResp, err := postWithRetry(ctx, url, encodedData)
localCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

httpResp, err := postWithRetry(localCtx, url, encodedData)
if err != nil {
log.Println(err)
if httpResp == nil || httpResp.Body == nil {
Expand Down

0 comments on commit 738341b

Please sign in to comment.