Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State Change Callback in circuit breaker #101

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions hystrix/callback/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package callback

var circuitCallback map[string]stateFunc

//State is a type to hold Circuit-state this will be used while calling stateFunc on State change
type State string

const (
//Open is a state to indicate that Circuit state is Open
Open = "Open"
//Close is a state to indicate that Circuit state is Close
Close = "Close"
//AllowSingle is a state to indicate that Circuit state is AllowSingle or trying to Open Circuit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AllowSingle is for trying to 'close' the circuit?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

AllowSingle = "Allow Single"
)

type stateFunc func(name string, state State)

func init() {
circuitCallback = make(map[string]stateFunc)
}

//Register adds callback for a circuit
func Register(name string, callbackFunc stateFunc) {
circuitCallback[name] = callbackFunc
}

//Invoke is a function to invoke Callback function in a goroutine on State change
func Invoke(name string, state State) {
callbackFunc, _ := circuitCallback[name]
if callbackFunc != nil {
go callbackFunc(name, state)
}
}
57 changes: 57 additions & 0 deletions hystrix/callback/callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package callback

import (
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)

func TestRegister(t *testing.T) {
Convey("Register a command", t, func() {
Register("Test-Command", func(name string, state State) {})

Convey("Read Callback function for Registered Command", func() {
callbackFunc, _ := circuitCallback["Test-Command"]
So(callbackFunc, ShouldNotBeNil)
})
Convey("Read Callback function for unknown Command", func() {
callbackFunc, _ := circuitCallback["Command"]
So(callbackFunc, ShouldBeNil)
})
})
}

func TestInvoke(t *testing.T) {
Convey("Register a command", t, func() {
var callbackInvoked bool
var callbackState State = Close
Register("TestInvokeCommand", func(name string, state State) {
callbackInvoked = true
callbackState = state
})

Invoke("TestInvokeCommand", Open)
time.Sleep(2 * time.Second)
Convey("Invoke Callback for Registered Command", func() {
So(callbackInvoked, ShouldBeTrue)
So(callbackState, ShouldEqual, Open)
})
})

Convey("Register a Invoke command", t, func() {
var callbackInvoked = false
var callbackState State = Close
Register("TestInvokeCommand", func(name string, state State) {
callbackInvoked = true
callbackState = state
})

Invoke("Command", Open)

Convey("Read Callback function for unknown Command", func() {
So(callbackInvoked, ShouldBeFalse)
So(callbackState, ShouldEqual, Close)
})
})
}
11 changes: 10 additions & 1 deletion hystrix/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/ContinuumLLC/hystrix-go/hystrix/callback"
)

// CircuitBreaker is created for each ExecutorPool to track whether requests
Expand Down Expand Up @@ -128,6 +130,8 @@ func (circuit *CircuitBreaker) allowSingleTest() bool {
swapped := atomic.CompareAndSwapInt64(&circuit.openedOrLastTestedTime, openedOrLastTestedTime, now)
if swapped {
log.Printf("hystrix-go: allowing single test to possibly close circuit %v", circuit.Name)

callback.Invoke(circuit.Name, callback.AllowSingle)
}
return swapped
}
Expand All @@ -144,9 +148,11 @@ func (circuit *CircuitBreaker) setOpen() {
}

log.Printf("hystrix-go: opening circuit %v", circuit.Name)

circuit.openedOrLastTestedTime = time.Now().UnixNano()
circuit.open = true

callback.Invoke(circuit.Name, callback.Open)

}

func (circuit *CircuitBreaker) setClose() {
Expand All @@ -161,6 +167,9 @@ func (circuit *CircuitBreaker) setClose() {

circuit.open = false
circuit.metrics.Reset()

callback.Invoke(circuit.Name, callback.Close)

}

// ReportEvent records command metrics for tracking recent error rates and exposing data to the dashboard.
Expand Down
2 changes: 2 additions & 0 deletions hystrix/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
DefaultLogger = NoopLogger{}
)

//Settings is used to tune circuit settings
type Settings struct {
Timeout time.Duration
MaxConcurrentRequests int
Expand Down Expand Up @@ -106,6 +107,7 @@ func getSettings(name string) *Settings {
return s
}

//GetCircuitSettings returns Circuit Settings for each command
func GetCircuitSettings() map[string]*Settings {
copy := make(map[string]*Settings)

Expand Down