This Go package provides a flexible and configurable batch processing framework for concurrent execution of tasks in a controlled manner. It allows for efficient handling of batches of items using a supplier, processor, and finalizer.
To use the batch processor, follow these steps:
-
Create a Supplier:
- Implement the
Supplier
interface to fetch the next batch of items.
- Implement the
-
Create a Processor:
- Implement the
Processor
interface to define how to process a batch of items.
- Implement the
-
Create a Finalizer (Optional):
- Implement the
Finalizer
interface if you need to perform actions after each batch is processed.
- Implement the
-
Instantiate the Batch Processor:
processor := processor.NewBatchProcessorImpl(maxWorkersCount, yourSupplier, yourProcessor). WithFinalizer(yourFinalizer). WithNoBatchSleepIntervalInMilliseconds(sleepIntervalInMillis). WithProcessorTimeout(processorTimeoutInMillis)
-
Start the Batch Processor:
processor.Start()
-
Stop the Batch Processor:
processor.Stop()
Start()
: Initiates the batch processing loop.Stop()
: Stops the batch processor, waiting for all processing to finish.
FetchNextBatch() ([]*T, error)
: Fetches the next batch of items for processing.
ProcessBatch(ctx context.Context, batch []*T) ([]*T, error)
: Processes a batch of items with optional context and returns the processed items or an error.
OnBatchProcessed(processedBatch []*T, err error)
: Performs actions after process
Mocks were generated using https://vektra.github.io/mockery/latest/