autobundler

package
v0.0.0-...-abd3a35 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package autobundler provides an automatic bundler for a stream of values.

Unlike `google.golang.org/api/support/bundler`, package autobundler does not require setting timeouts or thresholds.

Instead, Autobundler tries to minimize the latency between when a value arrives and when it is handled. At steady state, autobundler will buffer incoming values while an invocation of the handler is running, starting a new invocation with the next bundle of values as soon as the prior invocation finishes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AutoBundler

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

AutoBundler manages

func New

func New(ctx context.Context, itemExample interface{}, handler func(ctx context.Context, v interface{}), max int) *AutoBundler

New returns a new AutoBundler.

The itemExample argument is used to set the type of values the AutoBundler will handle.

Handler is called and passed a bundle of values to be handled. Only one instance of handler will be running at a time. Values are passed to handler in the order they were added to the AutoBundler.

If itemExample is of type T, then handler will be passed an argument of type []T. For example, if you want to create bundles of type `*Entry` then you can pass `&Entry{}` as itemExample and handler will then be passed an argument of type `[]*Entry`.

The AutoBundler will only buffer max values, after which calls to Add will block until the bundle is submitted to the handler. Call AddNoWait if you want to detect if the buffer is full. Setting a reasonable max provides a way to apply backpressure to upstream producers. For handlers that are not CPU constrained (e.g. which write bundles to remote storage), a max of 1000 is reasonable.

The context ctx will be passed to the handler. If ctx is cancelled, no future handler invocations will occur and the autobundler will stop handling buffered and new values. There is no way to safely "flush" an autobundler. Applications should be resilient to losing values buffered in the autobundler if the context is cancelled. In other words, context cancellation is treated like termination of the program from the point of view of the autobundler.

func (*AutoBundler) Add

func (a *AutoBundler) Add(ctx context.Context, item interface{})

Add adds item to the current bundler.

It is safe to call Add from multiple goroutines.

func (*AutoBundler) AddNoWait

func (a *AutoBundler) AddNoWait(item interface{}) bool

AddNoWait tries to add an item to the current bundler. If successful, it returns true, otherwise it returns false. AddNoWait does not block.

It is safe to call AddNoWait from multiple goroutines.

func (*AutoBundler) Wait

func (a *AutoBundler) Wait()

Wait blocks until the autobundler has stopped. The autobundler will stop when the context passed to New is cancelled.

Jump to

Keyboard shortcuts

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