From 36da212ccb8ceed172d4c23df9127dbd03ef12c6 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Thu, 18 Jun 2020 15:10:42 +0800 Subject: [PATCH] Polish circuit breaking and "hot spot" param flow example Signed-off-by: Eric Zhao --- .../circuitbreaker/circuit_breaker_example.go | 25 ++++--- example/circuitbreaker/sentinel.yml | 4 ++ .../hot-pramas-sentinel.yml | 8 --- .../hotspot_param_flow_example.go} | 69 ++++++------------- example/hotspot_param_flow/sentinel.yml | 4 ++ 5 files changed, 44 insertions(+), 66 deletions(-) create mode 100644 example/circuitbreaker/sentinel.yml delete mode 100644 example/freq_params_traffic/hot-pramas-sentinel.yml rename example/{freq_params_traffic/hot_params_flow_example.go => hotspot_param_flow/hotspot_param_flow_example.go} (56%) create mode 100644 example/hotspot_param_flow/sentinel.yml diff --git a/example/circuitbreaker/circuit_breaker_example.go b/example/circuitbreaker/circuit_breaker_example.go index f01dce00e..99799ca1a 100644 --- a/example/circuitbreaker/circuit_breaker_example.go +++ b/example/circuitbreaker/circuit_breaker_example.go @@ -7,7 +7,7 @@ import ( "math/rand" "time" - "github.com/alibaba/sentinel-golang/api" + sentinel "github.com/alibaba/sentinel-golang/api" "github.com/alibaba/sentinel-golang/core/circuitbreaker" "github.com/alibaba/sentinel-golang/util" ) @@ -28,45 +28,50 @@ func (s *stateChangeTestListener) OnTransformToHalfOpen(prev circuitbreaker.Stat } func main() { - err := api.InitDefault() + err := sentinel.InitDefault() if err != nil { log.Fatal(err) } ch := make(chan struct{}) + // Register a state change listener so that we could observer the state change of the internal circuit breaker. circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{}) _, err = circuitbreaker.LoadRules([]circuitbreaker.Rule{ + // Statistic time span=10s, recoveryTimeout=3s, slowRtUpperBound=50ms, maxSlowRequestRatio=50% circuitbreaker.NewSlowRtRule("abc", 10000, 3000, 50, 10, 0.5), + // Statistic time span=10s, recoveryTimeout=3s, maxErrorRatio=50% circuitbreaker.NewErrorRatioRule("abc", 10000, 3000, 10, 0.5), }) if err != nil { log.Fatal(err) } + fmt.Println("Sentinel Go circuit breaking demo is running. You may see the pass/block metric in the metric log.") go func() { for { - e, b := api.Entry("abc") + e, b := sentinel.Entry("abc") if b != nil { - fmt.Println("g1blocked") + //fmt.Println("g1blocked") time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) } else { if rand.Uint64()%20 > 9 { - e.SetError(errors.New("biz error")) + // Record current invocation as error. + sentinel.TraceError(e, errors.New("biz error")) } - fmt.Println("g1passed") - time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond) + //fmt.Println("g1passed") + time.Sleep(time.Duration(rand.Uint64()%80+10) * time.Millisecond) e.Exit() } } }() go func() { for { - e, b := api.Entry("abc") + e, b := sentinel.Entry("abc") if b != nil { - fmt.Println("g2blocked") + //fmt.Println("g2blocked") time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) } else { - fmt.Println("g2passed") + //fmt.Println("g2passed") time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond) e.Exit() } diff --git a/example/circuitbreaker/sentinel.yml b/example/circuitbreaker/sentinel.yml new file mode 100644 index 000000000..c0cb94785 --- /dev/null +++ b/example/circuitbreaker/sentinel.yml @@ -0,0 +1,4 @@ +version: "v1" +sentinel: + app: + name: sentinel-go-circuit-breaking-demo \ No newline at end of file diff --git a/example/freq_params_traffic/hot-pramas-sentinel.yml b/example/freq_params_traffic/hot-pramas-sentinel.yml deleted file mode 100644 index 689e6a0d4..000000000 --- a/example/freq_params_traffic/hot-pramas-sentinel.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: "v1" -sentinel: - app: - name: sentinel-go-demo - log: - dir: ./example/ - metric: - maxFileCount: 7 \ No newline at end of file diff --git a/example/freq_params_traffic/hot_params_flow_example.go b/example/hotspot_param_flow/hotspot_param_flow_example.go similarity index 56% rename from example/freq_params_traffic/hot_params_flow_example.go rename to example/hotspot_param_flow/hotspot_param_flow_example.go index 3773f5834..150054f6c 100644 --- a/example/freq_params_traffic/hot_params_flow_example.go +++ b/example/hotspot_param_flow/hotspot_param_flow_example.go @@ -20,55 +20,33 @@ func main() { var Resource = "test" // We should initialize Sentinel first. - err := sentinel.Init("./hot-pramas-sentinel.yml") + err := sentinel.InitDefault() if err != nil { log.Fatalf("Unexpected error: %+v", err) } _, err = hotspot.LoadRules([]*hotspot.Rule{ { - Id: "a1", + Id: "1", Resource: Resource, - MetricType: hotspot.Concurrency, - Behavior: hotspot.Reject, - ParamIndex: 0, - Threshold: 100, - MaxQueueingTimeMs: 0, - BurstCount: 10, - DurationInSec: 1, - SpecificItems: make(map[hotspot.SpecificValue]int64), - }, - { - Id: "a2", - Resource: Resource, - MetricType: hotspot.Concurrency, - Behavior: hotspot.Reject, + MetricType: hotspot.QPS, + ControlBehavior: hotspot.Reject, ParamIndex: 1, - Threshold: 100, + Threshold: 50, MaxQueueingTimeMs: 0, - BurstCount: 10, + BurstCount: 0, DurationInSec: 1, - SpecificItems: make(map[hotspot.SpecificValue]int64), + SpecificItems: map[hotspot.SpecificValue]int64{ + {ValKind: hotspot.KindInt, ValStr: "9"}: 0, + }, }, { - Id: "a3", + Id: "2", Resource: Resource, - MetricType: hotspot.Concurrency, - Behavior: hotspot.Reject, + MetricType: hotspot.QPS, + ControlBehavior: hotspot.Reject, ParamIndex: 2, - Threshold: 100, - MaxQueueingTimeMs: 0, - BurstCount: 10, - DurationInSec: 1, - SpecificItems: make(map[hotspot.SpecificValue]int64), - }, - { - Id: "a4", - Resource: Resource, - MetricType: hotspot.Concurrency, - Behavior: hotspot.Reject, - ParamIndex: 3, - Threshold: 100, + Threshold: 50, MaxQueueingTimeMs: 0, BurstCount: 10, DurationInSec: 1, @@ -88,20 +66,18 @@ func main() { sc.AddStatSlotLast(&stat.StatisticSlot{}) sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{}) - for i := 0; i < 100; i++ { + for i := 0; i < 10; i++ { go func() { for { - e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Int()%3000, uuid.New().String(), uuid.New().String())) + e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", uuid.New().String())) if b != nil { // Blocked. We could get the block reason from the BlockError. time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) - fmt.Println(util.CurrentTimeMillis(), " blocked") - } else if e == nil && b == nil { - fmt.Println("e is ni") + //fmt.Println(util.CurrentTimeMillis(), "blocked") } else { // Passed, wrap the logic here. - fmt.Println(util.CurrentTimeMillis(), " passed") - time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) + fmt.Println(util.CurrentTimeMillis(), "passed") + //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) // Be sure the entry is exited finally. e.Exit() } @@ -111,21 +87,18 @@ func main() { } for { - e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Int()%3000, uuid.New().String(), uuid.New().String())) + e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(false, uint32(9), "ahas", uuid.New().String())) if b != nil { // Blocked. We could get the block reason from the BlockError. time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) - fmt.Println(util.CurrentTimeMillis(), " blocked") - } else if e == nil && b == nil { - fmt.Println("e is ni") + fmt.Println(util.CurrentTimeMillis(), "blocked") } else { // Passed, wrap the logic here. - fmt.Println(util.CurrentTimeMillis(), " passed") + fmt.Println(util.CurrentTimeMillis(), "passed") time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) // Be sure the entry is exited finally. e.Exit() } - } } diff --git a/example/hotspot_param_flow/sentinel.yml b/example/hotspot_param_flow/sentinel.yml new file mode 100644 index 000000000..509853bdb --- /dev/null +++ b/example/hotspot_param_flow/sentinel.yml @@ -0,0 +1,4 @@ +version: "v1" +sentinel: + app: + name: sentinel-go-param-flow-demo \ No newline at end of file