Skip to content

Commit

Permalink
V2 (#61)
Browse files Browse the repository at this point in the history
* [Go 1.21] Replace 'any' / 'interface{}' implementation with Generics (#54)

* gobreaker/v2: added generics implementation in v2 module

* gobreaker/v2/example: added v2 example

* gobreaker/README: updated README.md

* fixup! gobreaker/v2: added generics implementation in v2 module

---------

Co-authored-by: Yoshiyuki Mineo <[email protected]>

* Do refactoring

* Update README.md for v2

* Update README.md for v2

* Update README.md for v2

---------

Co-authored-by: Mario Salgado <[email protected]>
  • Loading branch information
YoshiyukiMineo and zalgonoise authored May 3, 2024
1 parent 27b8e2c commit 8c718af
Show file tree
Hide file tree
Showing 6 changed files with 858 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ Installation
------------

```
go get github.com/sony/gobreaker
go get github.com/sony/gobreaker/v2
```

Usage
-----

The struct `CircuitBreaker` is a state machine to prevent sending requests that are likely to fail.
The function `NewCircuitBreaker` creates a new `CircuitBreaker`.
The type parameter `T` specifies the return type of requests.

```go
func NewCircuitBreaker(st Settings) *CircuitBreaker
func NewCircuitBreaker[T any](st Settings) *CircuitBreaker[T]
```

You can configure `CircuitBreaker` by the struct `Settings`:
Expand Down Expand Up @@ -81,7 +82,7 @@ on the change of the state or at the closed-state intervals.
`CircuitBreaker` can wrap any function to send a request:

```go
func (cb *CircuitBreaker) Execute(req func() (interface{}, error)) (interface{}, error)
func (cb *CircuitBreaker[T]) Execute(req func() (T, error)) (T, error)
```

The method `Execute` runs the given request if `CircuitBreaker` accepts it.
Expand All @@ -94,17 +95,17 @@ Example
-------

```go
var cb *breaker.CircuitBreaker
var cb *gobreaker.CircuitBreaker[[]byte]

func Get(url string) ([]byte, error) {
body, err := cb.Execute(func() (interface{}, error) {
body, err := cb.Execute(func() ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand All @@ -115,11 +116,11 @@ func Get(url string) ([]byte, error) {
return nil, err
}

return body.([]byte), nil
return body, nil
}
```

See [example](https://github.com/sony/gobreaker/blob/master/example) for details.
See [example](https://github.com/sony/gobreaker/blob/master/v2/example) for details.

License
-------
Expand Down
55 changes: 55 additions & 0 deletions v2/example/http_breaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"io"
"log"
"net/http"

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

var cb *gobreaker.CircuitBreaker[[]byte]

func init() {
var st gobreaker.Settings
st.Name = "HTTP GET"
st.ReadyToTrip = func(counts gobreaker.Counts) bool {
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
return counts.Requests >= 3 && failureRatio >= 0.6
}

cb = gobreaker.NewCircuitBreaker[[]byte](st)
}

// Get wraps http.Get in CircuitBreaker.
func Get(url string) ([]byte, error) {
body, err := cb.Execute(func() ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return body, nil
})
if err != nil {
return nil, err
}

return body, nil
}

func main() {
body, err := Get("http://www.google.com/robots.txt")
if err != nil {
log.Fatal(err)
}

fmt.Println(string(body))
}
11 changes: 11 additions & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/sony/gobreaker/v2

go 1.21

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 8c718af

Please sign in to comment.