Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTaskExist thrown when the task to dispatch already exist ErrTaskExist = errors.New("task exist") )
Functions ¶
func ExecWithInterval ¶ added in v0.0.4
func ExecWithInterval(fn StoppableFunc, start, limit time.Duration)
ExecWithInterval executes a function with a dynamic interval the interval is getting multiplied by 2 in each round, up to the given limit and then it starts all over 1s > 2s > 4s > 8s ... > 1s > 2s > ...
Types ¶
type Dispatcher ¶ added in v0.0.4
type Dispatcher interface {
// Queue adds a new task
Queue(Task) error
// Dispatch will dispatch the next task
Dispatch()
// Start starts ticks
Start()
// Stats returns the number of waiting tasks and the number of running tasks
Stats() *DispatcherStats
}
Dispatcher maintains a queue of tasks to dispatch
func NewDispatcher ¶ added in v0.0.4
func NewDispatcher(opts DispatcherOptions) Dispatcher
NewDispatcher creates a new instance
type DispatcherOptions ¶ added in v0.0.4
type DispatcherOptions struct {
// Ctx is a context for stopping the dispatcher
Ctx context.Context
// Logger used for logs
Logger *zap.Logger
// Interval is the time interval ticker used by dispatcher
// if the value was not provided (zero) -> no interval will run.
// *the calls to Dispatch() should be in a higher level
Interval time.Duration
// Concurrent is the limit of concurrent tasks running
// if zero or negative (<= 0) then defaultConcurrentLimit will be used
Concurrent int
}
DispatcherOptions describes the needed arguments for dispatcher instance
type DispatcherStats ¶ added in v0.0.4
type DispatcherStats struct {
// Waiting is the number of tasks that waits in queue
Waiting int
// Running is the number of running tasks
Running int
// Time is the time when the stats snapshot was taken
Time time.Time
}
DispatcherStats represents runtime stats of the dispatcher
type StoppableFunc ¶ added in v0.0.4
StoppableFunc represents a function that returns two boolean to help with its execution stop will stop the interval, while continue will make the interval value to remain the same if both are false, the interval will be increased (x2)
type Stopper ¶ added in v0.0.4
type Stopper interface {
// IsStopped returns true if the stopper already stopped
IsStopped() bool
// Chan returns a bool channel to be notified once stopped
Chan() chan bool
}
Stopper represents the object used to stop running functions should be used by the running function, once stopped the function act accordingly