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

Add a user-defined callback for state changes #13

Merged
merged 1 commit into from
May 10, 2020

Conversation

nabeken
Copy link
Contributor

@nabeken nabeken commented May 3, 2020

I'm planning to migrate from https://github.com/sony/gobreaker to this implementation since this library has several features I really want to use it.

Only the missing feature here is a user-defined callback for state changes. I found #9 but I don't see new updates since the last year.

Here is my attempt to implement the feature. Hope it sounds good enough.

Thanks for the great library.

@matope
Copy link
Contributor

matope commented May 10, 2020

@nabeken Thank you for your contribution and sorry to be late!

LGTM!

The only my concern was that the hook is invoked during lock. It might lead some performance issues because nobody can't Do() on the CB during lock.

However, If we somehow let the hook run out of lock (maybe using a goroutine), the user need to introduce complexity of concurrency. For example, your test should be like below. Those additional concurrency primitives looks totally painful.

// ....
var mu sync.Mutex
var wg sync.WaitGroup
wg.Add(len(expectedStateChanges))
hook := func(from, to circuitbreaker.State) {
		mu.Lock()
		defer mu.Unlock()
		defer wg.Done()
		actualStateChanges = append(actualStateChanges, stateChange{
			from: from,
			to:   to,
		})
}

// some mutations...

wg.Wait()

All right. Let's run the hook function synchronously during lock. Instead, it is the user's responsibility to keep the function thin.

Thank you for your great PR!

@matope matope merged commit 7b2d3d5 into mercari:master May 10, 2020
@nabeken nabeken deleted the feature-on-state-change branch May 14, 2020 01:34
@nabeken
Copy link
Contributor Author

nabeken commented May 14, 2020

@matope Thanks for the merge. I think calling the hook within the lock is reasonable guarantee which the library provides. The user can do whatever they wants if the performance matters (e.g. deferring to goroutines as you mention).

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

Successfully merging this pull request may close these issues.

None yet

2 participants