@@ -7,15 +7,14 @@ import (
7
7
"time"
8
8
9
9
sentinel "github.com/alibaba/sentinel-golang/api"
10
- "github.com/alibaba/sentinel-golang/core/base"
11
- "github.com/alibaba/sentinel-golang/core/flow"
12
10
"github.com/alibaba/sentinel-golang/core/hotspot"
13
- "github.com/alibaba/sentinel-golang/core/stat"
14
- "github.com/alibaba/sentinel-golang/core/system"
15
11
"github.com/alibaba/sentinel-golang/util"
16
- "github.com/google/uuid"
17
12
)
18
13
14
+ type fooStruct struct {
15
+ n int64
16
+ }
17
+
19
18
func main () {
20
19
var Resource = "test"
21
20
@@ -27,7 +26,6 @@ func main() {
27
26
28
27
_ , err = hotspot .LoadRules ([]* hotspot.Rule {
29
28
{
30
- Id : "1" ,
31
29
Resource : Resource ,
32
30
MetricType : hotspot .QPS ,
33
31
ControlBehavior : hotspot .Reject ,
@@ -41,7 +39,6 @@ func main() {
41
39
},
42
40
},
43
41
{
44
- Id : "2" ,
45
42
Resource : Resource ,
46
43
MetricType : hotspot .QPS ,
47
44
ControlBehavior : hotspot .Reject ,
@@ -58,26 +55,19 @@ func main() {
58
55
return
59
56
}
60
57
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." )
69
59
for i := 0 ; i < 10 ; i ++ {
70
60
go func () {
71
61
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 } ))
73
63
if b != nil {
74
64
// 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 () )
77
67
} else {
78
68
// Passed, wrap the logic here.
79
69
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 )
81
71
// Be sure the entry is exited finally.
82
72
e .Exit ()
83
73
}
@@ -87,15 +77,14 @@ func main() {
87
77
}
88
78
89
79
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 } ))
91
81
if b != nil {
92
82
// 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 )
95
84
} else {
96
85
// Passed, wrap the logic here.
97
86
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 )
99
88
100
89
// Be sure the entry is exited finally.
101
90
e .Exit ()
0 commit comments