Skip to content

Commit f854c93

Browse files
authored
Update rules in concurrency for flow qps example. (#344)
1 parent e147228 commit f854c93

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

example/flow/qps/qps_limit_example.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package main
1616

1717
import (
18-
"fmt"
1918
"log"
2019
"math/rand"
2120
"time"
@@ -25,9 +24,10 @@ import (
2524
"github.com/alibaba/sentinel-golang/core/config"
2625
"github.com/alibaba/sentinel-golang/core/flow"
2726
"github.com/alibaba/sentinel-golang/logging"
28-
"github.com/alibaba/sentinel-golang/util"
2927
)
3028

29+
const resName = "example-flow-qps-resource"
30+
3131
func main() {
3232
// We should initialize Sentinel first.
3333
conf := config.NewDefaultConfig()
@@ -40,7 +40,7 @@ func main() {
4040

4141
_, err = flow.LoadRules([]*flow.Rule{
4242
{
43-
Resource: "some-test",
43+
Resource: resName,
4444
TokenCalculateStrategy: flow.Direct,
4545
ControlBehavior: flow.Reject,
4646
Threshold: 10,
@@ -53,17 +53,15 @@ func main() {
5353
}
5454

5555
ch := make(chan struct{})
56-
5756
for i := 0; i < 10; i++ {
5857
go func() {
5958
for {
60-
e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound))
59+
e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound))
6160
if b != nil {
6261
// Blocked. We could get the block reason from the BlockError.
6362
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
6463
} else {
6564
// Passed, wrap the logic here.
66-
fmt.Println(util.CurrentTimeMillis(), "passed")
6765
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
6866

6967
// Be sure the entry is exited finally.
@@ -73,5 +71,23 @@ func main() {
7371
}
7472
}()
7573
}
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+
}()
7692
<-ch
7793
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/fsnotify/fsnotify v1.4.7
77
github.com/google/uuid v1.1.1
88
github.com/pkg/errors v0.9.1
9-
github.com/shirou/gopsutil v3.20.11
9+
github.com/shirou/gopsutil v3.20.11+incompatible
1010
github.com/stretchr/testify v1.5.1
1111
go.uber.org/multierr v1.5.0
1212
gopkg.in/yaml.v2 v2.2.8

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
1616
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1717
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1818
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
19+
github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto=
20+
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
1921
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
2022
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2123
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 commit comments

Comments
 (0)