15
15
package main
16
16
17
17
import (
18
- "fmt"
19
18
"log"
20
19
"math/rand"
21
20
"time"
@@ -25,9 +24,10 @@ import (
25
24
"github.com/alibaba/sentinel-golang/core/config"
26
25
"github.com/alibaba/sentinel-golang/core/flow"
27
26
"github.com/alibaba/sentinel-golang/logging"
28
- "github.com/alibaba/sentinel-golang/util"
29
27
)
30
28
29
+ const resName = "example-flow-qps-resource"
30
+
31
31
func main () {
32
32
// We should initialize Sentinel first.
33
33
conf := config .NewDefaultConfig ()
@@ -40,7 +40,7 @@ func main() {
40
40
41
41
_ , err = flow .LoadRules ([]* flow.Rule {
42
42
{
43
- Resource : "some-test" ,
43
+ Resource : resName ,
44
44
TokenCalculateStrategy : flow .Direct ,
45
45
ControlBehavior : flow .Reject ,
46
46
Threshold : 10 ,
@@ -53,17 +53,15 @@ func main() {
53
53
}
54
54
55
55
ch := make (chan struct {})
56
-
57
56
for i := 0 ; i < 10 ; i ++ {
58
57
go func () {
59
58
for {
60
- e , b := sentinel .Entry ("some-test" , sentinel .WithTrafficType (base .Inbound ))
59
+ e , b := sentinel .Entry (resName , sentinel .WithTrafficType (base .Inbound ))
61
60
if b != nil {
62
61
// Blocked. We could get the block reason from the BlockError.
63
62
time .Sleep (time .Duration (rand .Uint64 ()% 10 ) * time .Millisecond )
64
63
} else {
65
64
// Passed, wrap the logic here.
66
- fmt .Println (util .CurrentTimeMillis (), "passed" )
67
65
time .Sleep (time .Duration (rand .Uint64 ()% 10 ) * time .Millisecond )
68
66
69
67
// Be sure the entry is exited finally.
@@ -73,5 +71,23 @@ func main() {
73
71
}
74
72
}()
75
73
}
74
+
75
+ // Simulate a scenario in which flow rules are updated concurrently
76
+ go func () {
77
+ time .Sleep (time .Second * 10 )
78
+ _ , err = flow .LoadRules ([]* flow.Rule {
79
+ {
80
+ Resource : resName ,
81
+ TokenCalculateStrategy : flow .Direct ,
82
+ ControlBehavior : flow .Reject ,
83
+ Threshold : 80 ,
84
+ StatIntervalInMs : 1000 ,
85
+ },
86
+ })
87
+ if err != nil {
88
+ log .Fatalf ("Unexpected error: %+v" , err )
89
+ return
90
+ }
91
+ }()
76
92
<- ch
77
93
}
0 commit comments