https://github.com/temoto/alive
Go library waiting for subtasks. sync.WaitGroup on steroids. Helps to coordinate graceful or fast shutdown.
https://github.com/temoto/alive
golang-library production-ready synchronization
Last synced: 5 months ago
JSON representation
Go library waiting for subtasks. sync.WaitGroup on steroids. Helps to coordinate graceful or fast shutdown.
- Host: GitHub
- URL: https://github.com/temoto/alive
- Owner: temoto
- License: mit
- Created: 2018-08-09T19:02:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-05T23:44:54.000Z (almost 6 years ago)
- Last Synced: 2025-03-31T20:06:07.191Z (6 months ago)
- Topics: golang-library, production-ready, synchronization
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# What
alive waits for subtasks, coordinate graceful or fast shutdown. sync.WaitGroup on steroids.
# Usage [](https://godoc.org/github.com/temoto/alive)
Key takeaways:
* `go get github.com/temoto/alive/v2`
* Zero value of `alive.Alive{}` is *not* usable ever, you *must* use `NewAlive()` constructor.
```
srv := MyServer{ alive: alive.NewAlive() }
```
* Call `.Add(n)` and `.Done()` just as with `WaitGroup` but check return value.
```
for {
task := <-queue
if !srv.alive.Add(1) {
break
}
go func() {
// be useful
srv.alive.Done()
}()
}
```
* Call `.Stop()` to switch `IsRunning` and stop creating new tasks if programmed so.
```
sigShutdownChan := make(chan os.Signal, 1)
signal.Notify(sigShutdownChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func(ch <-chan os.Signal) {
<-ch
log.Printf("graceful stop")
sdnotify("READY=0\nSTATUS=stopping\n")
srv.alive.Stop()
}(sigShutdownChan)
```
* Call `.Wait()` to synchronize on all subtasks `.Done()`, just as with `WaitGroup`.
```
func main() {
// ...
srv.alive.Wait()
}
```
* `.StopChan()` lets you observe `.Stop()` call from another place. A better option to `IsRunning()` poll.
```
stopch := srv.alive.StopChan()
for {
select {
case job := <-queue:
// be useful
case <-stopch:
// break for loop
}
}
```
* `.WaitChan()` is `select`-friendly version of `.Wait()`.
* There are few `panic()` which should never happen, like debug-build assertions. But please tell me if you find a way to trigger `"Bug in package"`# Flair
[](https://travis-ci.org/temoto/alive)
[](https://codecov.io/gh/temoto/alive)
[](https://goreportcard.com/report/github.com/temoto/alive)