diff --git a/.gitignore b/.gitignore index c8e4a809..a7020373 100644 --- a/.gitignore +++ b/.gitignore @@ -186,3 +186,5 @@ iOSInjectionProject/ **/xcshareddata/WorkspaceSettings.xcsettings # End of https://www.toptal.com/developers/gitignore/api/swift,xcode,Cobjective-c,osx + +.idea diff --git a/waku/v2/service/common_discovery_service.go b/waku/v2/service/common_discovery_service.go index c22f18f1..651c349b 100644 --- a/waku/v2/service/common_discovery_service.go +++ b/waku/v2/service/common_discovery_service.go @@ -49,8 +49,6 @@ func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData { return sp.channel } func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool { - sp.RLock() - defer sp.RUnlock() if err := sp.ErrOnNotRunning(); err != nil { return false } diff --git a/waku/v2/service/common_service.go b/waku/v2/service/common_service.go index 9bf3ea12..026651e4 100644 --- a/waku/v2/service/common_service.go +++ b/waku/v2/service/common_service.go @@ -4,6 +4,7 @@ import ( "context" "errors" "sync" + "sync/atomic" ) // this is common layout for all the services that require mutex protection and a guarantee that all running goroutines will be finished before stop finishes execution. This guarantee comes from waitGroup all one has to use CommonService.WaitGroup() in the goroutines that should finish by the end of stop function. @@ -12,7 +13,7 @@ type CommonService struct { cancel context.CancelFunc ctx context.Context wg sync.WaitGroup - started bool + started atomic.Bool } func NewCommonService() *CommonService { @@ -28,13 +29,13 @@ func NewCommonService() *CommonService { func (sp *CommonService) Start(ctx context.Context, fn func() error) error { sp.Lock() defer sp.Unlock() - if sp.started { + if sp.started.Load() { return ErrAlreadyStarted } - sp.started = true + sp.started.Store(true) sp.ctx, sp.cancel = context.WithCancel(ctx) if err := fn(); err != nil { - sp.started = false + sp.started.Store(false) sp.cancel() return err } @@ -48,18 +49,18 @@ var ErrNotStarted = errors.New("not started") func (sp *CommonService) Stop(fn func()) { sp.Lock() defer sp.Unlock() - if !sp.started { + if !sp.started.Load() { return } sp.cancel() fn() sp.wg.Wait() - sp.started = false + sp.started.Store(false) } // This is not a mutex protected function, it is up to the caller to use it in a mutex protected context func (sp *CommonService) ErrOnNotRunning() error { - if !sp.started { + if !sp.started.Load() { return ErrNotStarted } return nil