batch

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 1 Imported by: 0

README

Go Batch

Build

A simple batching library in Golang.

Guid

Installation
go get github.com/RashadAnsari/go-batch
Example
package main

import (
	"log"
	"math/rand"
	"time"

	goBatch "github.com/RashadAnsari/go-batch"
)

func main() {
	batch := goBatch.New(
		goBatch.WithSize(10),
		goBatch.WithMaxWait(1*time.Second),
	)

	go func() {
		for {
			output := <-batch.Output

			log.Printf("output: %v, size: %d\n", output, len(output))
		}
	}()

	for i := 1; i <= 100; i++ {
		batch.Input <- i
	}

	time.Sleep(1 * time.Second)

	for i := 1; i <= 100; i++ {
		batch.Input <- i

		if rand.Intn(2) == 0 {
			time.Sleep(300 * time.Millisecond)
		}
	}

	batch.Close()
}

Documentation

Index

Constants

View Source
const (
	// DefaultBatchSize represents the default value for the size of each batch.
	DefaultBatchSize = 10
	// DefaultMaxWait represents the default value of the maximum waiting time for filling each batch.
	DefaultMaxWait = 1 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {

	// Input represents the batch input channel.
	Input chan<- interface{}
	// Output represents the batch output channel.
	Output <-chan []interface{}
	// contains filtered or unexported fields
}

Batch represents the go-batch struct.

func New added in v1.1.0

func New(optionFuncs ...OptionFunc) *Batch

New creates a new go-batch instance.

func (*Batch) Close

func (b *Batch) Close()

Close drops all the batch events into the output channel and does not listen to the new events again.

type OptionFunc added in v1.1.0

type OptionFunc func(*Options)

OptionFunc defines type for Options setter function.

func WithMaxWait

func WithMaxWait(maxWait time.Duration) OptionFunc

WithMaxWait sets the maximum waiting time for filling each batch.

func WithSize added in v1.1.0

func WithSize(size int) OptionFunc

WithSize sets the maximum number of items that can be in each batch.

type Options added in v1.1.0

type Options struct {
	// Size specifies the maximum number of items that can be in a batch.
	//
	// Default: 10.
	Size int
	// MaxWait specifies the maximum waiting time for filling a batch.
	//
	// Default: 1 * time.Second.
	MaxWait time.Duration
}

Options sets configuration options for the go-batch instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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