cc

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 2 Imported by: 3

README

cc

A golang waitgroup with limits and error reporting

Usage:

import github.com/codeclysm/cc

p := cc.New(4)
p.Run(func() error {
	return errors.New("fail1")
})
p.Run(func() error {
	return errors.New("fail2")
})
p.Run(func() error {
	return nil
})

errs := p.Wait() // returns a list of errors [fail1, fail2]

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

Pool manages a pool of concurrent workers. It works a bit like a Waitgroup, but with error reporting and concurrency limits You create one with New, and run functions with Run. Then you wait on it like a regular WaitGroup.

Example
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/codeclysm/cc"
)

func main() {
	p := cc.New(4)
	p.Run(func() error {
		time.Sleep(1 * time.Second)
		return errors.New("fail1")
	})
	p.Run(func() error {
		return errors.New("fail2")
	})
	p.Run(func() error {
		return nil
	})

	errs := p.Wait()
	fmt.Println(errs)
}
Output:
2 errors:
:   fail2
:   fail1

func New

func New(concurrency int) *Pool

New returns a new pool where a limited number (concurrency) of goroutine can work at the same time

func (*Pool) Run

func (p *Pool) Run(fn func() error)

Run wraps the given function into a goroutine and ensure that the concurrency limits are respected. The error returned by the function is stored into the error list returned by Wait

func (*Pool) Wait

func (p *Pool) Wait() error

Wait blocks and ensures that the channels are closed when all the goroutines end. It returns a list of all the errors returned by the goroutine

type Stoppable added in v1.2.0

type Stoppable struct {
	Stopped chan struct{}
	// contains filtered or unexported fields
}

Stoppable is a function that can be stopped with the method Stop. You can also listen on the Stopped channel to see when it has been stopped. Stoppable is different from a context cancelation because it waits until the function has cleaned up before broadcasting on the Stopped channel

Example
package main

import (
	"fmt"
	"time"

	"github.com/codeclysm/cc"
)

func main() {
	stoppable := cc.Run(func(stop chan struct{}) {
		i := 0
	L:
		for {
			select {
			case <-stop:
				fmt.Println("receive stop signal")
				break L
			default:
				i++
				time.Sleep(250 * time.Millisecond)
				fmt.Println(i)
			}
		}
		fmt.Println("finished with", i)
	})

	go func() {
		time.Sleep(1 * time.Second)
		fmt.Println("send stop signal")
		stoppable.Stop()
		stoppable.Stop() // It shouldn't explode even if you attempt to close it multiple times
	}()

	<-stoppable.Stopped
	fmt.Println("stopped finally")
}
Output:
1
2
3
send stop signal
4
receive stop signal
finished with 4
stopped finally
Example (Multiple)
package main

import (
	"fmt"
	"time"

	"github.com/codeclysm/cc"
)

func main() {
	stoppable := cc.Run(func(stop chan struct{}) {
		i := 0
	L:
		for {
			select {
			case <-stop:
				fmt.Println("first received stop signal")
				break L
			default:
				i++
				time.Sleep(250 * time.Millisecond)
				fmt.Println("first: ", i)
			}
		}
		fmt.Println("first finished with", i)
	}, func(stop chan struct{}) {
		time.Sleep(50 * time.Millisecond)
		i := 0
	L:

		for {
			select {
			case <-stop:
				fmt.Println("second received stop signal")
				break L
			default:
				i++
				time.Sleep(250 * time.Millisecond)
				fmt.Println("second: ", i)
			}
		}
		fmt.Println("second finished with", i)
	})

	go func() {
		time.Sleep(1 * time.Second)
		fmt.Println("send stop signal")
		stoppable.Stop()
		stoppable.Stop() // It shouldn't explode even if you attempt to close it multiple times
	}()

	<-stoppable.Stopped
	fmt.Println("stopped finally")
}
Output:
first:  1
second:  1
first:  2
second:  2
first:  3
second:  3
send stop signal
first:  4
first received stop signal
first finished with 4
second:  4
second received stop signal
second finished with 4
stopped finally

func Run added in v1.2.0

func Run(fns ...StoppableFunc) (s *Stoppable)

Run creates a new stoppable function from the provided funcs. When you call the Stop method on the returned Stoppable the stop channel fed to the provided funcs is closed, signaling the need to stop. When all the provided func return the Stopped channel on the returned Stoppable is closed as well, broadcasting the message that it has finished

func (*Stoppable) Stop added in v1.2.0

func (s *Stoppable) Stop()

Stop signals the provided function that it needs to stop

type StoppableFunc added in v1.2.1

type StoppableFunc func(stop chan struct{})

StoppableFunc is a func that receives a stop channel that when closed broadcasts the need to wrap up and stop the function

Jump to

Keyboard shortcuts

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