日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(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)閉或限制對下游的請求。

原理

  1.  通常熔斷器分為三個時期:CLOSED,OPEN,HALFOPEN
  2.  RPC 正常時,為 CLOSED;
  3.  當(dāng) RPC 錯誤增多時,熔斷器會被觸發(fā),進入 OPEN;
  4.  OPEN 后經(jīng)過一定的冷卻時間,熔斷器變?yōu)?HALFOPEN;
  5.  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 判斷是否達到熔斷限額(可以自定義)

 
 
 
 
  1. type TripFunc func(Metricser) bool 
  •  ThresholdTripFunc 錯誤閾值
  •  ConsecutiveTripFunc 連續(xù)錯誤超過閾值
  •  RateTripFunc 根據(jù)最少訪問數(shù)和錯誤率判斷

Metricser 訪問統(tǒng)計,包括成功數(shù)、失敗數(shù)、超時數(shù)、錯誤率、采樣數(shù)、連續(xù)錯誤數(shù) 

 
 
 
 
  1. type Metricser interface {  
  2.    Fail()    // records a failure  
  3.    Succeed() // records a success  
  4.    Timeout() // records a timeout  
  5.    Failures() int64    // return the number of failures  
  6.    Successes() int64   // return the number of successes  
  7.    Timeouts() int64    // return the number of timeouts  
  8.    ConseErrors() int64 // return the consecutive errors recently  
  9.    ErrorRate() float64 // rate = (timeouts + failures) / (timeouts + failures + successes)  
  10.    Samples() int64     // (timeouts + failures + successes)  
  11.    Counts() (successes, failures, timeouts int64)  
  12.    Reset()  

window 實現(xiàn)類 

 
 
 
 
  1. type window struct {  
  2.    sync.RWMutex  
  3.    oldest  int32     // oldest bucket index  
  4.    latest  int32     // latest bucket index  
  5.    buckets []bucket // buckets this window holds  
  6.    bucketTime time.Duration // time each bucket holds  
  7.    bucketNums int32         // the numbe of buckets  
  8.    inWindow   int32         // the number of buckets in the window  
  9.    allSuccess int64  
  10.    allFailure int64  
  11.    allTimeout int64  
  12.    conseErr int64  
  13. }  
  14. type bucket struct {  
  15.    failure int64  
  16.    success int64  
  17.    timeout int64  

用環(huán)形隊列實現(xiàn)動態(tài)統(tǒng)計。把一個連續(xù)的時間切成多個小份,每一個 bucket 保存 BucketTime 的統(tǒng)計數(shù)據(jù),BucketTime * BucketNums 是統(tǒng)計的時間區(qū)間。

每 BucketTime,會有一個 bucket 過期 

 
 
 
 
  1. if w.inWindow == w.bucketNums {  
  2.    // the lastest covered the oldest(latest == oldest)  
  3.    oldBucket := &w.buckets[w.oldest]  
  4.    atomic.AddInt64(&w.allSuccess, -oldBucket.Successes())  
  5.    atomic.AddInt64(&w.allFailure, -oldBucket.Failures())  
  6.    atomic.AddInt64(&w.allTimeout, -oldBucket.Timeouts())  
  7.    w.oldest++  
  8.    if w.oldest >= w.bucketNums {  
  9.       w.oldest = 0  
  10.    }  
  11. } else {  
  12.    w.inWindow++  
  13. }  
  14. w.latest++ 
  15. if w.latest >= w.bucketNums {  
  16.    w.latest = 0  
  17. }  
  18. (&w.buckets[w.latest]).Reset() 

Panel Metricser 的容器

PanelStateChangeHandler 熔斷事件 

 
 
 
 
  1. type PanelStateChangeHandler func(key string, oldState, newState State, m Metricser) 

缺陷

  1.  所有 breaker 公用同一個 BucketTime,統(tǒng)計周期不支持更新
  2.  冷卻時間不支持動態(tài)更新 

新聞名稱:Golang實現(xiàn)熔斷機制
文章分享:http://m.5511xx.com/article/ccejecs.html