-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.go
44 lines (38 loc) · 925 Bytes
/
observer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package graceful
import (
"sync"
"sync/atomic"
)
// observerPool to manage goroutines monitoring the graceful shutdown.
type observerPool struct {
wg *sync.WaitGroup
count *atomic.Int64
}
// Add will add a shutdown observer (goroutine) to pool.
func (o *observerPool) Add() func() {
o.wg.Add(1)
o.count.Add(1)
return o.newCloser()
}
// newCloser will return a function to be called when routine is done.
// The function should be called only once.
func (o *observerPool) newCloser() func() {
closed := &atomic.Bool{}
return func() {
if closed.Load() {
logger.Println("ignoring close call, observer already closed")
return
}
closed.Store(true)
o.wg.Done()
o.count.Add(-1)
}
}
// Pending returns the number of pending observers.
func (o *observerPool) Pending() int {
return int(o.count.Load())
}
// Wait will wait for all observers to finish.
func (o *observerPool) Wait() {
o.wg.Wait()
}