Skip to content

Commit

Permalink
Merge pull request #78 from cep21/pr
Browse files Browse the repository at this point in the history
Error: add Error type.
  • Loading branch information
cep21 authored Aug 28, 2019
2 parents fb2df49 + 4d3f844 commit 7687802
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions v3/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func (c *Circuit) Run(ctx context.Context, runFunc func(context.Context) error)
}

// Execute the circuit. Prefer this over Go. Similar to http://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/HystrixCommand.html#execute--
// The returned error will either be the result of runFunc, the result of fallbackFunc, or an internal library error.
// Internal library errors will match the interface Error and you can use type casting to check this.
func (c *Circuit) Execute(ctx context.Context, runFunc func(context.Context) error, fallbackFunc func(context.Context, error) error) error {
if c.isEmptyOrNil() || c.threadSafeConfig.CircuitBreaker.Disabled.Get() {
return runFunc(ctx)
Expand Down
10 changes: 10 additions & 0 deletions v3/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ type circuitError struct {
circuitOpen bool
msg string
}
var _ Error = &circuitError{}

// Error is the type of error returned by internal errors using the circuit library.
type Error interface {
error
// ConcurrencyLimitReached returns true if this error is because the concurrency limit has been reached.
ConcurrencyLimitReached() bool
// CircuitOpen returns true if this error is because the circuit is open.
CircuitOpen() bool
}

func (m *circuitError) Error() string {
return fmt.Sprintf("%s: concurrencyReached=%t circuitOpen=%t", m.msg, m.ConcurrencyLimitReached(), m.CircuitOpen())
Expand Down
8 changes: 8 additions & 0 deletions v3/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,11 @@ func ExampleConfig_custommetrics() {
circuit.NewCircuitFromConfig("custom-metrics", config)
// Output:
}

// Shows how to check if an error is part of the circuit library.
func ExampleError_checking() {
x := errors.New("an error")
if _, ok := x.(circuit.Error); ok {
// this error is a circuit library error, not the result of runFunc or fallbackFunc
}
}
2 changes: 1 addition & 1 deletion v3/metrics/statsdmetrics/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ type ConcurrencyCollector struct {
Delay faststats.AtomicInt64
onClose chan struct{}
Manager *circuit.Manager
SampleRate float32
timeAfter func(time.Duration) <-chan time.Time
once sync.Once
SampleRate float32
}

func (c *ConcurrencyCollector) delay() time.Duration {
Expand Down
5 changes: 3 additions & 2 deletions v3/metrics/statsdmetrics/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func Test_sanitizeStatsd(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got := sanitizeStatsd(tt.args.s); got != tt.want {
t.Errorf("sanitizeStatsd() = %v, want %v", got, tt.want)
Expand All @@ -162,7 +163,7 @@ func TestConcurrencyCollector_delay(t *testing.T) {
require.Equal(t, x.delay(), time.Second*3)
}

func waitForGauge(name string, ss *rememberStats, clk *clock.MockClock) {
func waitForGauge(name string, ss *rememberStats, _ *clock.MockClock) {
hasGauge := false
for !hasGauge {
time.Sleep(time.Millisecond)
Expand All @@ -174,7 +175,7 @@ func waitForGauge(name string, ss *rememberStats, clk *clock.MockClock) {
}
}

func waitForCounter(name string, ss *rememberStats, clk *clock.MockClock) {
func waitForCounter(name string, ss *rememberStats, _ *clock.MockClock) {
hasCounter := false
for !hasCounter {
time.Sleep(time.Millisecond)
Expand Down

0 comments on commit 7687802

Please sign in to comment.