closer

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2018 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Closer

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

A Closer is an object, which can be used for cancelation of multiple go routines.

Example
var (
	wg sync.WaitGroup
	c  = New()
	n  = 0
)
wg.Add(5)

go func() {
	// Wait until other goroutine is counting
	wg.Wait()

	c.Close(fmt.Errorf("done"))
}()

go func() {
	for {
		// do some actions here
		select {
		case <-c.Chan():
			return
		case <-time.After(1):
		}
		if n < 5 {
			n++
			wg.Done()
		}
	}
}()

err := c.Wait()
fmt.Printf("received: %s, count: %d\n", err, n)
Output:

received: done, count: 5

func New

func New() *Closer

New creates new Closer.

func (*Closer) AddChild

func (c *Closer) AddChild(child *Closer)

AddChild add the child to this closer.

func (*Closer) Chan

func (c *Closer) Chan() <-chan bool

Chan returns a receive channel, which is closed when Close method is called.

func (*Closer) Child

func (c *Closer) Child() *Closer

Child creates new child closer. When this Closer is closer, it will close the child as well.

func (*Closer) Close

func (c *Closer) Close(err error)

Close closes the underlying channel, therefore all interested goroutines can be notified. Close is thread-safe and can be called multiple times, but only first error is kept and returned by Wait method.

func (*Closer) Wait

func (c *Closer) Wait() error

Wait block current goroutine until Close method is called. Wait returns error provided to first invocation of Close method.

Jump to

Keyboard shortcuts

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