alive

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: MIT Imports: 3 Imported by: 0

README

What

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

Usage

Key takeaways:

  • Zero value of alive.Alive{} is not ready, 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().

Install

go get -u github.com/temoto/alive

Flair

Build status Coverage Go Report Card

Documentation

Overview

alive helps servers to coordinate graceful or fast stopping

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alive

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

func NewAlive

func NewAlive() *Alive

func (*Alive) Add

func (self *Alive) Add(delta int)

func (*Alive) Done

func (self *Alive) 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()

func (*Alive) StopChan

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

func (*Alive) String

func (self *Alive) String() string

func (*Alive) Wait

func (self *Alive) Wait()

func (*Alive) WaitChan

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

func (*Alive) WaitTasks

func (self *Alive) WaitTasks()

Jump to

Keyboard shortcuts

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