The purpose of this library is to quickly allow applications to batch requests that are being run at a high throughput.
- Go installed on your system (version 1.11 or higher recommended for module support).
go get github.com/jattkaim/micro-batcher
-
Implement the Processor Interface: This library requires a
Processorinterface to be implemented by the user, responsible for processing jobs. Define this processor in your application before using the micro-batcher library:type MyAwesomeBatchProcessor struct { // Your batch processor fields here } func (p *MyAwesomeBatchProcessor) Process(jobs []batcher.Job) ([]batcher.JobResult, error) { // Implement your batch processing logic here } func (p *MyAwesomeBatchProcessor) Start() error { return nil // return nil if not needed } func (p *MyAwesomeBatchProcessor) Stop() error { return nil // return nil if not needed }
Note: The
Start()andStop()methods are optional. Implement them if your processing logic requires initialization or cleanup. -
Initialize the Batcher: Create an instance of the
Batcherby providing the implementedbatchProcessor, abatchSize, and aflushInterval:processor := &MyAwesomeBatchProcessor{} batcher := batcher.NewBatcher(processor, 10, 2 * time.Second)
-
Add Jobs to the Batcher:
job := batcher.Job{ ID: "unique_job_id", Payload: yourPayloadObject, // Replace with your actual payload } batcher.Add(job)
-
Reading the Results: Ensure to read the results in a non-blocking manner or after all jobs have been added:
for result := range batcher.Results { // Handle your job results here }
Note: Results will be sent back respectful to the
batchSizeorflushIntervalthat was defined.. Depending on your application's design, consider processingbatcher.Resultsin a separate goroutine to avoid blocking if you're adding jobs and reading results concurrently.
You can also find a complete usage example in this library.
MIT License