// Get .
func(g*Group)Get(namestring)CircuitBreaker{m,ok:=g.val.Load().(map[string]CircuitBreaker)ifok{breaker,ok:=m[name]ifok{returnbreaker// 很具name从val拿出 breaker 如果存在返回
}}// slowpath for group don`t have specified name breaker.
g.mutex.Lock()nm:=make(map[string]CircuitBreaker,len(m)+1)fork,v:=rangem{nm[k]=v}breaker:=g.New()nm[name]=breaker// 如果不存在 生成一个 并放入map 并返回
g.val.Store(nm)g.mutex.Unlock()returnbreaker}
Breaker 结构体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Breaker is a sre CircuitBreaker pattern.
typeBreakerstruct{statwindow.RollingCounterr*rand.Rand// rand.New(...) returns a non thread safe object
randLocksync.Mutex// Reducing the k will make adaptive throttling behave more aggressively,
// Increasing the k will make adaptive throttling behave less aggressively.
kfloat64requestint64stateint32}
// MarkSuccess mark requeest is success.
func(b*Breaker)MarkSuccess(){b.stat.Add(1)// 成功数+1
}// MarkFailed mark request is failed.
func(b*Breaker)MarkFailed(){// NOTE: when client reject requets locally, continue add counter let the
// drop ratio higher.
b.stat.Add(0)// 失败数+1
}