diff --git a/example/flow/qps/qps_limit_example.go b/example/flow/qps/qps_limit_example.go index 19b839a21..ee726a703 100644 --- a/example/flow/qps/qps_limit_example.go +++ b/example/flow/qps/qps_limit_example.go @@ -15,7 +15,6 @@ package main import ( - "fmt" "log" "math/rand" "time" @@ -25,9 +24,10 @@ import ( "github.com/alibaba/sentinel-golang/core/config" "github.com/alibaba/sentinel-golang/core/flow" "github.com/alibaba/sentinel-golang/logging" - "github.com/alibaba/sentinel-golang/util" ) +const resName = "example-flow-qps-resource" + func main() { // We should initialize Sentinel first. conf := config.NewDefaultConfig() @@ -40,7 +40,7 @@ func main() { _, err = flow.LoadRules([]*flow.Rule{ { - Resource: "some-test", + Resource: resName, TokenCalculateStrategy: flow.Direct, ControlBehavior: flow.Reject, Threshold: 10, @@ -53,17 +53,15 @@ func main() { } ch := make(chan struct{}) - for i := 0; i < 10; i++ { go func() { for { - e, b := sentinel.Entry("some-test", sentinel.WithTrafficType(base.Inbound)) + e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound)) if b != nil { // Blocked. We could get the block reason from the BlockError. time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) } else { // Passed, wrap the logic here. - fmt.Println(util.CurrentTimeMillis(), "passed") time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) // Be sure the entry is exited finally. @@ -73,5 +71,23 @@ func main() { } }() } + + // Simulate a scenario in which flow rules are updated concurrently + go func() { + time.Sleep(time.Second * 10) + _, err = flow.LoadRules([]*flow.Rule{ + { + Resource: resName, + TokenCalculateStrategy: flow.Direct, + ControlBehavior: flow.Reject, + Threshold: 80, + StatIntervalInMs: 1000, + }, + }) + if err != nil { + log.Fatalf("Unexpected error: %+v", err) + return + } + }() <-ch } diff --git a/go.mod b/go.mod index 9c1f9d26b..f5123ca09 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/fsnotify/fsnotify v1.4.7 github.com/google/uuid v1.1.1 github.com/pkg/errors v0.9.1 - github.com/shirou/gopsutil v3.20.11 + github.com/shirou/gopsutil v3.20.11+incompatible github.com/stretchr/testify v1.5.1 go.uber.org/multierr v1.5.0 gopkg.in/yaml.v2 v2.2.8 diff --git a/go.sum b/go.sum index ae789e2c4..40dc8256e 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto= +github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=