syncutil

package
v0.0.0-...-6668049 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go[T any](ctx context.Context, limiter *semaphore.Weighted, fn GoFunc[T], items ...T) error

Go concurrently invokes fn on items.

Types

type GoFunc

type GoFunc[T any] func(ctx context.Context, region *LimitedRegion, t T) error

GoFunc represents a function that can be invoked by Go.

type LimitedGroup

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

A LimitedGroup is a collection of goroutines working on subtasks that are part of the same overall task.

func LimitGroup

func LimitGroup(ctx context.Context, limit int) (*LimitedGroup, context.Context)

LimitGroup returns a new LimitedGroup and an associated Context derived from ctx.

The number of active goroutines in this group is limited to the given limit. A negative value indicates no limit.

The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.

func (*LimitedGroup) Go

func (g *LimitedGroup) Go(f func() error)

Go calls the given function in a new goroutine. It blocks until the new goroutine can be added without the number of active goroutines in the group exceeding the configured limit.

The first call to return a non-nil error cancels the group's context. After which, any subsequent calls to Go will not execute their given function. The error will be returned by Wait.

func (*LimitedGroup) Wait

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

type LimitedRegion

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

LimitedRegion provides a way to bound concurrent access to a code block.

func LimitRegion

func LimitRegion(ctx context.Context, limiter *semaphore.Weighted) *LimitedRegion

LimitRegion creates a new LimitedRegion.

func (*LimitedRegion) End

func (lr *LimitedRegion) End()

End ends the region with concurrency limit.

func (*LimitedRegion) Start

func (lr *LimitedRegion) Start() error

Start starts the region with concurrency limit.

type Merge

type Merge[T any] struct {
	// contains filtered or unexported fields
}

Merge represents merge operations on items. The state transfer is shown as below:

           +----------+
           |  Start   +--------+-------------+
           +----+-----+        |             |
                |              |             |
                v              v             v
           +----+-----+   +----+----+   +----+----+
   +-------+ Prepare  +<--+ Pending +-->+ Waiting |
   |       +----+-----+   +---------+   +----+----+
   |            |                            |
   |            v                            |
   |       + ---+---- +                      |
On Error   | Resolve  |                      |
   |       + ---+---- +                      |
   |            |                            |
   |            v                            |
   |       +----+-----+                      |
   +------>+ Complete +<---------------------+
           +----+-----+
                |
                v
           +----+-----+
           |   End    |
           +----------+

func (*Merge[T]) Do

func (m *Merge[T]) Do(item T, prepare func() error, resolve func(items []T) error) error

Do merges concurrent operations of items into a single call of prepare and resolve. If Do is called multiple times concurrently, only one of the calls will be selected to invoke prepare and resolve.

type Once

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

Once is an object that will perform exactly one action. Unlike sync.Once, this Once allows the action to have return values.

func NewOnce

func NewOnce() *Once

NewOnce creates a new Once instance.

func (*Once) Do

func (o *Once) Do(ctx context.Context, f func() (interface{}, error)) (bool, interface{}, error)

Do calls the function f if and only if Do is being called first time or all previous function calls are cancelled, deadline exceeded, or panicking. When `once.Do(ctx, f)` is called multiple times, the return value of the first call of the function f is stored, and is directly returned for other calls. Besides the return value of the function f, including the error, Do returns true if the function f passed is called first and is not cancelled, deadline exceeded, or panicking. Otherwise, returns false.

type OnceOrRetry

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

OnceOrRetry is an object that will perform exactly one success action.

func (*OnceOrRetry) Do

func (o *OnceOrRetry) Do(f func() error) error

OnceOrRetry calls the function f if and only if Do is being called for the first time for this instance of Once or all previous calls to Do are failed.

type Pool

type Pool[T any] struct {
	// New optionally specifies a function to generate a value when Get would
	// otherwise return nil.
	// It may not be changed concurrently with calls to Get.
	New func() T
	// contains filtered or unexported fields
}

Pool is a scalable pool with items identified by keys.

func (*Pool[T]) Get

func (p *Pool[T]) Get(key any) (*T, func())

Get gets the value identified by key. The caller should invoke the returned function after using the returned item.

Jump to

Keyboard shortcuts

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