From 4bf15afdd7db43f085619ec29b366a92d94cd6e8 Mon Sep 17 00:00:00 2001 From: Peter Nose Date: Tue, 23 Jan 2024 10:24:20 +0100 Subject: [PATCH] go/common/sync: Add IsRunning function to sync.One --- .changelog/5536.feature.md | 1 + go/common/sync/sync.go | 10 ++++++++++ go/p2p/peermgmt/peermgr.go | 4 +--- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changelog/5536.feature.md diff --git a/.changelog/5536.feature.md b/.changelog/5536.feature.md new file mode 100644 index 00000000000..3a7dbc33fc3 --- /dev/null +++ b/.changelog/5536.feature.md @@ -0,0 +1 @@ +go/common/sync: Add IsRunning function to sync.One diff --git a/go/common/sync/sync.go b/go/common/sync/sync.go index cbc26816a23..b02926bdbf3 100644 --- a/go/common/sync/sync.go +++ b/go/common/sync/sync.go @@ -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 { @@ -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 { @@ -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: @@ -86,3 +91,8 @@ func (s *one) TryStop() bool { return false } } + +// IsRunning implements One. +func (s *one) IsRunning() bool { + return len(s.startCh) > 0 +} diff --git a/go/p2p/peermgmt/peermgr.go b/go/p2p/peermgmt/peermgr.go index 6a65196cac7..c809cdea1b1 100644 --- a/go/p2p/peermgmt/peermgr.go +++ b/go/p2p/peermgmt/peermgr.go @@ -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.