runner

package
v0.0.0-...-184bb4f Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 2 Imported by: 0

README

What Is It ?

An utility package to allow run groups : one group fails, all peers are shutdown.

Used in gracefully shutdown multiple components that should not know each other.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Erroer

type Erroer func(error)

type Group

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

func (*Group) Add

func (g *Group) Add(execute Runner, interrupt Erroer)
Example (Basic)
package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/badu/wg-cc/pkg/runner"
)

func main() {
	var g runner.Group
	{
		cancel := make(chan struct{})
		g.Add(func() error {
			select {
			case <-time.After(time.Second):
				fmt.Printf("The first funcPair had its time elapsed\n")
				return nil
			case <-cancel:
				fmt.Printf("The first funcPair was canceled\n")
				return nil
			}
		}, func(err error) {
			fmt.Printf("The first funcPair was interrupted with: %v\n", err)
			close(cancel)
		})
	}
	{
		g.Add(func() error {
			fmt.Printf("The second funcPair is returning immediately\n")
			return errors.New("immediate teardown")
		}, func(err error) {
			// Note that this interrupt function is called, even though the
			// corresponding execute function has already returned.
			fmt.Printf("The second funcPair was interrupted with: %v\n", err)
		})
	}
	ctx, cancel := context.WithCancel(context.Background())
	fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel))
}
Output:

The second funcPair is returning immediately
The first funcPair was interrupted with: immediate teardown
The second funcPair was interrupted with: immediate teardown
The first funcPair was canceled
The group was terminated with: immediate teardown
Example (Context)
package main

import (
	"context"
	"fmt"

	"github.com/badu/wg-cc/pkg/runner"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	var g runner.Group
	{
		ctx, cancel := context.WithCancel(ctx) // note: shadowed
		g.Add(func() error {
			return runUntilCanceled(ctx)
		}, func(error) {
			cancel()
		})
	}
	go cancel()
	fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel))
}

func runUntilCanceled(ctx context.Context) error {
	<-ctx.Done()
	return ctx.Err()
}
Output:

The group was terminated with: context done
Example (Listener)
package main

import (
	"context"
	"errors"
	"fmt"
	"net"
	"net/http"

	"github.com/badu/wg-cc/pkg/runner"
)

func main() {
	var g runner.Group
	{
		ln, _ := net.Listen("tcp", ":0")
		g.Add(func() error {
			defer fmt.Printf("http.Serve returned\n")
			return http.Serve(ln, http.NewServeMux())
		}, func(error) {
			ln.Close()
		})
	}
	{
		g.Add(func() error {
			return errors.New("immediate teardown")
		}, func(error) {
			//
		})
	}

	ctx, cancel := context.WithCancel(context.Background())
	fmt.Printf("The group was terminated with: %v\n", g.Wait(ctx, cancel))
}
Output:

http.Serve returned
The group was terminated with: immediate teardown

func (*Group) Wait

func (g *Group) Wait(ctx context.Context, cancel context.CancelFunc) error

type Runner

type Runner func() error

Jump to

Keyboard shortcuts

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