fix: rework sync logic

This commit is contained in:
jar3b 2020-10-30 18:20:27 +03:00
parent ed59ea1d0a
commit 486382b9d6
4 changed files with 37 additions and 14 deletions

View File

@ -1,9 +1,6 @@
package grawt
import "sync"
type CloseHandler struct {
sync.Mutex
waiter *Waiter
Quit chan struct{}
active bool
@ -14,7 +11,3 @@ type CloseHandler struct {
func (ch *CloseHandler) Halt(err error) {
ch.waiter.Halt(err)
}
func (ch *CloseHandler) Done() {
ch.waiter.terminateHandler(ch, true)
}

4
go.mod
View File

@ -1,3 +1,5 @@
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
View 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=

View File

@ -9,9 +9,11 @@ import (
)
type Waiter struct {
sync.RWMutex
blockingMode bool
waitGroup sync.WaitGroup
closeHandlers []*CloseHandler
isHalting bool
}
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
@ -22,25 +24,31 @@ func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
autoDone: autoDone,
handlerFunc: f,
}
w.Lock()
w.waitGroup.Add(1)
w.closeHandlers = append(w.closeHandlers, &ch)
w.Unlock()
return &ch
}
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
if !h.active {
return
}
if h.handlerFunc != nil && *h.handlerFunc != nil {
(*h.handlerFunc)()
}
h.Lock()
if h.active {
close(h.Quit)
}
if h.autoDone || forceWaitGroupDone {
w.waitGroup.Done()
}
h.active = false
h.Unlock()
}
func (w *Waiter) AddCloseHandler(f func(), waitForChannel bool) *CloseHandler {
@ -48,11 +56,17 @@ func (w *Waiter) AddCloseHandler(f func(), waitForChannel bool) *CloseHandler {
}
func (w *Waiter) Halt(err error) {
if w.isHalting {
return
}
w.isHalting = true
w.RLock()
for _, h := range w.closeHandlers {
if h.active {
w.terminateHandler(h, false)
}
}
w.RUnlock()
if err != nil {
log.Errorf("Program was terminated with error: %v", err)
} else {
@ -65,6 +79,8 @@ func (w *Waiter) Halt(err error) {
os.Exit(0)
}
}
w.isHalting = false
}
func (w *Waiter) Wait() {
@ -80,9 +96,11 @@ func (w *Waiter) onSignal(sig os.Signal) {
func NewWaiter() *Waiter {
w := Waiter{
sync.RWMutex{},
false,
sync.WaitGroup{},
make([]*CloseHandler, 0),
false,
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)