前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo-go的DefaultHealthChecker

聊聊dubbo-go的DefaultHealthChecker

作者头像
code4it
发布2020-08-04 14:16:18
2940
发布2020-08-04 14:16:18
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下dubbo-go的DefaultHealthChecker

DefaultHealthChecker

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
func init() {
    extension.SethealthChecker(constant.DEFAULT_HEALTH_CHECKER, NewDefaultHealthChecker)
}

// DefaultHealthChecker is the default implementation of HealthChecker, which determines the health status of
// the invoker based on the number of successive bad request and the current active request.
type DefaultHealthChecker struct {
    // the limit of outstanding request
    outStandingRequestConutLimit int32
    // the threshold of successive-failure-request
    requestSuccessiveFailureThreshold int32
    // value of circuit-tripped timeout factor
    circuitTrippedTimeoutFactor int32
}
  • DefaultHealthChecker定义了outStandingRequestConutLimit、requestSuccessiveFailureThreshold、circuitTrippedTimeoutFactor属性

NewDefaultHealthChecker

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
// NewDefaultHealthChecker constructs a new DefaultHealthChecker based on the url
func NewDefaultHealthChecker(url *common.URL) router.HealthChecker {
    return &DefaultHealthChecker{
        outStandingRequestConutLimit:      int32(url.GetParamInt(constant.OUTSTANDING_REQUEST_COUNT_LIMIT_KEY, math.MaxInt32)),
        requestSuccessiveFailureThreshold: int32(url.GetParamInt(constant.SUCCESSIVE_FAILED_REQUEST_THRESHOLD_KEY, constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF)),
        circuitTrippedTimeoutFactor:       int32(url.GetParamInt(constant.CIRCUIT_TRIPPED_TIMEOUT_FACTOR_KEY, constant.DEFAULT_CIRCUIT_TRIPPED_TIMEOUT_FACTOR)),
    }
}
  • NewDefaultHealthChecker实例化DefaultHealthChecker

IsHealthy

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
// IsHealthy evaluates the healthy state on the given Invoker based on the number of successive bad request
// and the current active request
func (c *DefaultHealthChecker) IsHealthy(invoker protocol.Invoker) bool {
    urlStatus := protocol.GetURLStatus(invoker.GetUrl())
    if c.isCircuitBreakerTripped(urlStatus) || urlStatus.GetActive() > c.GetOutStandingRequestConutLimit() {
        logger.Debugf("Invoker [%s] is currently in circuitbreaker tripped state", invoker.GetUrl().Key())
        return false
    }
    return true
}
  • IsHealthy方法通过protocol.GetURLStatus(invoker.GetUrl())获取urlStatus,之后根据c.isCircuitBreakerTripped(urlStatus)或者urlStatus.GetActive() > c.GetOutStandingRequestConutLimit()判断是否处于circuitbreaker tripped状态

isCircuitBreakerTripped

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
// isCircuitBreakerTripped determine whether the invoker is in the tripped state by the number of successive bad request
func (c *DefaultHealthChecker) isCircuitBreakerTripped(status *protocol.RPCStatus) bool {
    circuitBreakerTimeout := c.getCircuitBreakerTimeout(status)
    currentTime := protocol.CurrentTimeMillis()
    if circuitBreakerTimeout <= 0 {
        return false
    }
    return circuitBreakerTimeout > currentTime
}
  • isCircuitBreakerTripped方法通过c.getCircuitBreakerTimeout(status)获取circuitBreakerTimeout,若circuitBreakerTimeout小于等于0返回false,否则判断circuitBreakerTimeout是否大于currentTime

getCircuitBreakerTimeout

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
// getCircuitBreakerTimeout get the timestamp recovered from tripped state, the unit is millisecond
func (c *DefaultHealthChecker) getCircuitBreakerTimeout(status *protocol.RPCStatus) int64 {
    sleepWindow := c.getCircuitBreakerSleepWindowTime(status)
    if sleepWindow <= 0 {
        return 0
    }
    return status.GetLastRequestFailedTimestamp() + sleepWindow
}
  • getCircuitBreakerTimeout方法先获取sleepWindow,若sleepWindow小于等于0则返回0,否则返回status.GetLastRequestFailedTimestamp() + sleepWindow

getCircuitBreakerSleepWindowTime

dubbo-go-v1.4.2/cluster/router/healthcheck/default_health_check.go

代码语言:javascript
复制
// getCircuitBreakerSleepWindowTime get the sleep window time of invoker, the unit is millisecond
func (c *DefaultHealthChecker) getCircuitBreakerSleepWindowTime(status *protocol.RPCStatus) int64 {

    successiveFailureCount := status.GetSuccessiveRequestFailureCount()
    diff := successiveFailureCount - c.GetRequestSuccessiveFailureThreshold()
    if diff < 0 {
        return 0
    } else if diff > constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF {
        diff = constant.DEFAULT_SUCCESSIVE_FAILED_REQUEST_MAX_DIFF
    }
    sleepWindow := (1 << diff) * c.GetCircuitTrippedTimeoutFactor()
    if sleepWindow > constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS {
        sleepWindow = constant.MAX_CIRCUIT_TRIPPED_TIMEOUT_IN_MS
    }
    return int64(sleepWindow)
}
  • getCircuitBreakerSleepWindowTime方法通过对比status.GetSuccessiveRequestFailureCount()及c.GetRequestSuccessiveFailureThreshold()计算diff,进而根据c.GetCircuitTrippedTimeoutFactor()计算sleepWindow

小结

DefaultHealthChecker定义了outStandingRequestConutLimit、requestSuccessiveFailureThreshold、circuitTrippedTimeoutFactor属性;IsHealthy方法通过protocol.GetURLStatus(invoker.GetUrl())获取urlStatus,之后根据c.isCircuitBreakerTripped(urlStatus)或者urlStatus.GetActive() > c.GetOutStandingRequestConutLimit()判断是否处于circuitbreaker tripped状态

doc

  • default_health_check
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • DefaultHealthChecker
  • NewDefaultHealthChecker
  • IsHealthy
  • isCircuitBreakerTripped
  • getCircuitBreakerTimeout
  • getCircuitBreakerSleepWindowTime
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档