You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/executors/bulkexecutor.go

102 lines
2.1 KiB
Go

4 years ago
package executors
import "time"
4 years ago
const defaultBulkTasks = 1000
type (
// BulkOption defines the method to customize a BulkExecutor.
4 years ago
BulkOption func(options *bulkOptions)
// A BulkExecutor is an executor that can execute tasks on either requirement meets:
// 1. up to given size of tasks
// 2. flush interval time elapsed
4 years ago
BulkExecutor struct {
executor *PeriodicalExecutor
container *bulkContainer
}
bulkOptions struct {
cachedTasks int
flushInterval time.Duration
}
)
// NewBulkExecutor returns a BulkExecutor.
4 years ago
func NewBulkExecutor(execute Execute, opts ...BulkOption) *BulkExecutor {
options := newBulkOptions()
for _, opt := range opts {
opt(&options)
}
container := &bulkContainer{
execute: execute,
maxTasks: options.cachedTasks,
}
executor := &BulkExecutor{
executor: NewPeriodicalExecutor(options.flushInterval, container),
container: container,
}
return executor
}
// Add adds task into be.
func (be *BulkExecutor) Add(task any) error {
4 years ago
be.executor.Add(task)
return nil
}
// Flush forces be to flush and execute tasks.
4 years ago
func (be *BulkExecutor) Flush() {
be.executor.Flush()
}
// Wait waits be to done with the task execution.
4 years ago
func (be *BulkExecutor) Wait() {
be.executor.Wait()
}
// WithBulkTasks customizes a BulkExecutor with given tasks limit.
4 years ago
func WithBulkTasks(tasks int) BulkOption {
return func(options *bulkOptions) {
options.cachedTasks = tasks
}
}
// WithBulkInterval customizes a BulkExecutor with given flush interval.
4 years ago
func WithBulkInterval(duration time.Duration) BulkOption {
return func(options *bulkOptions) {
options.flushInterval = duration
}
}
func newBulkOptions() bulkOptions {
return bulkOptions{
cachedTasks: defaultBulkTasks,
flushInterval: defaultFlushInterval,
}
}
type bulkContainer struct {
tasks []any
4 years ago
execute Execute
maxTasks int
}
func (bc *bulkContainer) AddTask(task any) bool {
4 years ago
bc.tasks = append(bc.tasks, task)
return len(bc.tasks) >= bc.maxTasks
}
func (bc *bulkContainer) Execute(tasks any) {
vals := tasks.([]any)
4 years ago
bc.execute(vals)
}
func (bc *bulkContainer) RemoveAll() any {
4 years ago
tasks := bc.tasks
bc.tasks = nil
return tasks
}