From 7681ae9139ccd76f093e37ad286a1686d08e11de Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Wed, 29 Jul 2020 14:03:15 +0800 Subject: [PATCH] Update hot-spot param flow example Signed-off-by: Eric Zhao --- .../hotspot_param_flow_example.go | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/example/hotspot_param_flow/hotspot_param_flow_example.go b/example/hotspot_param_flow/hotspot_param_flow_example.go index ffb2ee232..755470806 100644 --- a/example/hotspot_param_flow/hotspot_param_flow_example.go +++ b/example/hotspot_param_flow/hotspot_param_flow_example.go @@ -7,15 +7,14 @@ import ( "time" sentinel "github.com/alibaba/sentinel-golang/api" - "github.com/alibaba/sentinel-golang/core/base" - "github.com/alibaba/sentinel-golang/core/flow" "github.com/alibaba/sentinel-golang/core/hotspot" - "github.com/alibaba/sentinel-golang/core/stat" - "github.com/alibaba/sentinel-golang/core/system" "github.com/alibaba/sentinel-golang/util" - "github.com/google/uuid" ) +type fooStruct struct { + n int64 +} + func main() { var Resource = "test" @@ -27,7 +26,6 @@ func main() { _, err = hotspot.LoadRules([]*hotspot.Rule{ { - Id: "1", Resource: Resource, MetricType: hotspot.QPS, ControlBehavior: hotspot.Reject, @@ -41,7 +39,6 @@ func main() { }, }, { - Id: "2", Resource: Resource, MetricType: hotspot.QPS, ControlBehavior: hotspot.Reject, @@ -58,26 +55,19 @@ func main() { return } - sc := base.NewSlotChain() - sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{}) - sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{}) - sc.AddRuleCheckSlotLast(&flow.FlowSlot{}) - sc.AddRuleCheckSlotLast(&hotspot.Slot{}) - sc.AddStatSlotLast(&stat.StatisticSlot{}) - sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{}) - + fmt.Println("Sentinel Go hot-spot param flow control demo is running. You may see the pass/block metric in the metric log.") 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.Uint32()%30, "sentinel", uuid.New().String())) + e, b := sentinel.Entry(Resource, sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", fooStruct{rand.Int63() % 5})) 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") + time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond) + fmt.Println(util.CurrentTimeMillis(), b.Error()) } else { // Passed, wrap the logic here. fmt.Println(util.CurrentTimeMillis(), "passed") - //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) + time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond) // Be sure the entry is exited finally. e.Exit() } @@ -87,15 +77,14 @@ func main() { } for { - e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(false, uint32(9), "ahas", uuid.New().String())) + e, b := sentinel.Entry(Resource, sentinel.WithArgs(false, uint32(9), "ahas", fooStruct{rand.Int63() % 5})) 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") + time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond) } else { // Passed, wrap the logic here. fmt.Println(util.CurrentTimeMillis(), "passed") - time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) + time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond) // Be sure the entry is exited finally. e.Exit()