-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
21 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,22 @@ | ||
package circuitbreaker_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-kratos/aegis/circuitbreaker" | ||
"github.com/go-kratos/aegis/circuitbreaker/sre" | ||
) | ||
|
||
// This is a example of using a circuit breaker Do() when return nil. | ||
func Example() { | ||
g := &circuitbreaker.Group{New: func() circuitbreaker.CircuitBreaker { | ||
return sre.NewBreaker() | ||
}} | ||
err := g.Do("do", func() error { | ||
// dosomething | ||
return nil | ||
}) | ||
|
||
b := sre.NewBreaker() | ||
for i := 0; i < 1000; i++ { | ||
b.MarkSuccess() | ||
} | ||
for i := 0; i < 100; i++ { | ||
b.MarkFailed() | ||
} | ||
|
||
err := b.Allow() | ||
fmt.Printf("err=%v", err) | ||
// Output: err=<nil> | ||
} | ||
|
||
// This is a example of using a circuit breaker fn failed then call fallback. | ||
func Example_fallback() { | ||
g := &circuitbreaker.Group{New: func() circuitbreaker.CircuitBreaker { | ||
return sre.NewBreaker() | ||
}} | ||
err := g.Do("do", func() error { | ||
// dosomething | ||
return errors.New("fallback") | ||
}) | ||
|
||
fmt.Printf("err=%v", err) | ||
// Output: err=fallback | ||
} | ||
|
||
// This is a example of using a circuit breaker fn failed but ignore error mark | ||
// as success. | ||
func Example_ignore() { | ||
g := &circuitbreaker.Group{New: func() circuitbreaker.CircuitBreaker { | ||
return sre.NewBreaker() | ||
}} | ||
err := g.Do("do", func() error { | ||
// dosomething | ||
return circuitbreaker.Ignore(errors.New("ignore")) | ||
}) | ||
|
||
fmt.Printf("err=%v", err) | ||
// Output: err=ignore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters