alibaba / sentinel-golang

Sentinel Go enables reliability and resiliency for Go microservices
https://sentinelguard.io/
Apache License 2.0
2.79k stars 439 forks source link

[BUG] 方法fromOpenToHalfOpen,如果状态转换出现回滚,stateChangedCounter会更新一条错误的数据。 #559

Closed JZWen closed 9 months ago

JZWen commented 9 months ago

sentinel-goland/core/circuitbreaker.go

func : fromOpenToHalfOpen // fromOpenToHalfOpen updates circuit breaker state machine from open to half-open. // Return true only if current goroutine successfully accomplished the transformation. func (b circuitBreakerBase) fromOpenToHalfOpen(ctx base.EntryContext) bool { if b.state.cas(Open, HalfOpen) { for _, listener := range stateChangeListeners { listener.OnTransformToHalfOpen(Open, *b.rule) }

    entry := ctx.Entry()
    if entry == nil {
        logging.Error(errors.New("nil entry"), "Nil entry in circuitBreakerBase.fromOpenToHalfOpen()", "rule", b.rule)
    } else {
        // add hook for entry exit
        // if the current circuit breaker performs the probe through this entry, but the entry was blocked,
        // this hook will guarantee current circuit breaker state machine will rollback to Open from Half-Open
        entry.WhenExit(func(entry *base.SentinelEntry, ctx *base.EntryContext) error {
            if ctx.IsBlocked() && b.state.cas(HalfOpen, Open) {
                for _, listener := range stateChangeListeners {
                    listener.OnTransformToOpen(HalfOpen, *b.rule, 1.0)
                }
            }
            return nil
        })
    }

    stateChangedCounter.Add(float64(1), b.BoundRule().Resource, "Open", "HalfOpen")
    return true
}
return false

}