Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It will cause a deadlock when call breaker.Counts() method in the OnStateChange() method #37

Open
Brandon-zhong opened this issue Jan 22, 2022 · 3 comments

Comments

@Brandon-zhong
Copy link

When the OnStateChange method is trigered, I want to know the breaker counts, so i call breaker.Counts(), but I get a deadlock
The code is :

OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
	// NOTICE:do not call breaker.Counts() in this method , it will cause a deadlock !!! 
	counts := breaker.Counts()
	logger.Infof("key: %s, breaker state has change, from %s to %s , the data --> %v", breaker.Name(), from.String(), to.String(), counts)
}

What method should I call when I want to know the counts number in OnStateChange method ?

@Jurperle
Copy link

1.creating a goroutine where a channel receive change and call breaker.Counts().
2.breaker.Counts() function removes lock. Count is just a copy.

@YoshiyukiMineo
Copy link
Member

1.creating a goroutine where a channel receive change and call breaker.Counts().

I think the above way is better:

	type StateChange struct {
		Name   string
		From   gobreaker.State
		To     gobreaker.State
		Counts gobreaker.Counts
	}
	ch := make(chan *StateChange)
	
	var cb *gobreaker.CircuitBreaker
	st := gobreaker.Settings{
		OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
			go func(name string, from gobreaker.State, to gobreaker.State) {
				ch <- &StateChange{
					Name:   name,
					From:   from,
					To:     to,
					Counts: cb.Counts(),
				}
			}(name, from, to)
		},
	}
	cb = gobreaker.NewCircuitBreaker(st)

@Jurperle
Copy link

Jurperle commented Mar 26, 2022

#40
This request can resolve the problem. Async OnStateChange function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants