Skip to content

Commit

Permalink
feat: add ErrCBOpenState and ErrCBTooManyRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbolgarin committed Dec 9, 2024
1 parent 84d8841 commit d38fd12
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cliex.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,18 @@ func (c *HTTP) Request(ctx context.Context, url string, opts RequestOpts) (*rest
cb = gobreaker.NewCircuitBreaker[*resty.Response](c.cbCfg)
c.cbs.Set(url, cb)
}
return cb.Execute(func() (*resty.Response, error) {
resp, err := cb.Execute(func() (*resty.Response, error) {
return c.request(ctx, url, opts)
})
switch {
case errors.Is(err, gobreaker.ErrOpenState):
return nil, ErrCBOpenState
case errors.Is(err, gobreaker.ErrTooManyRequests):
return nil, ErrCBTooManyRequests
case err != nil:
return nil, err
}
return resp, nil
}

func (c *HTTP) request(ctx context.Context, url string, opts RequestOpts) (*resty.Response, error) {
Expand Down
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"net/http"
"time"

"github.com/sony/gobreaker/v2"
)

// ServerErrorResponse is the error response from server (try to guess what it is)
Expand Down Expand Up @@ -89,6 +91,13 @@ type RequestOpts struct {
EnableTrace bool
}

var (
// ErrCBOpenState is returned when the CB state is open
ErrCBOpenState = gobreaker.ErrOpenState
// ErrTooManyRequests is returned when the CB state is half open and the requests count is over the cb maxRequests
ErrCBTooManyRequests = gobreaker.ErrTooManyRequests
)

var (
// ErrBadRequest is when the server cannot or will not process the request due to a client error
// (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing).
Expand Down

0 comments on commit d38fd12

Please sign in to comment.