Skip to content

Commit 7681ae9

Browse files
committed
Update hot-spot param flow example
Signed-off-by: Eric Zhao <[email protected]>
1 parent 7ee9542 commit 7681ae9

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

example/hotspot_param_flow/hotspot_param_flow_example.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
"time"
88

99
sentinel "github.com/alibaba/sentinel-golang/api"
10-
"github.com/alibaba/sentinel-golang/core/base"
11-
"github.com/alibaba/sentinel-golang/core/flow"
1210
"github.com/alibaba/sentinel-golang/core/hotspot"
13-
"github.com/alibaba/sentinel-golang/core/stat"
14-
"github.com/alibaba/sentinel-golang/core/system"
1511
"github.com/alibaba/sentinel-golang/util"
16-
"github.com/google/uuid"
1712
)
1813

14+
type fooStruct struct {
15+
n int64
16+
}
17+
1918
func main() {
2019
var Resource = "test"
2120

@@ -27,7 +26,6 @@ func main() {
2726

2827
_, err = hotspot.LoadRules([]*hotspot.Rule{
2928
{
30-
Id: "1",
3129
Resource: Resource,
3230
MetricType: hotspot.QPS,
3331
ControlBehavior: hotspot.Reject,
@@ -41,7 +39,6 @@ func main() {
4139
},
4240
},
4341
{
44-
Id: "2",
4542
Resource: Resource,
4643
MetricType: hotspot.QPS,
4744
ControlBehavior: hotspot.Reject,
@@ -58,26 +55,19 @@ func main() {
5855
return
5956
}
6057

61-
sc := base.NewSlotChain()
62-
sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{})
63-
sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{})
64-
sc.AddRuleCheckSlotLast(&flow.FlowSlot{})
65-
sc.AddRuleCheckSlotLast(&hotspot.Slot{})
66-
sc.AddStatSlotLast(&stat.StatisticSlot{})
67-
sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{})
68-
58+
fmt.Println("Sentinel Go hot-spot param flow control demo is running. You may see the pass/block metric in the metric log.")
6959
for i := 0; i < 10; i++ {
7060
go func() {
7161
for {
72-
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", uuid.New().String()))
62+
e, b := sentinel.Entry(Resource, sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", fooStruct{rand.Int63() % 5}))
7363
if b != nil {
7464
// Blocked. We could get the block reason from the BlockError.
75-
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
76-
//fmt.Println(util.CurrentTimeMillis(), "blocked")
65+
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
66+
fmt.Println(util.CurrentTimeMillis(), b.Error())
7767
} else {
7868
// Passed, wrap the logic here.
7969
fmt.Println(util.CurrentTimeMillis(), "passed")
80-
//time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
70+
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
8171
// Be sure the entry is exited finally.
8272
e.Exit()
8373
}
@@ -87,15 +77,14 @@ func main() {
8777
}
8878

8979
for {
90-
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(false, uint32(9), "ahas", uuid.New().String()))
80+
e, b := sentinel.Entry(Resource, sentinel.WithArgs(false, uint32(9), "ahas", fooStruct{rand.Int63() % 5}))
9181
if b != nil {
9282
// Blocked. We could get the block reason from the BlockError.
93-
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
94-
fmt.Println(util.CurrentTimeMillis(), "blocked")
83+
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
9584
} else {
9685
// Passed, wrap the logic here.
9786
fmt.Println(util.CurrentTimeMillis(), "passed")
98-
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
87+
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
9988

10089
// Be sure the entry is exited finally.
10190
e.Exit()

0 commit comments

Comments
 (0)