Compare commits
No commits in common. "master" and "v0.1.2" have entirely different histories.
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2021 jar3b
|
Copyright (c) 2019 jar3b
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -1,17 +1,11 @@
|
|||||||
package grawt
|
package grawt
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CloseHandler struct {
|
type CloseHandler struct {
|
||||||
waiter *Waiter
|
waiter *Waiter
|
||||||
Quit chan struct{}
|
Quit chan bool
|
||||||
active bool
|
active bool
|
||||||
autoDone bool
|
autoDone bool
|
||||||
wgDone bool
|
|
||||||
handlerFunc *func()
|
handlerFunc *func()
|
||||||
mu *sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ch *CloseHandler) Halt(err error) {
|
func (ch *CloseHandler) Halt(err error) {
|
||||||
|
4
go.mod
4
go.mod
@ -1,5 +1,3 @@
|
|||||||
module github.com/jar3b/grawt
|
module github.com/jar3b/grawt
|
||||||
|
|
||||||
go 1.16
|
require github.com/sirupsen/logrus v1.4.1
|
||||||
|
|
||||||
require github.com/sirupsen/logrus v1.8.1
|
|
||||||
|
10
go.sum
10
go.sum
@ -1,10 +0,0 @@
|
|||||||
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=
|
|
62
waiter.go
62
waiter.go
@ -9,65 +9,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Waiter struct {
|
type Waiter struct {
|
||||||
sync.RWMutex
|
|
||||||
blockingMode bool
|
blockingMode bool
|
||||||
waitGroup sync.WaitGroup
|
waitGroup sync.WaitGroup
|
||||||
closeHandlers []*CloseHandler
|
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 {
|
func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
|
||||||
ch := CloseHandler{
|
ch := CloseHandler{
|
||||||
waiter: w,
|
w,
|
||||||
Quit: make(chan struct{}, 1),
|
make(chan bool, 1),
|
||||||
active: true,
|
true,
|
||||||
autoDone: autoDone,
|
autoDone,
|
||||||
wgDone: false,
|
f,
|
||||||
handlerFunc: f,
|
|
||||||
mu: &sync.Mutex{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
if !h.active {
|
|
||||||
if !h.wgDone {
|
|
||||||
w.waitGroup.Done()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
if h.handlerFunc != nil && *h.handlerFunc != nil {
|
||||||
(*h.handlerFunc)()
|
(*h.handlerFunc)()
|
||||||
}
|
}
|
||||||
|
h.Quit <- true
|
||||||
close(h.Quit)
|
|
||||||
if h.autoDone || forceWaitGroupDone {
|
if h.autoDone || forceWaitGroupDone {
|
||||||
w.waitGroup.Done()
|
w.waitGroup.Done()
|
||||||
h.wgDone = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h.active = false
|
h.active = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,17 +44,11 @@ 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.setHalting(true)
|
|
||||||
|
|
||||||
w.RLock()
|
|
||||||
for _, h := range w.closeHandlers {
|
for _, h := range w.closeHandlers {
|
||||||
w.terminateHandler(h, false)
|
if h.active {
|
||||||
|
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 {
|
||||||
@ -99,15 +61,12 @@ func (w *Waiter) Halt(err error) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.setHalting(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) Wait() {
|
func (w *Waiter) Wait() {
|
||||||
w.blockingMode = true
|
w.blockingMode = true
|
||||||
log.Info("Waiting...")
|
log.Info("Waiting...")
|
||||||
w.waitGroup.Wait()
|
w.waitGroup.Wait()
|
||||||
log.Info("Terminated")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Waiter) onSignal(sig os.Signal) {
|
func (w *Waiter) onSignal(sig os.Signal) {
|
||||||
@ -117,12 +76,9 @@ func (w *Waiter) onSignal(sig os.Signal) {
|
|||||||
|
|
||||||
func NewWaiter() *Waiter {
|
func NewWaiter() *Waiter {
|
||||||
w := Waiter{
|
w := Waiter{
|
||||||
sync.RWMutex{},
|
|
||||||
false,
|
false,
|
||||||
sync.WaitGroup{},
|
sync.WaitGroup{},
|
||||||
make([]*CloseHandler, 0),
|
make([]*CloseHandler, 0),
|
||||||
false,
|
|
||||||
sync.RWMutex{},
|
|
||||||
}
|
}
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user