Skip to content

Commit

Permalink
Polish circuit breaking and "hot spot" param flow example
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 authored and louyuting committed Jun 18, 2020
1 parent 0834aad commit 36da212
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 66 deletions.
25 changes: 15 additions & 10 deletions example/circuitbreaker/circuit_breaker_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"math/rand"
"time"

"github.com/alibaba/sentinel-golang/api"
sentinel "github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
"github.com/alibaba/sentinel-golang/util"
)
Expand All @@ -28,45 +28,50 @@ func (s *stateChangeTestListener) OnTransformToHalfOpen(prev circuitbreaker.Stat
}

func main() {
err := api.InitDefault()
err := sentinel.InitDefault()
if err != nil {
log.Fatal(err)
}
ch := make(chan struct{})
// Register a state change listener so that we could observer the state change of the internal circuit breaker.
circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{})

_, err = circuitbreaker.LoadRules([]circuitbreaker.Rule{
// Statistic time span=10s, recoveryTimeout=3s, slowRtUpperBound=50ms, maxSlowRequestRatio=50%
circuitbreaker.NewSlowRtRule("abc", 10000, 3000, 50, 10, 0.5),
// Statistic time span=10s, recoveryTimeout=3s, maxErrorRatio=50%
circuitbreaker.NewErrorRatioRule("abc", 10000, 3000, 10, 0.5),
})
if err != nil {
log.Fatal(err)
}

fmt.Println("Sentinel Go circuit breaking demo is running. You may see the pass/block metric in the metric log.")
go func() {
for {
e, b := api.Entry("abc")
e, b := sentinel.Entry("abc")
if b != nil {
fmt.Println("g1blocked")
//fmt.Println("g1blocked")
time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond)
} else {
if rand.Uint64()%20 > 9 {
e.SetError(errors.New("biz error"))
// Record current invocation as error.
sentinel.TraceError(e, errors.New("biz error"))
}
fmt.Println("g1passed")
time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond)
//fmt.Println("g1passed")
time.Sleep(time.Duration(rand.Uint64()%80+10) * time.Millisecond)
e.Exit()
}
}
}()
go func() {
for {
e, b := api.Entry("abc")
e, b := sentinel.Entry("abc")
if b != nil {
fmt.Println("g2blocked")
//fmt.Println("g2blocked")
time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond)
} else {
fmt.Println("g2passed")
//fmt.Println("g2passed")
time.Sleep(time.Duration(rand.Uint64()%80) * time.Millisecond)
e.Exit()
}
Expand Down
4 changes: 4 additions & 0 deletions example/circuitbreaker/sentinel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "v1"
sentinel:
app:
name: sentinel-go-circuit-breaking-demo
8 changes: 0 additions & 8 deletions example/freq_params_traffic/hot-pramas-sentinel.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,33 @@ func main() {
var Resource = "test"

// We should initialize Sentinel first.
err := sentinel.Init("./hot-pramas-sentinel.yml")
err := sentinel.InitDefault()
if err != nil {
log.Fatalf("Unexpected error: %+v", err)
}

_, err = hotspot.LoadRules([]*hotspot.Rule{
{
Id: "a1",
Id: "1",
Resource: Resource,
MetricType: hotspot.Concurrency,
Behavior: hotspot.Reject,
ParamIndex: 0,
Threshold: 100,
MaxQueueingTimeMs: 0,
BurstCount: 10,
DurationInSec: 1,
SpecificItems: make(map[hotspot.SpecificValue]int64),
},
{
Id: "a2",
Resource: Resource,
MetricType: hotspot.Concurrency,
Behavior: hotspot.Reject,
MetricType: hotspot.QPS,
ControlBehavior: hotspot.Reject,
ParamIndex: 1,
Threshold: 100,
Threshold: 50,
MaxQueueingTimeMs: 0,
BurstCount: 10,
BurstCount: 0,
DurationInSec: 1,
SpecificItems: make(map[hotspot.SpecificValue]int64),
SpecificItems: map[hotspot.SpecificValue]int64{
{ValKind: hotspot.KindInt, ValStr: "9"}: 0,
},
},
{
Id: "a3",
Id: "2",
Resource: Resource,
MetricType: hotspot.Concurrency,
Behavior: hotspot.Reject,
MetricType: hotspot.QPS,
ControlBehavior: hotspot.Reject,
ParamIndex: 2,
Threshold: 100,
MaxQueueingTimeMs: 0,
BurstCount: 10,
DurationInSec: 1,
SpecificItems: make(map[hotspot.SpecificValue]int64),
},
{
Id: "a4",
Resource: Resource,
MetricType: hotspot.Concurrency,
Behavior: hotspot.Reject,
ParamIndex: 3,
Threshold: 100,
Threshold: 50,
MaxQueueingTimeMs: 0,
BurstCount: 10,
DurationInSec: 1,
Expand All @@ -88,20 +66,18 @@ func main() {
sc.AddStatSlotLast(&stat.StatisticSlot{})
sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{})

for i := 0; i < 100; i++ {
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.Int()%3000, uuid.New().String(), uuid.New().String()))
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Uint32()%30, "sentinel", uuid.New().String()))
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")
} else if e == nil && b == nil {
fmt.Println("e is ni")
//fmt.Println(util.CurrentTimeMillis(), "blocked")
} else {
// Passed, wrap the logic here.
fmt.Println(util.CurrentTimeMillis(), " passed")
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
fmt.Println(util.CurrentTimeMillis(), "passed")
//time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
// Be sure the entry is exited finally.
e.Exit()
}
Expand All @@ -111,21 +87,18 @@ func main() {
}

for {
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(true, rand.Int()%3000, uuid.New().String(), uuid.New().String()))
e, b := sentinel.Entry(Resource, sentinel.WithTrafficType(base.Inbound), sentinel.WithSlotChain(sc), sentinel.WithArgs(false, uint32(9), "ahas", uuid.New().String()))
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")
} else if e == nil && b == nil {
fmt.Println("e is ni")
fmt.Println(util.CurrentTimeMillis(), "blocked")
} else {
// Passed, wrap the logic here.
fmt.Println(util.CurrentTimeMillis(), " passed")
fmt.Println(util.CurrentTimeMillis(), "passed")
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)

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

}
}
4 changes: 4 additions & 0 deletions example/hotspot_param_flow/sentinel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "v1"
sentinel:
app:
name: sentinel-go-param-flow-demo

0 comments on commit 36da212

Please sign in to comment.