From 501dda2b35488f116ee8cd9dad0b8cff5d37b8f2 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Mon, 28 Nov 2022 13:54:00 +0200 Subject: [PATCH 1/3] fix tests --- request_test.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/request_test.go b/request_test.go index 64f5f81..b06c993 100644 --- a/request_test.go +++ b/request_test.go @@ -1,6 +1,7 @@ package common import ( + "context" "fmt" "net/http" "time" @@ -18,7 +19,9 @@ func ExampleMakeRequest() { } out := Out{} client := &http.Client{} + ctx := context.Background() body, err := MakeRequest( + ctx, HTTPRequest{ URL: "https://ingress-api.csf.elisa.fi/healthz", Method: "GET", @@ -29,10 +32,26 @@ func ExampleMakeRequest() { backoff, ) - fmt.Printf("%s\n%s\n%d\n%v", out.Message, body.Body, body.StatusCode, err) + fmt.Printf("%s\n%s\n%d\n%v\n", out.Message, body.Body, body.StatusCode, err) + ctx, cancel := context.WithTimeout(ctx, 1*time.Millisecond) + defer cancel() + _, err = MakeRequest( + ctx, + HTTPRequest{ + URL: "https://ingress-api.csf.elisa.fi/healthz", + Method: "GET", + OKCode: []int{200}, + }, + &out, + client, + backoff, + ) + + fmt.Printf("%v", err) // Output: pong // {"message":"pong","error":""} // 200 // + // retrying timed out: Get "https://ingress-api.csf.elisa.fi/healthz": context deadline exceeded } From f8ed65589924463cd0d2890359d216d70e8e4266 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Mon, 28 Nov 2022 13:59:35 +0200 Subject: [PATCH 2/3] if context is closed, exit without retry --- request.go | 4 ++++ request_test.go | 2 +- time.go | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index a4b2f33..b140b6a 100644 --- a/request.go +++ b/request.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -79,6 +80,9 @@ func MakeRequest(ctx context.Context, request HTTPRequest, output interface{}, c Str("url", request.URL). Str("error", err.Error()). Msg("do request error") + if errors.Is(err, context.DeadlineExceeded) { + return true, err + } return false, err } defer resp.Body.Close() diff --git a/request_test.go b/request_test.go index b06c993..8773cb5 100644 --- a/request_test.go +++ b/request_test.go @@ -53,5 +53,5 @@ func ExampleMakeRequest() { // {"message":"pong","error":""} // 200 // - // retrying timed out: Get "https://ingress-api.csf.elisa.fi/healthz": context deadline exceeded + // Get "https://ingress-api.csf.elisa.fi/healthz": context deadline exceeded } diff --git a/time.go b/time.go index e55fb9f..7e5d08d 100644 --- a/time.go +++ b/time.go @@ -26,7 +26,7 @@ func SleepUntil(backoff Backoff, condition ConditionFunc) error { } if err != nil { - return errors.Wrap(err, "retrying timed out") + return err } return errors.New("Timed out waiting for the condition") } From 3673b82bd48652da9aac8c8b3bd1b27cc8f630e0 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Mon, 28 Nov 2022 14:01:23 +0200 Subject: [PATCH 3/3] lint --- request.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/request.go b/request.go index b140b6a..26dbf76 100644 --- a/request.go +++ b/request.go @@ -48,7 +48,13 @@ type Backoff struct { } // MakeRequest ... -func MakeRequest(ctx context.Context, request HTTPRequest, output interface{}, client *http.Client, backoff Backoff) (*HTTPResponse, error) { +func MakeRequest( + ctx context.Context, + request HTTPRequest, + output interface{}, + client *http.Client, + backoff Backoff, +) (*HTTPResponse, error) { httpresp := &HTTPResponse{} err := SleepUntil(backoff, func() (bool, error) { httpreq, err := http.NewRequest(request.Method, request.URL, nil)