自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang 實(shí)現(xiàn)熔斷機(jī)制

開(kāi)發(fā) 后端
一些場(chǎng)景下,為了保障服務(wù)穩(wěn)定性會(huì)引入熔斷機(jī)制。本文介紹了用 Go 語(yǔ)言自己實(shí)現(xiàn)熔斷需要什么操作。

 一些場(chǎng)景下,為了保障服務(wù)穩(wěn)定性會(huì)引入熔斷機(jī)制。本文介紹了用 Go 語(yǔ)言自己實(shí)現(xiàn)熔斷需要什么操作。

什么是熔斷?

熔斷是指在下游發(fā)生錯(cuò)誤時(shí)上游主動(dòng)關(guān)閉或限制對(duì)下游的請(qǐng)求。

原理

  1.  通常熔斷器分為三個(gè)時(shí)期:CLOSED,OPEN,HALFOPEN
  2.  RPC 正常時(shí),為 CLOSED;
  3.  當(dāng) RPC 錯(cuò)誤增多時(shí),熔斷器會(huì)被觸發(fā),進(jìn)入 OPEN;
  4.  OPEN 后經(jīng)過(guò)一定的冷卻時(shí)間,熔斷器變?yōu)?HALFOPEN;
  5.  HALFOPEN 時(shí)會(huì)對(duì)下游進(jìn)行一些有策略的訪問(wèn),然后根據(jù)結(jié)果決定是變?yōu)?CLOSED,還是 OPEN;

總得來(lái)說(shuō)三個(gè)狀態(tài)的轉(zhuǎn)換大致如下圖:

Go 實(shí)現(xiàn)

https://github.com/rubyist/circuitbreaker

IsAllowed 是否允許請(qǐng)求,根據(jù)當(dāng)前狀態(tài)判斷

CLOSE 允許

OPEN

  •  在 CoolingTimeout 冷卻時(shí)間內(nèi),不允許
  •  過(guò)了冷卻時(shí)間,狀態(tài)變?yōu)?HALFOPEN,允許訪問(wèn)

HALFOPEN

  •  在 DetectTimeout 檢測(cè)時(shí)間內(nèi),允許訪問(wèn)
  •  否則不允許

atomic.StoreInt32((*int32)(&b.state), int32(HALFOPEN))

trip 判斷是否達(dá)到熔斷限額(可以自定義)

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

Metricser 訪問(wèn)統(tǒng)計(jì),包括成功數(shù)、失敗數(shù)、超時(shí)數(shù)、錯(cuò)誤率、采樣數(shù)、連續(xù)錯(cuò)誤數(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 實(shí)現(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)形隊(duì)列實(shí)現(xiàn)動(dòng)態(tài)統(tǒng)計(jì)。把一個(gè)連續(xù)的時(shí)間切成多個(gè)小份,每一個(gè) bucket 保存 BucketTime 的統(tǒng)計(jì)數(shù)據(jù),BucketTime * BucketNums 是統(tǒng)計(jì)的時(shí)間區(qū)間。

每 BucketTime,會(huì)有一個(gè) bucket 過(guò)期 

  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 公用同一個(gè) BucketTime,統(tǒng)計(jì)周期不支持更新
  2.  冷卻時(shí)間不支持動(dòng)態(tài)更新 

 

責(zé)任編輯:龐桂玉 來(lái)源: 馬哥Linux運(yùn)維
相關(guān)推薦

2021-11-25 09:55:47

Golang熔斷器語(yǔ)言

2021-07-22 09:43:09

Golang語(yǔ)言并發(fā)機(jī)制

2021-02-22 11:30:07

Golang 1.16ModuleGolang

2020-09-26 10:56:33

服務(wù)器熔斷服務(wù)隔離

2022-07-05 09:44:25

服務(wù)治理熔斷限流

2022-08-08 08:31:00

Linux內(nèi)存管理

2022-12-28 08:08:57

2025-02-27 08:00:00

熔斷機(jī)制微服務(wù)Spring

2025-02-27 09:35:22

2022-08-26 10:24:48

架構(gòu)Golang

2022-05-17 12:23:25

排序算法面試

2025-04-09 11:15:00

服務(wù)熔斷服務(wù)降分布式系統(tǒng)

2022-01-17 10:55:50

微服務(wù)API網(wǎng)關(guān)

2020-07-28 08:32:57

微服務(wù)API網(wǎng)關(guān)熔斷

2022-11-16 17:16:41

spring限流

2023-01-26 00:59:39

B-Treegolang度量衡

2024-11-12 08:00:00

LSM樹(shù)GolangMemTable

2023-07-03 07:40:13

VueGolangweb

2024-10-09 17:19:04

GoGolangKubernetes

2024-06-19 10:31:48

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)