Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ProcessorStopped = errors.New("run failed: processor is stopped")
Functions ¶
func GoroutineNumberForKey ¶
Types ¶
type Options ¶
type Options[Resource any] struct { // All batches will be run for at least MinDuration. // // By default, 100ms. MinDuration time.Duration // Batch will have timeout with MaxDuration. Context with this timeout will be passed to // LoadResource and SaveResource functions, which can abort the batch by returning an error. // // By default, 2*MinDuration. MaxDuration time.Duration // LoadResource loads resource with given key from a database. Returning an error aborts the batch. // This function is called in the beginning of each new batch. Context passed as a first parameter // has a timeout calculated using batch MaxDuration. You can use this information to abort loading resource // if it takes too long. // // By default, returns zero-value Resource. LoadResource func(_ context.Context, key string) (Resource, error) // SaveResource saves resource with given key to a database. Returning an error aborts the batch. // This function is called at the end of each batch. Context passed as a first parameter // has a timeout calculated using batch MaxDuration. You can use this information to abort saving resource // if it takes too long. // // By default, does nothing. SaveResource func(_ context.Context, key string, _ Resource) error // GoRoutines specifies how many goroutines should be used to run batch operations. // // By default, 16 * number of CPUs. GoRoutines int // GoRoutineNumberForKey returns go-routine number which will be used to run operation on // a given resource key. This function is crucial to properly serialize requests. // // This function must be deterministic - it should always return the same go-routine number // for given combination of key and goroutines parameters. // // By default, GoroutineNumberForKey function is used. This implementation calculates hash // on a given key and use modulo to calculate go-routine number. GoRoutineNumberForKey func(key string, goroutines int) int }
Options represent parameters for batch.Processor. They should be passed to StartProcessor function. All options (as the name suggest) are optional and have default values.
type Processor ¶
type Processor[Resource any] struct { // contains filtered or unexported fields }
Processor represents instance of batch processor which can be used to issue operations which run in a batch manner.
func StartProcessor ¶
StartProcessor starts batch processor which will run operations in batches.
Please note that Processor is a go-routine pool internally and should be stopped when no longer needed. Please use Processor.Stop method to stop it.
func (*Processor[Resource]) Run ¶
Run lets you run an operation on a resource with given key. Operation will run along other operations in batches. If there is no pending batch then the new batch will be started and will run for at least MinDuration. After the MinDuration no new operations will be accepted and SaveResource function will be called.
Operations are run sequentially. No manual locking is required inside operation. Operation should be fast, which basically means that any I/O should be avoided at all cost.
Run ends when the entire batch has ended. It returns error when batch is aborted or processor is stopped. Only LoadResource and SaveResource functions can abort the batch by returning an error. If error was reported for a batch all Run calls assigned to this batch will get this error.