Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7096388647 | ||
|
|
b2c16b7750 |
@@ -1,13 +1,23 @@
|
|||||||
package grawt
|
package grawt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type CloseHandler struct {
|
type CloseHandler struct {
|
||||||
waiter *Waiter
|
waiter *Waiter
|
||||||
Quit chan struct{}
|
Quit chan struct{}
|
||||||
active bool
|
active bool
|
||||||
autoDone bool
|
autoDone bool
|
||||||
|
wgDone bool
|
||||||
handlerFunc *func()
|
handlerFunc *func()
|
||||||
|
mu *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *CloseHandler) Halt(err error) {
|
func (ch *CloseHandler) Halt(err error) {
|
||||||
ch.waiter.Halt(err)
|
ch.waiter.Halt(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ch *CloseHandler) Done() {
|
||||||
|
ch.waiter.terminateHandler(ch, true)
|
||||||
|
}
|
||||||
|
|||||||
14
waiter.go
14
waiter.go
@@ -22,7 +22,9 @@ func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
|||||||
Quit: make(chan struct{}, 1),
|
Quit: make(chan struct{}, 1),
|
||||||
active: true,
|
active: true,
|
||||||
autoDone: autoDone,
|
autoDone: autoDone,
|
||||||
|
wgDone: false,
|
||||||
handlerFunc: f,
|
handlerFunc: f,
|
||||||
|
mu: &sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Lock()
|
w.Lock()
|
||||||
@@ -34,18 +36,23 @@ func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
if !h.active {
|
if !h.active {
|
||||||
|
if !h.wgDone {
|
||||||
|
w.waitGroup.Done()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
||||||
(*h.handlerFunc)()
|
(*h.handlerFunc)()
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.active {
|
close(h.Quit)
|
||||||
close(h.Quit)
|
|
||||||
}
|
|
||||||
if h.autoDone || forceWaitGroupDone {
|
if h.autoDone || forceWaitGroupDone {
|
||||||
w.waitGroup.Done()
|
w.waitGroup.Done()
|
||||||
|
h.wgDone = true
|
||||||
}
|
}
|
||||||
|
|
||||||
h.active = false
|
h.active = false
|
||||||
@@ -87,6 +94,7 @@ func (w *Waiter) Wait() {
|
|||||||
w.blockingMode = true
|
w.blockingMode = true
|
||||||
log.Info("Waiting...")
|
log.Info("Waiting...")
|
||||||
w.waitGroup.Wait()
|
w.waitGroup.Wait()
|
||||||
|
log.Info("Terminated")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) onSignal(sig os.Signal) {
|
func (w *Waiter) onSignal(sig os.Signal) {
|
||||||
|
|||||||
Reference in New Issue
Block a user