Skip to content

Commit

Permalink
Release v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamil-Najafov authored Sep 6, 2023
2 parents d551c60 + 7d0a434 commit 85633c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @sezaakgun
* @sezaakgun @rafet
10 changes: 10 additions & 0 deletions insrequester/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package insrequester

import "errors"

var (
ErrCircuitBreakerOpen = errors.New("circuit breaker is open")
ErrTimeout = errors.New("timeout")

ErrRetryable = errors.New("retryable error")
)
33 changes: 14 additions & 19 deletions insrequester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
"github.com/slok/goresilience"
"github.com/slok/goresilience/circuitbreaker"
goresilienceErrors "github.com/slok/goresilience/errors"
"github.com/slok/goresilience/retry"
"github.com/slok/goresilience/timeout"
"github.com/useinsider/go-pkg/inscodeerr"
"net/http"
"time"
)
Expand Down Expand Up @@ -88,9 +86,11 @@ func (r *Request) sendRequest(httpMethod string, re RequestEntity) (*http.Respon
}

runnerErr := r.runner.Run(context.TODO(), func(ctx context.Context) error {
req, err := http.NewRequest(httpMethod, re.Endpoint, bytes.NewReader(re.Body))
if err != nil {
res, outerErr = nil, err
var req *http.Request

req, outerErr = http.NewRequest(httpMethod, re.Endpoint, bytes.NewReader(re.Body))
if outerErr != nil {
res = nil
return nil
}

Expand All @@ -102,26 +102,21 @@ func (r *Request) sendRequest(httpMethod string, re RequestEntity) (*http.Respon
return nil
}

if res.StatusCode >= http.StatusInternalServerError {
outerErr = err
return inscodeerr.CodeErr{
Code: res.StatusCode,
Message: fmt.Sprintf("status code: %v", res.StatusCode),
}
if res.StatusCode >= 100 && res.StatusCode < 200 ||
res.StatusCode == 429 ||
res.StatusCode >= 500 && res.StatusCode <= 599 {
return ErrRetryable
}

if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices {
outerErr = inscodeerr.CodeErr{
Code: res.StatusCode,
Message: fmt.Sprintf("status code: %v", res.StatusCode),
}
return nil
}
return nil
})

if runnerErr == goresilienceErrors.ErrCircuitOpen {
return nil, errors.Wrap(runnerErr, fmt.Sprintf("%v", outerErr))
return nil, ErrCircuitBreakerOpen
}

if runnerErr == goresilienceErrors.ErrTimeout {
return nil, ErrTimeout
}

if outerErr != nil {
Expand Down

0 comments on commit 85633c8

Please sign in to comment.