batch

package
v0.0.0-...-97ebcb8 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: Apache-2.0, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package batch provides support for batching up singular calls into multiple-value calls which may be more efficient.

Eventually, this could potentially live in x/sync alongside the singleflight package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller[Value, Result any] struct {
	// contains filtered or unexported fields
}

Caller represents a batch caller. It accumulates multiple calls into a single "batch" call to reduce the total number of calls made. The zero value is equivalent to NewCaller(0, 0).

func NewCaller

func NewCaller[Value, Result any](maxConcurrency int, initialDelay time.Duration) *Caller[Value, Result]

NewCaller returns a Caller that issues a maximum of maxConcurrency concurrent calls, and delays for at least initialDelay after issuing a call to accumulate possible extra calls to avoid the first call being issued immediately.

If maxConcurrency is non-positive, 1 concurrent call will be allowed.

func (*Caller[V, R]) Do

func (g *Caller[V, R]) Do(v V, call func(vs ...V) ([]R, error)) (R, error)

Do does the equivalent of:

rs, err := call(v)
if err != nil {
	return _, err
}
return rs[0], nil

except that only a limited number of Do methods can be excecuting at a time. If the maximum count has been reached, additional Do calls will accumulate argument values into a slice and use the same call function, which should return a slice with the results in corresponding elements to the arguments.

func (*Caller[V, R]) DoChan

func (g *Caller[V, R]) DoChan(v V, call func(vs ...V) ([]R, error)) <-chan Result[R]

DoChan is like Do but returns a channel on which the result can be received instead of the result itself.

type Result

type Result[R any] struct {
	Val R
	Err error
}

Result represents the result of a call.

Jump to

Keyboard shortcuts

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