phase

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2018 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Phase

Phase provides features to enable systems to perform graceful ordered shutdown.

You can use Phase to help you build systems that shutdown in an ordered sequence, allowing you to drain connections, flush outstanding queue items and persist data before termination.

Use

Install the package with go get github.com/aelse/phase

A phase.Phaser is created from any context object, using phase.FromContext(ctx).

The returned Phaser can be used to create additional child Phasers by calling its Next() function, which can also be used to create new Phasers.

  • When a Phaser is cancelled its children will terminate in reverse order.
  • A phaser may be cancelled by calling Cancel() on it to trigger termination of itself and all downstream Phasers.
  • Every phaser must have Cancel() called on it once its context has terminated.
  • A Phaser may be passed to a function expecting a context.Context but ordering of shutdown is not guaranteed since that function has no way of signalling the parent when it has completed.

Here's an example implementing a solution to the problem scenario further below.

import (
	"context"
	"fmt"
	"time"

	"github.com/aelse/phase"
)

func main() {
	// Create a top lever phaser which will be cancelled at end of main.
	phaser := phase.FromContext(context.Background())

	p0 := phaser.Next()
	// Start the web server
	go component(p0, "web server")

	p1 := p0.Next()
	go component(p1, "data pipeline")

	p2 := p1.Next()
	go component(p2, "db")

	fmt.Println("Shutdown in 5 seconds")
	time.Sleep(5 * time.Second)
	phaser.Cancel()
	<-phaser.Done() // Wait until everything has finished.
	fmt.Println("Bye!")
}

// simulate any component that needs to perform cleanup at end of context.
func component(p *phase.Phaser, name string) {
	defer p.Cancel()
	fmt.Printf("%s started\n", name)
	<-p.Done()
	fmt.Printf("%s shutting down\n", name)
	time.Sleep(time.Second)
}

See the examples directory for working examples.

Background

Since contexts were released in go 1.7 they have become a staple of many projects. One common use of contexts is to manage graceful program termination. Unfortunately contexts on their own are not well suited to this as they do not provide very granular control over program execution.

Phase solves this problem by providing a Context that handles ordered cancellation. This allows control of shutdown sequence for Phaser-aware functions, and approximate control of shutdown sequence where functions are only context-aware.

Problem scenario

For example, if my application consists of a web server with an internal pipeline backed by a database my data flow may look something like:

[ Web API Request ] -> [ Transform Data ] -> [ Persist to DB ]

When I shutdown this system I don't want to lose any data submitted by users, so what I would like to happen is

  1. Stop the web server accepting requests
  2. Empty the queue of any outstanding payloads to transform
  3. Complete reading the queued, transformed data and persist to DB
  4. Terminate program

When you cancel a context that immediately progagates downstream to any contexts created from it. This means you cannot simply cancel a single context at top level because then you can not choose the order in which your goroutines will terminate.

To have ordered shutdown you have to deal with dependent contexts yourself. This makes your code busy, prone to error and difficult to troubleshoot.

Alternatives

Here are similar projects I'm aware of.

License

This project is released under the MIT License. Please see the License file in this repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Phaser

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

func FromContext

func FromContext(ctx context.Context) *Phaser

func (*Phaser) Cancel

func (p *Phaser) Cancel()

Cancel triggers cancellation of the Phaser chain. This must be called when Phaser context has finished (context semantics, the Done() channel),, and may be called to trigger cancellation of downstream phasers.

func (*Phaser) Deadline added in v0.0.2

func (p *Phaser) Deadline() (deadline time.Time, ok bool)

func (*Phaser) Done added in v0.0.2

func (p *Phaser) Done() <-chan struct{}

func (*Phaser) Err added in v0.0.2

func (p *Phaser) Err() error

func (*Phaser) Next

func (p *Phaser) Next() *Phaser

Next registers and returns a new child Phaser. This should be called to create a new Phaser for each downstream component that needs ordered shutdown.

func (*Phaser) Value added in v0.0.2

func (p *Phaser) Value(key interface{}) interface{}

Directories

Path Synopsis
examples
contexts command
order command
simple command

Jump to

Keyboard shortcuts

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