Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
/go.work*

*.bench
*.profile

.idea/
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@ Simple low-overhead circuit breaker library.
## Usage

```go
// some arbitrary function
foo := func(ctx context.Context, bar int) (Foo, error) {
if bar == 42 {
return Foo{Bar: bar}, nil
}
return Foo{}, fmt.Errorf("bar is not 42")
}

h, err := hoglet.NewCircuit(
func(ctx context.Context, bar int) (Foo, error) {
if bar == 42 {
return Foo{Bar: bar}, nil
}
return Foo{}, fmt.Errorf("bar is not 42")
},
hoglet.NewSlidingWindowBreaker(5*time.Second, 0.1),
hoglet.WithFailureCondition(hoglet.IgnoreContextCanceled),
)
/* if err != nil ... */

f, _ := h.Call(context.Background(), 42)
f, _ := hoglet.Wrap(h, foo)(context.Background(), 42)
fmt.Println(f.Bar) // 42

_, err = h.Call(context.Background(), 0)
_, err = hoglet.Wrap(h, foo)(context.Background(), 0)
fmt.Println(err) // bar is not 42

_, err = h.Call(context.Background(), 42)
_, err = hoglet.Wrap(h, foo)(context.Background(), 42)
fmt.Println(err) // hoglet: breaker is open

time.Sleep(5 * time.Second)

f, _ = h.Call(context.Background(), 42)
f, _ = hoglet.Wrap(h, foo)(context.Background(), 42)
fmt.Println(f.Bar) // 42
```

Expand All @@ -51,4 +53,4 @@ non-racy behavior around the failed function.
## Design

Hoglet prefers throughput to correctness (e.g. by avoiding locks), which means it cannot guarantee an exact number of
calls will go through.
calls will go through.
37 changes: 18 additions & 19 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,28 @@ func foo(ctx context.Context, bar int) (Foo, error) {

func ExampleEWMABreaker() {
h, err := hoglet.NewCircuit(
foo,
hoglet.NewEWMABreaker(10, 0.1),
hoglet.WithHalfOpenDelay(time.Second),
)
if err != nil {
log.Fatal(err)
}

f, err := h.Call(context.Background(), 1)
f, err := hoglet.Wrap(h, foo)(context.Background(), 1)
if err != nil {
log.Fatal(err)
}
fmt.Println(f.Bar)

_, err = h.Call(context.Background(), 100)
_, err = hoglet.Wrap(h, foo)(context.Background(), 100)
fmt.Println(err)

_, err = h.Call(context.Background(), 2)
_, err = hoglet.Wrap(h, foo)(context.Background(), 2)
fmt.Println(err)

time.Sleep(time.Second) // wait for half-open delay

f, err = h.Call(context.Background(), 3)
f, err = hoglet.Wrap(h, foo)(context.Background(), 3)
if err != nil {
log.Fatal(err)
}
Expand All @@ -59,28 +58,27 @@ func ExampleEWMABreaker() {

func ExampleSlidingWindowBreaker() {
h, err := hoglet.NewCircuit(
foo,
hoglet.NewSlidingWindowBreaker(time.Second, 0.1),
)
if err != nil {
log.Fatal(err)
}

f, err := h.Call(context.Background(), 1)
f, err := hoglet.Wrap(h, foo)(context.Background(), 1)
if err != nil {
log.Fatal(err)
}
fmt.Println(f.Bar)

_, err = h.Call(context.Background(), 100)
_, err = hoglet.Wrap(h, foo)(context.Background(), 100)
fmt.Println(err)

_, err = h.Call(context.Background(), 2)
_, err = hoglet.Wrap(h, foo)(context.Background(), 2)
fmt.Println(err)

time.Sleep(time.Second) // wait for sliding window

f, err = h.Call(context.Background(), 3)
f, err = hoglet.Wrap(h, foo)(context.Background(), 3)
if err != nil {
log.Fatal(err)
}
Expand All @@ -94,14 +92,15 @@ func ExampleSlidingWindowBreaker() {
}

func ExampleConcurrencyLimiter() {
foo := func(ctx context.Context, _ any) (any, error) {
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
return nil, nil
}

h, err := hoglet.NewCircuit(
func(ctx context.Context, _ any) (any, error) {
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
return nil, nil
},
hoglet.NewSlidingWindowBreaker(10, 0.1),
hoglet.WithBreakerMiddleware(hoglet.ConcurrencyLimiter(1, false)),
)
Expand All @@ -116,15 +115,15 @@ func ExampleConcurrencyLimiter() {

go func() {
// use up the concurrency limit
_, _ = h.Call(ctx, 42)
_, _ = hoglet.Wrap(h, foo)(ctx, 42)
}()

// ensure call above actually started
time.Sleep(time.Millisecond * 100)

go func() {
defer close(errCh)
_, err := h.Call(ctx, 42)
_, err := hoglet.Wrap(h, foo)(ctx, 42)
if err != nil {
errCh <- err
}
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
module github.com/exaring/hoglet

go 1.21
go 1.24.0

require (
github.com/stretchr/testify v1.8.2
golang.org/x/sync v0.5.0
golang.org/x/sync v0.18.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
Loading