gogroup

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2020 License: BSD-2-Clause Imports: 7 Imported by: 0

README

Documentation

gogroup allows running a group of goroutines. The gogroup.Group waits for all goroutines to end. All goroutines in the group are signaled through a context to end gracefully when one goroutine ends.

Use Group.Cancel() to cancel all gorountines gracefully.

Group.Interrupted indicates if an os.Signal was received.

Group.Err() indicates if an error was set by a goroutine.

Example

package main

import (
	"fmt"
	"github.com/aletheia7/gogroup"
	"log"
	"time"
)

func main() {
	log.SetFlags(log.Lshortfile)
	// ctrl-c to end gracefully or
	// let Object.Run count to 8 for a graceful error
	g := gogroup.New(gogroup.Add_signals(gogroup.Unix))
	// Register()/Unregister() example
	go do(g)
	// Grouper Interface
	g.Go(&Object{})
	defer log.Println("wait done:", g.Wait())
	<-g.Context.Done()
}

func do(g *gogroup.Group) {
	defer log.Println("do done")
	key := g.Register()
	defer g.Unregister(key)
	for {
		select {
		case <-g.Done():
			return
		case <-time.After(time.Second):
			log.Println("do")
		}
	}
}

type Object struct{}

func (o *Object) Run(g *gogroup.Group) {
	defer log.Println("Object.Run done")
	ct := 0
	total := 8
	for {
		select {
		case <-g.Done():
			return
		case t := <-time.After(time.Second):
			ct++
			log.Println("run:", ct, "of", total, t.Format("3:04:05 pm"))
			if ct == total {
				g.Set_err(fmt.Errorf("some err"))
				return
			}
		}
	}
}

License

Use of this source code is governed by a BSD-2-Clause license that can be found in the LICENSE file.

BSD-2-Clause License

Documentation

Index

Constants

View Source
const (
	None      Line_end = ``
	Unix               = "\n"
	Osx                = "\r"
	Windows            = "\r\n"
	Backspace          = "\b\b"
)

Variables

This section is empty.

Functions

func Add_signals

func Add_signals(end Line_end, sig ...os.Signal) option

Add_signals will call Group.Cancel() when a signal is received. Interrupted will be set to true. An "end" character will be output to os.Stderr upon receiving a signal. if sig is absent, os.Interrupt and syscall.SIGTERM will be used.

func With_cancel

func With_cancel(parent *Group) option

Use WithCancel() as the context to New(). Will panic if context is already set. Will panic if parent is nil. Register/Unregister use the parent's WaitGroup.

func With_cancel_nowait

func With_cancel_nowait(ctx context.Context) option

Use WithCancel() as the context to New(). If ctx is a *gogroup.Group, the parent will not Wait(). Use With_cancel_wait() to have a parent gogroup.Group wait on a child gogroup.Group.

func With_timeout

func With_timeout(parent *Group, timeout time.Duration) option

Will panic if gg is nil or context is already set.

func With_timeout_nowait

func With_timeout_nowait(ctx context.Context, timeout time.Duration) option

Use WithTimeout() as the context to New().

Types

type Group

type Group struct {
	context.Context
	context.CancelFunc
	Interrupted bool
	// contains filtered or unexported fields
}

A Group is a collection of goroutines working on subtasks that are part of the same overall task.

func New

func New(opt ...option) *Group

New returns a Group using with zero or more options. If a context is not provided in an option, With_cancel() will be used. The Group.Context is canceled when either a Go() func returns or a func using Register()/Unregister(). New must be called to make a Group.

func (*Group) Cancel

func (o *Group) Cancel()

func (*Group) Go

func (o *Group) Go(f Grouper)

Go calls the given function in a new goroutine. The first call to return cancels the group. A Grouper should receive on Group.Ctx.Done() to gracefully end.

func (*Group) Register

func (o *Group) Register() int

Register increments the internal sync.WaitGroup. Unregister() must be called with the returned int to end Group.Wait(). goroutines using Register/Unregister should end upon receipt from the Group.Ctx.Done() channel.

func (*Group) Set_err

func (o *Group) Set_err(err error)

Set_err will set the returned error for the first time called.

func (*Group) Unregister

func (o *Group) Unregister(index int)

Unregister decrements the internal sync.WaitGroup and called Group.Cancel(). It is safe to call Unregister multiple times.

func (*Group) Wait

func (o *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type Grouper

type Grouper interface {
	Run(g *Group)
}

type Line_end

type Line_end string

Jump to

Keyboard shortcuts

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