deps

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 4 Imported by: 1 module, 1 package

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

deps

Go Reference coverage

Manage lifecycle of the application's dependencies and shutdown gracefully, with minimalistic API.

How to use

Create application entrypoint using deps.New() and describe dependencies with Dependent().

func main() {
	root := deps.New()

	// Worker #1 (Web server)
	go func(dep *deps.Dependency) {
		var (
			svr http.Server
			err error
			e   = make(chan error, 1)
		)
		defer dep.Stop(&err) // request abort if err!=nil

		go func() {
			e <- svr.ListenAndServe()
		}()
		select {
		case err = <-e:
			log.Println("server stopped unexpectedly: ", err)
		case <-dep.Aborted():
			log.Println("start shutdown server")
			// do not pass shutdown error to dep.Stop()
			if shutdownErr := svr.Shutdown(dep.AbortContext()); shutdownErr != nil { // timeout=1m
				log.Println("failed to shutdown server gracefully: ", shutdownErr)
			}
		}
	}(root.Dependent())

	// Worker #2 (Periodic task runner or something)
	go func(dep *deps.Dependency) {
		defer dep.Stop(nil)

		// Start worker and describe shutdown flow as same as Worker #1...
	}(root.Dependent())

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt)

	select {
	case <-root.AbortRequested():
		log.Println("abort: server error")
	case s := <-sig:
		log.Printf("abort: signal received (%s)", s.String())
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	err := root.Abort(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package deps helps managing lifecycle of the application's dependencies and shutting down gracefully, with minimalistic API.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dependency

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

Dependency is a controller of the worker depends on the parent. After receiving abort signal from the parent, wait its dependent's stop and notify the parent of its Stop.

func (*Dependency) AbortContext

func (d *Dependency) AbortContext() context.Context

AbortContext returns a context given to (*Root).Abort. The worker on behalf of this controller can get the deadline of shutdown from the context, if specified.

func (*Dependency) Aborted

func (d *Dependency) Aborted() <-chan struct{}

Aborted returns a channel that's closed when its Root aborted. After the close of Aborted channel, the worker on behalf of this controller will have to start shutdown process including its dependents.

func (*Dependency) Dependent

func (d *Dependency) Dependent() *Dependency

Dependent creates the controller depends on this controller. Dependency should be created before the statement creating the goroutine or other event to be waited for. Otherwise, a data race could occur. Dependency uses sync.WaitGroup internally. For detail, see sync.WaitGroup.Add.

func (*Dependency) Stop

func (d *Dependency) Stop(abortOnError *error)

Stop marks the worker on behalf of this controller stopped after all dependents stopped. If abortOnError indicates error, this requests Root to abort.

func (*Dependency) StopImmediately added in v0.0.3

func (d *Dependency) StopImmediately(abortOnError *error)

StopImmediately marks the worker on behalf of this controller stopped, even if its any dependents still working. If abortOnError indicates error, this requests Root to abort.

func (*Dependency) Wait

func (d *Dependency) Wait() <-chan struct{}

Wait returns a channel that's closed when its all dependents stopped. To shutdown gracefully, the worker on behalf of this controller have to wait the stop of its children before starting its shutdown process.

type Root

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

Root is a root controller and describe its dependents using (*Root).Dependent. Root can send signal of shutdown to all its dependents.

func New

func New() *Root

New creates Root controller.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/daichitakahashi/deps"
)

func main() {
	root := deps.New()

	go func(dep *deps.Dependency) {
		defer dep.Stop(nil)
		for {
			select {
			case <-dep.Aborted():
				return
			default:
				for i := 0; i < 3; i++ {
					time.Sleep(time.Second)
					fmt.Printf("...%d", i)
				}
				fmt.Println()
			}
		}
	}(root.Dependent())

	time.Sleep(time.Second * 4)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	err := root.Abort(ctx)
	if err != nil {
		log.Fatal(err)
	}

}
Output:
...0...1...2
...0...1...2

func (*Root) Abort

func (r *Root) Abort(ctx context.Context) error

Abort fires shutdown of the application. When all dependents stopped successfully, it returns nil. The context given as argument can be accessed via (Dependency).AbortContext.

func (*Root) AbortRequested added in v0.0.3

func (r *Root) AbortRequested() <-chan struct{}

func (*Root) Aborted

func (r *Root) Aborted() <-chan struct{}

func (*Root) Dependent

func (r *Root) Dependent() *Dependency

Dependent creates the controller depends on this root. Dependency should be created before the statement creating the goroutine or other event to be waited for. Otherwise, a data race could occur. Root uses sync.WaitGroup internally. For detail, see sync.WaitGroup.Add.

Jump to

Keyboard shortcuts

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