alive

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2019 License: MIT Imports: 3 Imported by: 0

README

What

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

Usage GoDoc

Key takeaways:

  • go get github.com/temoto/alive
  • 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, monitor .IsRunning().
    for srv.alive.IsRunning() {
        task := <-queue
        srv.alive.Add(1)
        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 your 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().
  • Incorrect API usage .Stop() ; .Add() will panic. Please tell me how to remove it while staying interface compatible with WaitGroup.
  • There are few more 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

Documentation

Overview

alive helps servers to coordinate graceful or fast stopping

Index

Constants

View Source
const NotRunning = "Alive.Add(): need state Running. Attempted to run new task after Stop()"

Variables

This section is empty.

Functions

This section is empty.

Types

type Alive

type Alive struct {
	// contains filtered or unexported fields
}

Alive waits for subtasks, coordinate graceful or fast shutdown. Exported for easy/fast direct references in your code. Zero `Alive{}` is not usable ever. You *must* call `NewAlive()`.

func NewAlive

func NewAlive() *Alive

func (*Alive) Add

func (self *Alive) Add(delta int)

Corresponds to `sync.WaitGroup.Add()` `a.Stop() ; a.Add(_)` will `panic(NotRunning)`

func (*Alive) Done

func (self *Alive) Done()

Corresponds to `sync.WaitGroup.Done()`

func (*Alive) IsFinished

func (self *Alive) IsFinished() bool

func (*Alive) IsRunning

func (self *Alive) IsRunning() bool

func (*Alive) IsStopping

func (self *Alive) IsStopping() bool

func (*Alive) Stop

func (self *Alive) Stop()

Stop puts Alive into Stopping mode, closes `StopChan()` and returns immediately. After all pending tasks `Done()` state changes to Finished and unblocks all `Wait*` calls/channel. Multiple and concurrent calls are allowed and produce same result.

func (*Alive) StopChan

func (self *Alive) StopChan() <-chan struct{}

StopChan is closed when `Stop()` is called.

func (*Alive) String

func (self *Alive) String() string

func (*Alive) Wait

func (self *Alive) Wait()

Wait returns after both `Stop()` is called **and** all pending tasks done. Multiple and concurrent `Wait()`/`<-WaitChan()` are allowed and produce same result.

func (*Alive) WaitChan

func (self *Alive) WaitChan() <-chan struct{}

WaitChan is closed when both `Stop()` is called **and** all pending tasks done. Multiple and concurrent `Wait()`/`<-WaitChan()` are allowed and produce same result.

func (*Alive) WaitTasks

func (self *Alive) WaitTasks()

Corresponds to `sync.WaitGroup.Wait()` Multiple and concurrent calls allowed and produce same result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL