Skip to content
/ alive Public

Go library waiting for subtasks. sync.WaitGroup on steroids. Helps to coordinate graceful or fast shutdown.

License

Notifications You must be signed in to change notification settings

temoto/alive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What

alive waits for subtasks, coordinate graceful or fast shutdown. sync.WaitGroup on steroids.

Usage GoDoc

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

Build status Coverage Go Report Card

About

Go library waiting for subtasks. sync.WaitGroup on steroids. Helps to coordinate graceful or fast shutdown.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published