Skip to content

Commit 738341b

Browse files
authored
fix context cancel error (#260)
* Update api-v2.go * fix context flaky problem * more fix * remove log * Update api-v2.go
1 parent 01f7a64 commit 738341b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

api/v2/api-v2.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ var (
7878
)
7979

8080
func post(ctx context.Context, url string, encodedData []byte) (*http.Response, error) {
81-
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
82-
defer cancel()
83-
8481
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(encodedData))
8582
if err != nil {
8683
log.Println(err)
@@ -119,6 +116,7 @@ func postWithRetry(ctx context.Context, url string, encodedData []byte) (*http.R
119116
return resp, err
120117
}
121118
if resp.StatusCode != http.StatusServiceUnavailable {
119+
log.Println("Statuscode: ", resp.StatusCode)
122120
RequestTimeHistogram.WithLabelValues(resp.Status).Observe(float64(time.Since(start).Nanoseconds()) / 1e6)
123121
return resp, ErrStatusNotOK
124122
}
@@ -156,7 +154,10 @@ func GetAnnotations(ctx context.Context, url string, date time.Time, ips []strin
156154
return nil, err
157155
}
158156

159-
httpResp, err := postWithRetry(ctx, url, encodedData)
157+
localCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
158+
defer cancel()
159+
160+
httpResp, err := postWithRetry(localCtx, url, encodedData)
160161
if err != nil {
161162
log.Println(err)
162163
if httpResp == nil || httpResp.Body == nil {

0 commit comments

Comments
 (0)