Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher struct {
// contains filtered or unexported fields
}
Batcher provides a mechanism to group individual jobs into batches for more efficient processing. It allows the configuration of batch size and the frequency of batch processing.
Fields: - config: Contains parameters for controlling the behavior of the batcher, such as batch size and processing interval. - jobs: A channel for receiving jobs to be batched and processed. - results: A mapping from job ID to a channel where the processed job result will be sent. - batchProcessor: An interface for the actual processing of batches. The implementation is provided by the user. - wg: A wait group to keep track of ongoing jobs and ensure graceful shutdown. - mu: A mutex to safeguard concurrent access to the results map. - resultChanPool: A pool of channels to optimise the allocation and reuse of result channels. - currentBatchNumber: A counter to keep track of the batches that have been processed. - shutdown: A channel used to signal the batcher to stop processing and shut down. - shutdownOnce: Ensures that the shutdown process is executed only once. - closed: An atomic value to indicate whether the batcher has been closed or not, aiding in thread-safe operations.
func NewBatcher ¶
func NewBatcher(bp types.BatchProcessor, config Config) *Batcher
NewBatcher initialises and returns a new Batcher instance. It sets up the necessary channels, maps, and configuration based on the provided types.BatchProcessor and Config.
func (*Batcher) Shutdown ¶
func (b *Batcher) Shutdown() <-chan struct{}
Shutdown gracefully shuts down the Batcher. It signals the internal components to stop accepting new jobs and waits for the processing of the current batch of jobs to complete. This method can be called multiple times, but it will only have an effect once.
func (*Batcher) Start ¶
func (b *Batcher) Start()
Initiates the processing loop of the Batcher. It collects jobs into batches and processes them either when: - The batch size reaches the configured limit or - The configured time interval elapses, whichever happens first.
type Config ¶
type Config struct {
BatchSize int // Maximum number of tasks in a single batch.
TimeInterval time.Duration // Duration to wait before processing a batch.
}
Config defines the configuration parameters for the Batcher. It allows users to specify the maximum number of tasks that can be grouped together in a batch (BatchSize) and the maximum duration to wait before processing a batch (TimeInterval).