gogroup

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: BSD-2-Clause Imports: 7 Imported by: 9

README

Go Reference

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.

Use to cancel all goroutines gracefully.

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

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

Example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/aletheia7/gogroup"
)

var gg = gogroup.New()

func main() {
	log.SetFlags(log.Lshortfile)
	go do_1()
	go do_2()
	defer log.Println("main done")
	// gg.Wait() will wait for all goroutines
	// <ctrl-c> will cancel all goroutines
	gg.Wait()
	log.Printf("main error: %v, interrupted: %v\n", gg.Get_err(), gg.Interrupted)
}

func do_1() {
	defer log.Println("do_1: done")
	key := gg.Register()
	defer gg.Unregister(key)
	gg.Set_err(fmt.Errorf("do_1 error"))
	child_gg := gogroup.New(gogroup.With_cancel(gg))
	// parent gg.Cancel() will call child_gg.Cancel() to cleanup hierarchy
	<-child_gg.Done()
	log.Println("do_1 child_gg.Cancel() called by parent")
	<-gg.Done()
}

func do_2() {
	defer log.Println("do_2: done")
	key := gg.Register()
	defer gg.Unregister(key)
	for ct, total := 1, 2; ct <= total; ct++ {
		select {
		case <-time.After(time.Second):
			log.Println("do_2: tick", ct, "of", total)
		case <-gg.Done():
			return
		}
	}
	// Set_err() was called earlier in do_1().
	// It will be ignored.
	gg.Set_err(fmt.Errorf("do_2 error"))
}

Output

t.go:43: do_2: tick 1 of 2
t.go:43: do_2: tick 2 of 2
t.go:51: do_2: done
t.go:32: do_1 child_gg.Cancel() called by parent
t.go:34: do_1: done
t.go:21: main error: do_1 error, interrupted: false
t.go:22: main done

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

This section is empty.

Variables

View Source
var Line_end = "\n"

Functions

func With_cancel

func With_cancel(parent *Group) option

Use With_cancel() 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.

parent goroutine will not wait on the child. parent.Cancel() will call child.Cancel(). child.Cancel() will not call parent.Cancel().

func With_cancel_nowait

func With_cancel_nowait(ctx context.Context) option

Use With_cancel_nowait() as the context to New(). If ctx is a *gogroup.Group, the parent will not Wait().

parent goroutine will not wait on the child. parent.Cancel() will call child.Cancel(). child.Cancel() will not call parent.Cancel().

func With_timeout

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

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

parent.Cancel() will call child.Cancel(). child.Cancel() will not call parent.Cancel(). child timeout will not cancel parent. child timeout will not cancel parent.

func With_timeout_nowait

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

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

parent goroutine will not wait on the child. parent.Cancel() will call child.Cancel(). child.Cancel() will not call parent.Cancel(). child timeout will not cancel parent.

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) (r *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) Get_err added in v0.2.0

func (o *Group) Get_err() error

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 must end upon receipt from the Group.Ctx.Done() channel.

func (*Group) Set_err

func (o *Group) Set_err(err error)

Set_err will return the first called

func (*Group) Unregister

func (o *Group) Unregister(index int)

Unregister decrements the internal sync.WaitGroup and calls 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.

Jump to

Keyboard shortcuts

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