Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7d5b41aba | ||
|
|
7f494b3d57 | ||
|
|
ac0f979ef9 | ||
|
|
a980f1f8d2 |
@@ -10,5 +10,6 @@ waiter.AddCloseHandler(func() {
|
||||
nacl.FinalizeStan()
|
||||
}, false)
|
||||
|
||||
// blocking wait, if no need to block (with http server, for example), you can omit .Wait() call
|
||||
waiter.Wait()
|
||||
```
|
||||
@@ -2,7 +2,7 @@ package grawt
|
||||
|
||||
type CloseHandler struct {
|
||||
waiter *Waiter
|
||||
Quit chan bool
|
||||
Quit chan struct{}
|
||||
active bool
|
||||
autoDone bool
|
||||
handlerFunc *func()
|
||||
|
||||
14
waiter.go
14
waiter.go
@@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type Waiter struct {
|
||||
blockingMode bool
|
||||
waitGroup sync.WaitGroup
|
||||
closeHandlers []*CloseHandler
|
||||
}
|
||||
@@ -16,7 +17,7 @@ type Waiter struct {
|
||||
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
||||
ch := CloseHandler{
|
||||
w,
|
||||
make(chan bool, 1),
|
||||
make(chan struct{}, 1),
|
||||
true,
|
||||
autoDone,
|
||||
f,
|
||||
@@ -31,7 +32,7 @@ func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
||||
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
||||
(*h.handlerFunc)()
|
||||
}
|
||||
h.Quit <- true
|
||||
close(h.Quit)
|
||||
if h.autoDone || forceWaitGroupDone {
|
||||
w.waitGroup.Done()
|
||||
}
|
||||
@@ -53,9 +54,17 @@ func (w *Waiter) Halt(err error) {
|
||||
} else {
|
||||
log.Info("Program was terminated gracefully.")
|
||||
}
|
||||
if !w.blockingMode {
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Waiter) Wait() {
|
||||
w.blockingMode = true
|
||||
log.Info("Waiting...")
|
||||
w.waitGroup.Wait()
|
||||
}
|
||||
@@ -67,6 +76,7 @@ func (w *Waiter) onSignal(sig os.Signal) {
|
||||
|
||||
func NewWaiter() *Waiter {
|
||||
w := Waiter{
|
||||
false,
|
||||
sync.WaitGroup{},
|
||||
make([]*CloseHandler, 0),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user