alive

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 3 Imported by: 10

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/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

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) bool

Corresponds to `sync.WaitGroup.Add()` Returns `true` on successful increment or `false` after `Stop()`.

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