alibaba/sentinel-golang

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

JZWen opened this issue · 0 comments

JZWen commented

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

}