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

Feature/start stop #128

Merged
merged 15 commits into from
Feb 16, 2021
Merged

Feature/start stop #128

merged 15 commits into from
Feb 16, 2021

Conversation

joe94
Copy link
Member

@joe94 joe94 commented Feb 10, 2021

No description provided.

@joe94 joe94 marked this pull request as draft February 10, 2021 01:42
@codecov
Copy link

codecov bot commented Feb 10, 2021

Codecov Report

Merging #128 (a572664) into main (3118676) will increase coverage by 3.87%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #128      +/-   ##
==========================================
+ Coverage   57.36%   61.23%   +3.87%     
==========================================
  Files          16       16              
  Lines         842      859      +17     
==========================================
+ Hits          483      526      +43     
+ Misses        334      308      -26     
  Partials       25       25              
Impacted Files Coverage Δ
chrysom/metrics.go 0.00% <ø> (ø)
chrysom/client.go 92.45% <100.00%> (+18.50%) ⬆️
chrysom/store.go 10.00% <0.00%> (+10.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3118676...a572664. Read the comment docs.

@joe94 joe94 linked an issue Feb 10, 2021 that may be closed by this pull request
c.observer.mu.Lock()
defer c.observer.mu.Unlock()

if c.observer.state == running {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're intending to use this method as an uber/fx Lifecycle hook, it should return an error when Start has already been called without previous call to Stop.

@@ -354,28 +370,56 @@ func (c *Client) Start(ctx context.Context) error {
return ErrUndefinedIntervalTicker
}

c.observer.mu.Lock()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can certainly do this with a lock. However, the CAS pattern is way simpler for this. You don't need separate fields for a mutex and a state.

If you need some sort of control channel for the goroutine, it's pretty simple to just use an atomic.Value for that channel, which can also double as the atomic started/stopped state.

}

c.observer.ticker.Stop()
c.observer.shutdown <- struct{}{}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to actually close a shutdown channel. When Start is invoked, you create the channel. When Stop is invoked, you can close the channel and nil out the field.

The rationale here is that you don't want to miss state changes. When a channel is reused for shutdown, it can be possible for the previously started goroutine to miss the change and the currently started goroutine to receive the change. That's because there is no ordering to how goroutines receive data on channels.

@joe94 joe94 force-pushed the feature/startStop branch from ff104b9 to 4d5c38e Compare February 11, 2021 19:35
@joe94 joe94 force-pushed the feature/startStop branch from 170849f to ee3d358 Compare February 16, 2021 01:27
@joe94 joe94 marked this pull request as ready for review February 16, 2021 01:29
@sonarqubecloud
Copy link

@joe94 joe94 merged commit 9e05361 into main Feb 16, 2021
@joe94 joe94 deleted the feature/startStop branch February 16, 2021 01:56
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.

Add waitgroup for listeners
2 participants