Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
55ef377926 | ||
|
d75e8a9725 | ||
|
7096388647 | ||
|
b2c16b7750 | ||
|
486382b9d6 | ||
|
ed59ea1d0a | ||
|
9eec782920 | ||
|
f7d5b41aba | ||
|
7f494b3d57 | ||
|
ac0f979ef9 | ||
|
a980f1f8d2 |
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 jar3b
|
||||
Copyright (c) 2021 jar3b
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -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()
|
||||
```
|
@ -1,11 +1,17 @@
|
||||
package grawt
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type CloseHandler struct {
|
||||
waiter *Waiter
|
||||
Quit chan bool
|
||||
Quit chan struct{}
|
||||
active bool
|
||||
autoDone bool
|
||||
wgDone bool
|
||||
handlerFunc *func()
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func (ch *CloseHandler) Halt(err error) {
|
||||
|
4
go.mod
4
go.mod
@ -1,3 +1,5 @@
|
||||
module github.com/jar3b/grawt
|
||||
|
||||
require github.com/sirupsen/logrus v1.4.1
|
||||
go 1.16
|
||||
|
||||
require github.com/sirupsen/logrus v1.8.1
|
||||
|
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.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/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=
|
74
waiter.go
74
waiter.go
@ -9,32 +9,65 @@ import (
|
||||
)
|
||||
|
||||
type Waiter struct {
|
||||
sync.RWMutex
|
||||
blockingMode bool
|
||||
waitGroup sync.WaitGroup
|
||||
closeHandlers []*CloseHandler
|
||||
haltingFlag bool
|
||||
haltingMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (w *Waiter) isHalting() bool {
|
||||
w.haltingMutex.RLock()
|
||||
defer w.haltingMutex.RUnlock()
|
||||
return w.haltingFlag
|
||||
}
|
||||
|
||||
func (w *Waiter) setHalting(value bool) {
|
||||
w.haltingMutex.Lock()
|
||||
defer w.haltingMutex.Unlock()
|
||||
w.haltingFlag = value
|
||||
}
|
||||
|
||||
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
||||
ch := CloseHandler{
|
||||
w,
|
||||
make(chan bool, 1),
|
||||
true,
|
||||
autoDone,
|
||||
f,
|
||||
waiter: w,
|
||||
Quit: make(chan struct{}, 1),
|
||||
active: true,
|
||||
autoDone: autoDone,
|
||||
wgDone: false,
|
||||
handlerFunc: f,
|
||||
mu: &sync.Mutex{},
|
||||
}
|
||||
|
||||
w.Lock()
|
||||
w.waitGroup.Add(1)
|
||||
w.closeHandlers = append(w.closeHandlers, &ch)
|
||||
w.Unlock()
|
||||
|
||||
return &ch
|
||||
}
|
||||
|
||||
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !h.active {
|
||||
if !h.wgDone {
|
||||
w.waitGroup.Done()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
||||
(*h.handlerFunc)()
|
||||
}
|
||||
h.Quit <- true
|
||||
|
||||
close(h.Quit)
|
||||
if h.autoDone || forceWaitGroupDone {
|
||||
w.waitGroup.Done()
|
||||
h.wgDone = true
|
||||
}
|
||||
|
||||
h.active = false
|
||||
}
|
||||
|
||||
@ -43,21 +76,38 @@ func (w *Waiter) AddCloseHandler(f func(), waitForChannel bool) *CloseHandler {
|
||||
}
|
||||
|
||||
func (w *Waiter) Halt(err error) {
|
||||
for _, h := range w.closeHandlers {
|
||||
if h.active {
|
||||
w.terminateHandler(h, false)
|
||||
}
|
||||
if w.isHalting() {
|
||||
return
|
||||
}
|
||||
w.setHalting(true)
|
||||
|
||||
w.RLock()
|
||||
for _, h := range w.closeHandlers {
|
||||
w.terminateHandler(h, false)
|
||||
}
|
||||
w.RUnlock()
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("Program was terminated with error: %v", err)
|
||||
} else {
|
||||
log.Info("Program was terminated gracefully.")
|
||||
}
|
||||
if !w.blockingMode {
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
w.setHalting(false)
|
||||
}
|
||||
|
||||
func (w *Waiter) Wait() {
|
||||
w.blockingMode = true
|
||||
log.Info("Waiting...")
|
||||
w.waitGroup.Wait()
|
||||
log.Info("Terminated")
|
||||
}
|
||||
|
||||
func (w *Waiter) onSignal(sig os.Signal) {
|
||||
@ -67,8 +117,12 @@ func (w *Waiter) onSignal(sig os.Signal) {
|
||||
|
||||
func NewWaiter() *Waiter {
|
||||
w := Waiter{
|
||||
sync.RWMutex{},
|
||||
false,
|
||||
sync.WaitGroup{},
|
||||
make([]*CloseHandler, 0),
|
||||
false,
|
||||
sync.RWMutex{},
|
||||
}
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
Loading…
x
Reference in New Issue
Block a user