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 is a bounded goroutine manager. It ensures that goroutines spawned are within the given limit. The benefit being the ability to think and write go programs without worrying about the overhead of spawning too much goroutines.
Pool provides some simple synchronization and error capturing abilities too. Developers can wait for all goroutines in the pool to complete and exit with Wait(). The first error captured is returned.
Pool lazily spawns workers in the pool as tasks are queued up. Tasks are favored to be completed by an existing worker. If all workers are busy then it will spawn a new worker and enqueue the task again. This behaviour is ongoing until the size of the pool has reached it's limit.
Example (Parallel) ¶
Parallel illustrates the use of a Group for synchronizing a simple parallel task: the "Google Search 2.0" function from https://talks.golang.org/2012/concurrency.slide#46, augmented with a Context and error-handling.
package main import ( "context" "fmt" "os" "github.com/JackyChiu/bounded" ) var ( Web = fakeSearch("web") Image = fakeSearch("image") Video = fakeSearch("video") ) type Result string type Search func(ctx context.Context, query string) (Result, error) func fakeSearch(kind string) Search { return func(_ context.Context, query string) (Result, error) { return Result(fmt.Sprintf("%s result for %q", kind, query)), nil } } // Parallel illustrates the use of a Group for synchronizing a simple parallel // task: the "Google Search 2.0" function from // https://talks.golang.org/2012/concurrency.slide#46, augmented with a Context // and error-handling. func main() { Google := func(ctx context.Context, query string) ([]Result, error) { pool, ctx := bounded.NewPool(ctx, 3) searches := []Search{Web, Image, Video} results := make([]Result, len(searches)) for i, search := range searches { i, search := i, search // https://golang.org/doc/faq#closures_and_goroutines pool.Go(func() error { result, err := search(ctx, query) if err == nil { results[i] = result } return err }) } if err := pool.Wait(); err != nil { return nil, err } return results, nil } results, err := Google(context.Background(), "golang") if err != nil { fmt.Fprintln(os.Stderr, err) return } for _, result := range results { fmt.Println(result) } }
Output: web result for "golang" image result for "golang" video result for "golang"
func NewPool ¶
NewPool returns a Pool instances and a new context. The number of goroutines spawned are limited by the given max capacity. The new context includes cancellations from the goroutines in the Pool.
func (*Pool) Go ¶
Go will enqueue the task for execution by one of goroutines in the pool. Calls to Go will spin up workers lazily, as the workers are blocked, new workers will be spawned until the goroutine limit has been reached.