executors

package
v0.0.0-...-018885d Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BulkExecutor

type BulkExecutor struct {
	// contains filtered or unexported fields
}

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

func NewBulkExecutor

func NewBulkExecutor(execute Execute, opts ...BulkOption) *BulkExecutor

NewBulkExecutor returns a BulkExecutor.

func (*BulkExecutor) Add

func (be *BulkExecutor) Add(task any) error

Add adds task into be.

func (*BulkExecutor) Flush

func (be *BulkExecutor) Flush()

Flush forces be to flush and execute tasks.

func (*BulkExecutor) Wait

func (be *BulkExecutor) Wait()

Wait waits be to done with the task execution.

type BulkOption

type BulkOption func(options *bulkOptions)

BulkOption defines the method to customize a BulkExecutor.

func WithBulkInterval

func WithBulkInterval(duration time.Duration) BulkOption

WithBulkInterval customizes a BulkExecutor with given flush interval.

func WithBulkTasks

func WithBulkTasks(tasks int) BulkOption

WithBulkTasks customizes a BulkExecutor with given tasks limit.

type ChunkExecutor

type ChunkExecutor struct {
	// contains filtered or unexported fields
}

A ChunkExecutor is an executor to execute tasks when either requirement meets: 1. up to given chunk size 2. flush interval elapsed

func NewChunkExecutor

func NewChunkExecutor(execute Execute, opts ...ChunkOption) *ChunkExecutor

NewChunkExecutor returns a ChunkExecutor.

func (*ChunkExecutor) Add

func (ce *ChunkExecutor) Add(task any, size int) error

Add adds task with given chunk size into ce.

func (*ChunkExecutor) Flush

func (ce *ChunkExecutor) Flush()

Flush forces ce to flush and execute tasks.

func (*ChunkExecutor) Wait

func (ce *ChunkExecutor) Wait()

Wait waits the execution to be done.

type ChunkOption

type ChunkOption func(options *chunkOptions)

ChunkOption defines the method to customize a ChunkExecutor.

func WithChunkBytes

func WithChunkBytes(size int) ChunkOption

WithChunkBytes customizes a ChunkExecutor with the given chunk size.

func WithFlushInterval

func WithFlushInterval(duration time.Duration) ChunkOption

WithFlushInterval customizes a ChunkExecutor with the given flush interval.

type DelayExecutor

type DelayExecutor struct {
	// contains filtered or unexported fields
}

A DelayExecutor delays a tasks on given delay interval.

func NewDelayExecutor

func NewDelayExecutor(fn func(), delay time.Duration) *DelayExecutor

NewDelayExecutor returns a DelayExecutor with given fn and delay.

func (*DelayExecutor) Trigger

func (de *DelayExecutor) Trigger()

Trigger triggers the task to be executed after given delay, safe to trigger more than once.

type Execute

type Execute func(tasks []any)

Execute defines the method to execute tasks.

type LessExecutor

type LessExecutor struct {
	// contains filtered or unexported fields
}

A LessExecutor is an executor to limit execution once within given time interval.

func NewLessExecutor

func NewLessExecutor(threshold time.Duration) *LessExecutor

NewLessExecutor returns a LessExecutor with given threshold as time interval.

func (*LessExecutor) DoOrDiscard

func (le *LessExecutor) DoOrDiscard(execute func()) bool

DoOrDiscard executes or discards the task depends on if another task was executed within the time interval.

type PeriodicalExecutor

type PeriodicalExecutor struct {
	// contains filtered or unexported fields
}

A PeriodicalExecutor is an executor that periodically execute tasks.

func NewPeriodicalExecutor

func NewPeriodicalExecutor(interval time.Duration, container TaskContainer) *PeriodicalExecutor

NewPeriodicalExecutor returns a PeriodicalExecutor with given interval and container.

func (*PeriodicalExecutor) Add

func (pe *PeriodicalExecutor) Add(task any)

Add adds tasks into pe.

func (*PeriodicalExecutor) Flush

func (pe *PeriodicalExecutor) Flush() bool

Flush forces pe to execute tasks.

func (*PeriodicalExecutor) Sync

func (pe *PeriodicalExecutor) Sync(fn func())

Sync lets caller run fn thread-safe with pe, especially for the underlying container.

func (*PeriodicalExecutor) Wait

func (pe *PeriodicalExecutor) Wait()

Wait waits the execution to be done.

type TaskContainer

type TaskContainer interface {
	// AddTask adds the task into the container.
	// Returns true if the container needs to be flushed after the addition.
	AddTask(task any) bool
	// Execute handles the collected tasks by the container when flushing.
	Execute(tasks any)
	// RemoveAll removes the contained tasks, and return them.
	RemoveAll() any
}

TaskContainer interface defines a type that can be used as the underlying container that used to do periodical executions.

Jump to

Keyboard shortcuts

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