Skip to content

Commit

Permalink
Merge pull request #2952 from ActiveState/DX-1772
Browse files Browse the repository at this point in the history
Add sync safety to poller package
  • Loading branch information
MDrakos authored Dec 15, 2023
2 parents 3916611 + 2703bb7 commit 7119a80
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/poller/poller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package poller

import (
"sync"
"time"

"github.com/ActiveState/cli/internal/errs"
Expand All @@ -12,6 +13,7 @@ import (
type Poller struct {
pollFunc func() (interface{}, error)
cache interface{}
cacheMutex sync.Mutex
done chan struct{}
errorReported bool
}
Expand All @@ -26,6 +28,8 @@ func New(interval time.Duration, pollFunc func() (interface{}, error)) *Poller {
}

func (p *Poller) ValueFromCache() interface{} {
p.cacheMutex.Lock()
defer p.cacheMutex.Unlock()
return p.cache
}

Expand Down Expand Up @@ -59,9 +63,11 @@ func (p *Poller) refresh() {
return
}

p.cacheMutex.Lock()
p.cache = info
p.cacheMutex.Unlock()
}

func (p *Poller) Close() {
p.done <- struct{}{}
close(p.done)
}

0 comments on commit 7119a80

Please sign in to comment.