tickgroup

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

README

tickgroup

A tickgroup is a collection of goroutines that are spawned to call a subtask every set interval.

Documentation

Full documentation is available on godoc.

Documentation

Overview

Package tickgroup allows a collection of goroutines to call a subtask every set time interval.

Example

This example uses the tickgroup package to create a simple 5 second timer.

ctx, cancel := context.WithCancel(context.Background())

// A tickgroup tg with a cancel context is initialized.
tg := New(ctx)

var i int
tg.Go(time.Second, func() error {
	if i > 4 {
		cancel()
		return nil
	}
	// i is incremeneted every 1 second.
	i++
	fmt.Println(i)
	return nil
})

// After 5 seconds the context is canceled and Wait() returns a nil value, which ceases the process.
if err := tg.Wait(); err != nil {
	fmt.Println(err)
}
Output:

1
2
3
4
5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

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

A Group is a collection of goroutines working on subtask that are spawned on a set interval per task.

func New

func New(ctx context.Context) *Group

New returns a new Group that stops spawning subtasks when ctx is done.

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext creates a child context from the given context, and uses that to control context cancelation.

func (*Group) Go

func (g *Group) Go(d time.Duration, f func() error)

Go spawns a subtask f every d. The goroutine terminates when an error is encountered. The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*Group) Wait

func (g *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