Skip to content

Commit

Permalink
Update hot-spot param flow example
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Jul 29, 2020
1 parent 7ee9542 commit 7681ae9
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions example/hotspot_param_flow/hotspot_param_flow_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"time"

sentinel "github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/flow"
"github.com/alibaba/sentinel-golang/core/hotspot"
"github.com/alibaba/sentinel-golang/core/stat"
"github.com/alibaba/sentinel-golang/core/system"
"github.com/alibaba/sentinel-golang/util"
"github.com/google/uuid"
)

type fooStruct struct {
n int64
}

func main() {
var Resource = "test"

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

_, err = hotspot.LoadRules([]*hotspot.Rule{
{
Id: "1",
Resource: Resource,
MetricType: hotspot.QPS,
ControlBehavior: hotspot.Reject,
Expand All @@ -41,7 +39,6 @@ func main() {
},
},
{
Id: "2",
Resource: Resource,
MetricType: hotspot.QPS,
ControlBehavior: hotspot.Reject,
Expand All @@ -58,26 +55,19 @@ func main() {
return
}

sc := base.NewSlotChain()
sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{})
sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{})
sc.AddRuleCheckSlotLast(&flow.FlowSlot{})
sc.AddRuleCheckSlotLast(&hotspot.Slot{})
sc.AddStatSlotLast(&stat.StatisticSlot{})
sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{})

fmt.Println("Sentinel Go hot-spot param flow control demo is running. You may see the pass/block metric in the metric log.")
for i := 0; i < 10; i++ {
go func() {
for {
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", uuid.New().String()))
e, b := sentinel.Entry(Resource, sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", fooStruct{rand.Int63() % 5}))
if b != nil {
// Blocked. We could get the block reason from the BlockError.
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
//fmt.Println(util.CurrentTimeMillis(), "blocked")
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
fmt.Println(util.CurrentTimeMillis(), b.Error())
} else {
// Passed, wrap the logic here.
fmt.Println(util.CurrentTimeMillis(), "passed")
//time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
time.Sleep(time.Duration(rand.Uint64()%50) * time.Millisecond)
// Be sure the entry is exited finally.
e.Exit()
}
Expand All @@ -87,15 +77,14 @@ func main() {
}

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

// Be sure the entry is exited finally.
e.Exit()
Expand Down

0 comments on commit 7681ae9

Please sign in to comment.