Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
486382b9d6 | ||
|
|
ed59ea1d0a | ||
|
|
9eec782920 | ||
|
|
f7d5b41aba | ||
|
|
7f494b3d57 | ||
|
|
ac0f979ef9 | ||
|
|
a980f1f8d2 |
@@ -10,5 +10,6 @@ waiter.AddCloseHandler(func() {
|
|||||||
nacl.FinalizeStan()
|
nacl.FinalizeStan()
|
||||||
}, false)
|
}, false)
|
||||||
|
|
||||||
|
// blocking wait, if no need to block (with http server, for example), you can omit .Wait() call
|
||||||
waiter.Wait()
|
waiter.Wait()
|
||||||
```
|
```
|
||||||
@@ -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()
|
||||||
@@ -11,7 +11,3 @@ type CloseHandler struct {
|
|||||||
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)
|
|
||||||
}
|
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/jar3b/grawt
|
module github.com/jar3b/grawt
|
||||||
|
|
||||||
require github.com/sirupsen/logrus v1.4.1
|
go 1.15
|
||||||
|
|
||||||
|
require github.com/sirupsen/logrus v1.7.0
|
||||||
|
|||||||
10
go.sum
Normal file
10
go.sum
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
|
||||||
|
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
|
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
48
waiter.go
48
waiter.go
@@ -9,32 +9,45 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Waiter struct {
|
type Waiter struct {
|
||||||
|
sync.RWMutex
|
||||||
|
blockingMode bool
|
||||||
waitGroup sync.WaitGroup
|
waitGroup sync.WaitGroup
|
||||||
closeHandlers []*CloseHandler
|
closeHandlers []*CloseHandler
|
||||||
|
isHalting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
||||||
ch := CloseHandler{
|
ch := CloseHandler{
|
||||||
w,
|
waiter: w,
|
||||||
make(chan bool, 1),
|
Quit: make(chan struct{}, 1),
|
||||||
true,
|
active: true,
|
||||||
autoDone,
|
autoDone: autoDone,
|
||||||
f,
|
handlerFunc: f,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.Lock()
|
||||||
w.waitGroup.Add(1)
|
w.waitGroup.Add(1)
|
||||||
w.closeHandlers = append(w.closeHandlers, &ch)
|
w.closeHandlers = append(w.closeHandlers, &ch)
|
||||||
|
w.Unlock()
|
||||||
|
|
||||||
return &ch
|
return &ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
||||||
|
if !h.active {
|
||||||
|
return
|
||||||
|
}
|
||||||
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
||||||
(*h.handlerFunc)()
|
(*h.handlerFunc)()
|
||||||
}
|
}
|
||||||
h.Quit <- true
|
|
||||||
|
if h.active {
|
||||||
|
close(h.Quit)
|
||||||
|
}
|
||||||
if h.autoDone || forceWaitGroupDone {
|
if h.autoDone || forceWaitGroupDone {
|
||||||
w.waitGroup.Done()
|
w.waitGroup.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
h.active = false
|
h.active = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,19 +56,35 @@ func (w *Waiter) AddCloseHandler(f func(), waitForChannel bool) *CloseHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) Halt(err error) {
|
func (w *Waiter) Halt(err error) {
|
||||||
|
if w.isHalting {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.isHalting = true
|
||||||
|
|
||||||
|
w.RLock()
|
||||||
for _, h := range w.closeHandlers {
|
for _, h := range w.closeHandlers {
|
||||||
if h.active {
|
|
||||||
w.terminateHandler(h, false)
|
w.terminateHandler(h, false)
|
||||||
}
|
}
|
||||||
}
|
w.RUnlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Program was terminated with error: %v", err)
|
log.Errorf("Program was terminated with error: %v", err)
|
||||||
} else {
|
} else {
|
||||||
log.Info("Program was terminated gracefully.")
|
log.Info("Program was terminated gracefully.")
|
||||||
}
|
}
|
||||||
|
if !w.blockingMode {
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
} else {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.isHalting = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) Wait() {
|
func (w *Waiter) Wait() {
|
||||||
|
w.blockingMode = true
|
||||||
log.Info("Waiting...")
|
log.Info("Waiting...")
|
||||||
w.waitGroup.Wait()
|
w.waitGroup.Wait()
|
||||||
}
|
}
|
||||||
@@ -67,8 +96,11 @@ func (w *Waiter) onSignal(sig os.Signal) {
|
|||||||
|
|
||||||
func NewWaiter() *Waiter {
|
func NewWaiter() *Waiter {
|
||||||
w := Waiter{
|
w := Waiter{
|
||||||
|
sync.RWMutex{},
|
||||||
|
false,
|
||||||
sync.WaitGroup{},
|
sync.WaitGroup{},
|
||||||
make([]*CloseHandler, 0),
|
make([]*CloseHandler, 0),
|
||||||
|
false,
|
||||||
}
|
}
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|||||||
Reference in New Issue
Block a user