新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Golang實現(xiàn)熔斷機制
一些場景下,為了保障服務(wù)穩(wěn)定性會引入熔斷機制。本文介紹了用 Go 語言自己實現(xiàn)熔斷需要什么操作。

成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,先為德清等服務(wù)建站,德清等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為德清企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
什么是熔斷?
熔斷是指在下游發(fā)生錯誤時上游主動關(guān)閉或限制對下游的請求。
原理
- 通常熔斷器分為三個時期:CLOSED,OPEN,HALFOPEN
- RPC 正常時,為 CLOSED;
- 當(dāng) RPC 錯誤增多時,熔斷器會被觸發(fā),進入 OPEN;
- OPEN 后經(jīng)過一定的冷卻時間,熔斷器變?yōu)?HALFOPEN;
- HALFOPEN 時會對下游進行一些有策略的訪問,然后根據(jù)結(jié)果決定是變?yōu)?CLOSED,還是 OPEN;
總得來說三個狀態(tài)的轉(zhuǎn)換大致如下圖:
Go 實現(xiàn)
https://github.com/rubyist/circuitbreaker
IsAllowed 是否允許請求,根據(jù)當(dāng)前狀態(tài)判斷
CLOSE 允許
OPEN
- 在 CoolingTimeout 冷卻時間內(nèi),不允許
- 過了冷卻時間,狀態(tài)變?yōu)?HALFOPEN,允許訪問
HALFOPEN
- 在 DetectTimeout 檢測時間內(nèi),允許訪問
- 否則不允許
atomic.StoreInt32((*int32)(&b.state), int32(HALFOPEN))
trip 判斷是否達到熔斷限額(可以自定義)
- type TripFunc func(Metricser) bool
- ThresholdTripFunc 錯誤閾值
- ConsecutiveTripFunc 連續(xù)錯誤超過閾值
- RateTripFunc 根據(jù)最少訪問數(shù)和錯誤率判斷
Metricser 訪問統(tǒng)計,包括成功數(shù)、失敗數(shù)、超時數(shù)、錯誤率、采樣數(shù)、連續(xù)錯誤數(shù)
- type Metricser interface {
- Fail() // records a failure
- Succeed() // records a success
- Timeout() // records a timeout
- Failures() int64 // return the number of failures
- Successes() int64 // return the number of successes
- Timeouts() int64 // return the number of timeouts
- ConseErrors() int64 // return the consecutive errors recently
- ErrorRate() float64 // rate = (timeouts + failures) / (timeouts + failures + successes)
- Samples() int64 // (timeouts + failures + successes)
- Counts() (successes, failures, timeouts int64)
- Reset()
- }
window 實現(xiàn)類
- type window struct {
- sync.RWMutex
- oldest int32 // oldest bucket index
- latest int32 // latest bucket index
- buckets []bucket // buckets this window holds
- bucketTime time.Duration // time each bucket holds
- bucketNums int32 // the numbe of buckets
- inWindow int32 // the number of buckets in the window
- allSuccess int64
- allFailure int64
- allTimeout int64
- conseErr int64
- }
- type bucket struct {
- failure int64
- success int64
- timeout int64
- }
用環(huán)形隊列實現(xiàn)動態(tài)統(tǒng)計。把一個連續(xù)的時間切成多個小份,每一個 bucket 保存 BucketTime 的統(tǒng)計數(shù)據(jù),BucketTime * BucketNums 是統(tǒng)計的時間區(qū)間。
每 BucketTime,會有一個 bucket 過期
- if w.inWindow == w.bucketNums {
- // the lastest covered the oldest(latest == oldest)
- oldBucket := &w.buckets[w.oldest]
- atomic.AddInt64(&w.allSuccess, -oldBucket.Successes())
- atomic.AddInt64(&w.allFailure, -oldBucket.Failures())
- atomic.AddInt64(&w.allTimeout, -oldBucket.Timeouts())
- w.oldest++
- if w.oldest >= w.bucketNums {
- w.oldest = 0
- }
- } else {
- w.inWindow++
- }
- w.latest++
- if w.latest >= w.bucketNums {
- w.latest = 0
- }
- (&w.buckets[w.latest]).Reset()
Panel Metricser 的容器
PanelStateChangeHandler 熔斷事件
- type PanelStateChangeHandler func(key string, oldState, newState State, m Metricser)
缺陷
- 所有 breaker 公用同一個 BucketTime,統(tǒng)計周期不支持更新
- 冷卻時間不支持動態(tài)更新
新聞名稱:Golang實現(xiàn)熔斷機制
文章分享:http://m.5511xx.com/article/ccejecs.html


咨詢
建站咨詢
