sentinel-golang icon indicating copy to clipboard operation
sentinel-golang copied to clipboard

[BUG] fromOpenToHalfOpen 如果状态出现回滚,那么stateChangedCounter会更新一条错误数据。

Open JZWen opened this issue 2 years ago • 0 comments

`// 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

}`

JZWen avatar Feb 23 '24 02:02 JZWen