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

go/common/sync: Add IsRunning function to sync.One #5536

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5536.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/common/sync: Add IsRunning function to sync.One
10 changes: 10 additions & 0 deletions go/common/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type One interface {
// TryStop stops the running function, if any. This method blocks until
// the function finishes its work.
TryStop() bool

// IsRunning returns true iff the function is running.
IsRunning() bool
}

type one struct {
Expand All @@ -39,6 +42,7 @@ func NewOne() One {
}
}

// TryStart implements One.
func (s *one) TryStart(fn func(ctx context.Context)) bool {
// Allow running only one function at a time.
select {
Expand Down Expand Up @@ -75,6 +79,7 @@ func (s *one) TryStart(fn func(ctx context.Context)) bool {
return true
}

// TryStop implements One.
func (s *one) TryStop() bool {
select {
case ctrl := <-s.ctrlCh:
Expand All @@ -86,3 +91,8 @@ func (s *one) TryStop() bool {
return false
}
}

// IsRunning implements One.
func (s *one) IsRunning() bool {
return len(s.startCh) > 0
}
4 changes: 1 addition & 3 deletions go/p2p/peermgmt/peermgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ func (m *PeerManager) PeerTagger() api.PeerTagger {

// Start starts the background services required for the peer manager to work.
func (m *PeerManager) Start() {
m.startOne.TryStart(func(ctx context.Context) {
go m.run(ctx)
})
m.startOne.TryStart(m.run)
}

// Stop stops all background services. The method blocks until all services finish their work.
Expand Down
Loading