mirror of
https://github.com/coder/coder.git
synced 2025-07-08 11:39:50 +00:00
committed by
GitHub
parent
6377f17fda
commit
a0320f455a
@ -1,6 +1,7 @@
|
|||||||
package notify
|
package notify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -8,6 +9,10 @@ import (
|
|||||||
|
|
||||||
// Notifier calls a Condition at most once for each count in countdown.
|
// Notifier calls a Condition at most once for each count in countdown.
|
||||||
type Notifier struct {
|
type Notifier struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
pollDone chan struct{}
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
condition Condition
|
condition Condition
|
||||||
notifiedAt map[time.Duration]bool
|
notifiedAt map[time.Duration]bool
|
||||||
@ -28,11 +33,14 @@ type Condition func(now time.Time) (deadline time.Time, callback func())
|
|||||||
// Notify is a convenience function that initializes a new Notifier
|
// Notify is a convenience function that initializes a new Notifier
|
||||||
// with the given condition, interval, and countdown.
|
// with the given condition, interval, and countdown.
|
||||||
// It is the responsibility of the caller to call close to stop polling.
|
// It is the responsibility of the caller to call close to stop polling.
|
||||||
func Notify(cond Condition, interval time.Duration, countdown ...time.Duration) (close func()) {
|
func Notify(cond Condition, interval time.Duration, countdown ...time.Duration) (closeFunc func()) {
|
||||||
notifier := New(cond, countdown...)
|
notifier := New(cond, countdown...)
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
go notifier.Poll(ticker.C)
|
go notifier.Poll(ticker.C)
|
||||||
return ticker.Stop
|
return func() {
|
||||||
|
ticker.Stop()
|
||||||
|
_ = notifier.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a Notifier that calls cond once every time it polls.
|
// New returns a Notifier that calls cond once every time it polls.
|
||||||
@ -45,7 +53,11 @@ func New(cond Condition, countdown ...time.Duration) *Notifier {
|
|||||||
return ct[i] < ct[j]
|
return ct[i] < ct[j]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
n := &Notifier{
|
n := &Notifier{
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
pollDone: make(chan struct{}),
|
||||||
countdown: ct,
|
countdown: ct,
|
||||||
condition: cond,
|
condition: cond,
|
||||||
notifiedAt: make(map[time.Duration]bool),
|
notifiedAt: make(map[time.Duration]bool),
|
||||||
@ -57,13 +69,29 @@ func New(cond Condition, countdown ...time.Duration) *Notifier {
|
|||||||
// Poll polls once immediately, and then once for every value from ticker.
|
// Poll polls once immediately, and then once for every value from ticker.
|
||||||
// Poll exits when ticker is closed.
|
// Poll exits when ticker is closed.
|
||||||
func (n *Notifier) Poll(ticker <-chan time.Time) {
|
func (n *Notifier) Poll(ticker <-chan time.Time) {
|
||||||
|
defer close(n.pollDone)
|
||||||
|
|
||||||
// poll once immediately
|
// poll once immediately
|
||||||
n.pollOnce(time.Now())
|
n.pollOnce(time.Now())
|
||||||
for t := range ticker {
|
for {
|
||||||
n.pollOnce(t)
|
select {
|
||||||
|
case <-n.ctx.Done():
|
||||||
|
return
|
||||||
|
case t, ok := <-ticker:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.pollOnce(t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Notifier) Close() error {
|
||||||
|
n.cancel()
|
||||||
|
<-n.pollDone
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Notifier) pollOnce(tick time.Time) {
|
func (n *Notifier) pollOnce(tick time.Time) {
|
||||||
n.lock.Lock()
|
n.lock.Lock()
|
||||||
defer n.lock.Unlock()
|
defer n.lock.Unlock()
|
||||||
|
@ -90,9 +90,10 @@ func TestNotifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
n := notify.New(cond, testCase.Countdown...)
|
n := notify.New(cond, testCase.Countdown...)
|
||||||
|
defer n.Close()
|
||||||
n.Poll(ch)
|
n.Poll(ch)
|
||||||
wg.Done()
|
|
||||||
}()
|
}()
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
for _, tick := range testCase.Ticks {
|
for _, tick := range testCase.Ticks {
|
||||||
|
Reference in New Issue
Block a user