// Copyright 1999-2020 Alibaba Group Holding Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
ch := make(chanstruct{}) for i := 0; i < 12; i++ { gofunc() { // 使用方法,resName名称一致,Inbound代表入口流量控制 e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound)) if b != nil { // 此时请求block // Blocked. We could get the block reason from the BlockError. //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) log.Println("限流。。。") } else { // 请求通过 // Passed, wrap the logic here. //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) log.Println("通过。。。") // Be sure the entry is exited finally. e.Exit() } }() } }
// Copyright 1999-2020 Alibaba Group Holding Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
var passNum int64 var blockNum int64 var totalNum int64
ch := make(chanstruct{}) for i := 0; i < 10; i++ { gofunc() { for { // 使用方法,resName名称一致,Inbound代表入口流量控制 atomic.AddInt64(&totalNum, 1) e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound)) if b != nil { // 此时请求block // Blocked. We could get the block reason from the BlockError. atomic.AddInt64(&blockNum, 1) } else { // 请求通过 // Passed, wrap the logic here. atomic.AddInt64(&passNum, 1) // Be sure the entry is exited finally. e.Exit() } time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) } }() }
var oldTotal int64 var oldPass int64 var oldBlock int64 for { time.Sleep(time.Second) tmpTotal := atomic.LoadInt64(&totalNum) tmpPass := atomic.LoadInt64(&passNum) tmpBlock := atomic.LoadInt64(&blockNum) log.Printf("total: %5d,pass %5d:block %5d", tmpTotal-oldTotal, tmpPass-oldPass, tmpBlock-oldBlock) oldTotal = tmpTotal oldPass = tmpPass oldBlock = tmpBlock } <-ch }
// Copyright 1999-2020 Alibaba Group Holding Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
ch := make(chanstruct{}) gofunc() { for { // 使用方法,resName名称一致,Inbound代表入口流量控制 e, b := sentinel.Entry(resName, sentinel.WithTrafficType(base.Inbound)) if b != nil { // 此时请求block // Blocked. We could get the block reason from the BlockError. //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) log.Println("限流。。。") } else { // 请求通过 // Passed, wrap the logic here. //time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond) log.Println("通过。。。") // Be sure the entry is exited finally. e.Exit() } time.Sleep(50 * time.Millisecond) } }() <-ch }
// Copyright 1999-2020 Alibaba Group Holding Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
func(s *stateChangeTestListener) OnTransformToHalfOpen(prev circuitbreaker.State, rule circuitbreaker.Rule) { fmt.Printf("rule.steategy: %+v, From %s to Half-Open, time: %d\n", rule.Strategy, prev.String(), util.CurrentTimeMillis()) }
funcmain() { var ( total int64 errNum int64 block int64 )
conf := config.NewDefaultConfig() // for testing, logging output to console conf.Sentinel.Log.Logger = logging.NewConsoleLogger() err := sentinel.InitWithConfig(conf) if err != nil { log.Fatal(err) } ch := make(chanstruct{}) // Register a state change listener so that we could observer the state change of the internal circuit breaker. // 内部状态发生转换时,可以执行某些方法,例如状态转换时,注销/注册到服务发现中心 circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{})
logging.Info("[CircuitBreaker ErrorCount] Sentinel Go circuit breaking demo is running. You may see the pass/block metric in the metric log.") gofunc() { for { atomic.AddInt64(&total, 1) e, b := sentinel.Entry("abc") if b != nil { // g1 blocked atomic.AddInt64(&block, 1) time.Sleep(time.Duration(rand.Uint64()%20) * time.Millisecond) } else { if rand.Uint64()%20 > 9 { // Record current invocation as error. // 随机记录一个错误 atomic.AddInt64(&errNum, 1) sentinel.TraceError(e, errors.New("biz error")) } // g1 passed time.Sleep(time.Duration(rand.Uint64()%80+10) * time.Millisecond) e.Exit() } } }() gofunc() { for { fmt.Printf("total:%5d block:%5d err:%5d\n", atomic.LoadInt64(&total), atomic.LoadInt64(&block), atomic.LoadInt64(&errNum)) time.Sleep(time.Second) } }() <-ch }
funcinitSentinel() { conf := config.NewDefaultConfig() // for testing, logging output to console conf.Sentinel.Log.Logger = logging.NewConsoleLogger() err := sentinel.InitWithConfig(conf) if err != nil { log.Fatal(err) } ch := make(chanstruct{}) // Register a state change listener so that we could observer the state change of the internal circuit breaker. circuitbreaker.RegisterStateChangeListeners(&stateChangeTestListener{})