async

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

The package async defines asynchronous adapters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[V any](timeout time.Duration, n int, seq iter.Seq[V]) iter.Seq[[]V]

Chunk returns an iterator over consecutive values of up to n elements from seq.

The returned iterator reuses the buffer it yields. Apply mapper.Clone if the caller needs to retain slices.

Chunk may yield slices where 0 < len(s) <= n. Values may be shorter than n if timeout > 0 and the timeout duration passed since last time seq generated a value.

Chunk panics if n is less than 1.

func Map

func Map[V1, V2 any](
	ctx context.Context,
	queueLimit int,
	workerLimit int,
	mapper func(context.Context, V1) (V2, error),
	seq iter.Seq[V1],
) iter.Seq2[V2, error]

Map maps values from seq asynchronously.

Map applies mapper to values from seq in separate goroutines while keeping output order. When the order does not need to be kept, just send all values to workers through a channel using hiter.ChanSend and receive results via other channel using hiter.Chan.

queueLimit limits max amounts of possible simultaneous resource allocated. queueLimit must not be less than 1, otherwise Map panics. workerLimit limits max possible concurrent invocation of mapper. workerLimit is ok to be less than 1. When queueLimit > workerLimit, the total number of workers is only limited by queueLimit.

Cancelling ctx may stop the returned iterator early. mapper should respect the ctx, otherwise it delays the iterator to return.

If mapper panics Map panics with the first panic value.

The counter part of Map for iter.Seq2[K, V] does not exist since mapper is allowed to return error as second ret value. If you need to map iter.Seq2[K, V], convert it into iter.Seq[V] by hiter.ToKeyValue.

Types

This section is empty.

Jump to

Keyboard shortcuts

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