pool

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockedReader

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

LockedReader wraps an io.Reader to be safe for concurrent reads.

This type implements io.Reader, returning the same output.

This means acquiring a lock whenever a read happens, so be aware of that for performance or concurrency reasons.

func NewLockedReader

func NewLockedReader(r io.Reader) *LockedReader

NewLockedReader creates a LockedReader by wrapping an underlying value.

func (*LockedReader) Read

func (r *LockedReader) Read(p []byte) (int, error)

Read implements io.Reader for LockedReader

The behavior is to return the same output as the underlying reader. The difference is that it's safe to call this function concurrently.

Naturally, when calling this function concurrently, what value ends up getting read is raced, but you won't end up reading the same value twice, or otherwise messing up the state of the reader.

type Pool

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

Pool represents a pool of workers, used for parallelizing functions.

Functions needing a *Pool will work with a nil receiver, doing the equivalent work on the current thread instead.

By creating a pool, you avoid the overhead of spinning up goroutines for each new operation.

A Pool is only ever intended to be used from a single goroutine, and might cause deadlocks if used by multiple goroutines concurrently.

func NewPool

func NewPool(count int) *Pool

NewPool creates a new pool, with a certain number of workers.

If count ⩽ 0, this will use the number of available CPUs instead.

func (*Pool) Parallelize

func (p *Pool) Parallelize(count int, f func(int) interface{}) []interface{}

Parallelize calls a function count times, passing in indices from 0..count-1.

The result will be a slice containing [f(0), f(1), ..., f(count - 1)].

func (*Pool) Search

func (p *Pool) Search(count int, f func() interface{}) []interface{}

Search queries the function f, until count successes are found.

f is supposed to try a single candidate, returning nil if that candidate isn't successful.

The result will be an array containing the first count successes.

func (*Pool) TearDown

func (p *Pool) TearDown()

TearDown cleanly tears down a pool, closing channels, etc.

Jump to

Keyboard shortcuts

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