2 Commits

Author SHA1 Message Date
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
3 changed files with 10 additions and 11 deletions

View File

@@ -10,5 +10,6 @@ waiter.AddCloseHandler(func() {
nacl.FinalizeStan() nacl.FinalizeStan()
}, false) }, false)
waiter.Wait(true) // 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 { type CloseHandler struct {
waiter *Waiter waiter *Waiter
Quit chan bool Quit chan struct{}
active bool active bool
autoDone bool autoDone bool
handlerFunc *func() handlerFunc *func()

View File

@@ -17,7 +17,7 @@ type Waiter struct {
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler { func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
ch := CloseHandler{ ch := CloseHandler{
w, w,
make(chan bool, 1), make(chan struct{}, 1),
true, true,
autoDone, autoDone,
f, f,
@@ -32,7 +32,7 @@ func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
if h.handlerFunc != nil && *h.handlerFunc != nil { if h.handlerFunc != nil && *h.handlerFunc != nil {
(*h.handlerFunc)() (*h.handlerFunc)()
} }
h.Quit <- true h.Quit <- struct{}{}
if h.autoDone || forceWaitGroupDone { if h.autoDone || forceWaitGroupDone {
w.waitGroup.Done() w.waitGroup.Done()
} }
@@ -63,12 +63,10 @@ func (w *Waiter) Halt(err error) {
} }
} }
func (w *Waiter) Wait(blockingMode bool) { func (w *Waiter) Wait() {
w.blockingMode = blockingMode w.blockingMode = true
if blockingMode {
log.Info("Waiting...") log.Info("Waiting...")
w.waitGroup.Wait() w.waitGroup.Wait()
}
} }
func (w *Waiter) onSignal(sig os.Signal) { func (w *Waiter) onSignal(sig os.Signal) {
@@ -78,7 +76,7 @@ func (w *Waiter) onSignal(sig os.Signal) {
func NewWaiter() *Waiter { func NewWaiter() *Waiter {
w := Waiter{ w := Waiter{
true, false,
sync.WaitGroup{}, sync.WaitGroup{},
make([]*CloseHandler, 0), make([]*CloseHandler, 0),
} }