4 Commits

Author SHA1 Message Date
jar3b
f7d5b41aba Use 'close(h.Quit)' instead of <- struct{}{}, up to 0.1.4 2019-06-18 01:12:42 +03:00
jar3b
7f494b3d57 Use 'chan struct{}' instead of 'chan bool' for quit channel for compatibility, up to 0.1.3 2019-06-18 00:50:05 +03:00
jar3b
ac0f979ef9 Rework blocking logic, up to 0.1.2 2019-04-09 17:55:36 +03:00
jar3b
a980f1f8d2 Add blocking mode support 2019-04-09 17:50:55 +03:00
3 changed files with 14 additions and 3 deletions

View File

@@ -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()
```

View File

@@ -2,7 +2,7 @@ package grawt
type CloseHandler struct {
waiter *Waiter
Quit chan bool
Quit chan struct{}
active bool
autoDone bool
handlerFunc *func()

View File

@@ -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),
}