Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Aggregator ¶
type Aggregator[T any] struct { // contains filtered or unexported fields }
Aggregator provides a mechanism to collect items into bulks of a specified size. A bulk is processed either when it's full or when a timeout occurs. It is safe for concurrent use.
Type Parameters:
T: The type of the items to be aggregated.
func NewAggregator ¶
func NewAggregator[T any](bulkSize int, timeout time.Duration) *Aggregator[T]
NewAggregator creates and returns a new Aggregator instance. It initializes the aggregator with the specified bulk size and timeout, and starts the timeout processing goroutine.
Type Parameters:
T: The type of the items to be aggregated.
Parameters:
bulkSize: The number of items to collect before processing a bulk. timeout: The duration to wait before processing a non-full bulk.
Returns:
A pointer to the newly created Aggregator.
func (*Aggregator[T]) Add ¶
func (agg *Aggregator[T]) Add(item T)
Add appends an item to the aggregator. If adding the item causes the bulk to reach its target size, the bulk is processed using the bulk callback.
Parameters:
item: The item to add to the aggregator.
func (*Aggregator[T]) Close ¶
func (agg *Aggregator[T]) Close()
Close gracefully shuts down the aggregator. It stops the timeout processing goroutine and purges any remaining items. It is safe to call Close multiple times.
func (*Aggregator[T]) Count ¶
func (agg *Aggregator[T]) Count() int
Count returns the current number of items in the aggregator.
Returns:
The number of items.
func (*Aggregator[T]) Purge ¶
func (agg *Aggregator[T]) Purge()
Purge removes all items from the aggregator without triggering any callbacks.
func (*Aggregator[T]) SetBulkCallback ¶
func (agg *Aggregator[T]) SetBulkCallback(callback bulkCallback[T])
SetBulkCallback sets the callback function to be executed when a bulk reaches its target size.
Parameters:
callback: The function to call with the full bulk.
func (*Aggregator[T]) SetTimeoutCallback ¶
func (agg *Aggregator[T]) SetTimeoutCallback(callback timeoutCallback[T])
SetTimeoutCallback sets the callback function to be executed when a timeout occurs.
Parameters:
callback: The function to call with the items that have timed out.