Skip to content

Commit

Permalink
Update README.md for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiyukiMineo committed Apr 30, 2024
1 parent d5b9529 commit 682127b
Showing 1 changed file with 4 additions and 62 deletions.
66 changes: 4 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Installation
------------

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

Usage
Expand All @@ -19,7 +19,7 @@ The struct `CircuitBreaker` is a state machine to prevent sending requests that
The function `NewCircuitBreaker` creates a new `CircuitBreaker`.

```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 +81,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 @@ -90,67 +90,9 @@ Otherwise, `Execute` returns the result of the request.
If a panic occurs in the request, `CircuitBreaker` handles it as an error
and causes the same panic again.


V2 Implementation
---

The [v2 implementation](./v2) provides the same CircuitBreaker logic, but with support for generics in Go.

This change allows for CircuitBreaker instances to specify the handled type directly, and skips type-casting an `any`
or `interface{}` type into the desired target one.

This change mostly focuses on the [CircuitBreaker's Execute](./v2/gobreaker.go#L228) method, which accepts an
executable function of a given type:

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


Example
-------

> *v1 Example*

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

"github.com/sony/gobreaker"
)

var cb *breaker.CircuitBreaker

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

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

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

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


> *v2 Example*

```go
import (
"fmt"
Expand Down Expand Up @@ -186,7 +128,7 @@ func Get(url string) ([]byte, error) {
}
```

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

0 comments on commit 682127b

Please sign in to comment.