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
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) }
}