-
Notifications
You must be signed in to change notification settings - Fork 479
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
continuum-lokesh-jain
wants to merge
6
commits into
afex:master
Choose a base branch
from
ContinuumLLC:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
221ad18
Added Callback in circuit breaker
continuum-lokesh-jain 4f7f0a2
Updated Import statement
continuum-lokesh-jain d82962f
Updated readme file
continuum-lokesh-jain 575c187
add go.mod
84160d4
Merge pull request #1 from ContinuumLLC/gomod
guspanc 7b4e784
point to 1.0.1 fork version
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/afex/hystrix-go | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/DataDog/datadog-go v4.0.1+incompatible | ||
github.com/cactus/go-statsd-client/statsd v0.0.0-20200728222731-a2baea3bbfc6 | ||
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 | ||
github.com/smartystreets/goconvey v1.6.4 | ||
github.com/stretchr/testify v1.6.1 // indirect | ||
) | ||
|
||
replace github.com/afex/hystrix-go => github.com/ContinuumLLC/hystrix-go v1.0.1 |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
github.com/DataDog/datadog-go v4.0.1+incompatible h1:6BF/mPNV2f+PJzpw5vxDK91Z3IfOM7fEmz5ptuL+0vo= | ||
github.com/DataDog/datadog-go v4.0.1+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= | ||
github.com/cactus/go-statsd-client/statsd v0.0.0-20200728222731-a2baea3bbfc6 h1:arL5CcymFtWyyi5Xk44VpSxUfFfJqxCMbhZQ0lxLkDE= | ||
github.com/cactus/go-statsd-client/statsd v0.0.0-20200728222731-a2baea3bbfc6/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= | ||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= | ||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= | ||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ= | ||
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= | ||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= | ||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= | ||
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= | ||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= | ||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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 |
---|---|---|
@@ -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 | ||
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) | ||
} | ||
} |
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 |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes