queue

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInit         = errors.New("init error")
	ErrQueueFull    = errors.New("queue is full")
	ErrNotFound     = errors.New("not found")
	ErrNotAvailable = errors.New("item not available")
	ErrMarshal      = errors.New("marshal error")
	ErrInternal     = errors.New("internal error")
)

Functions

This section is empty.

Types

type Item

type Item struct {
	Id   string
	Task *task.Task
	// Score is the priority score of the item. The lower the socre, the higher the priority.
	// It is desired to fetch items with the lowest score first.
	// The score is determined per queue kind as following:
	//  - Fifo: Incremental sequence number.
	//  - Priority: The priority of the task.
	//  - Scheduled: Epoch in millisecond of the scheduled time.
	Score    int64
	QueuedAt time.Time
}

Item represents an item in the queue.

type Kind

type Kind int

Kind is the kind of the queue.

const (
	Fifo Kind = iota
	Priority
	Scheduled
)

type Operator

type Operator interface {
	// Push pushes a task item into the queue.
	Push(ctx context.Context, item *Item) error

	// Pop pops the Item object with the highest priority.
	//  it returns ErrNotFound when there's no items in the queue.
	Pop(ctx context.Context, group task.Group) (*Item, error)

	// PopScheduled pops an Item object that met the schedule.
	//  it returns ErrNotFound when there's no items in the queue.
	//  it returns ErrNotAvailable when there's no available items in the queue(the scheduled time not yet reached).
	PopScheduled(ctx context.Context, group task.Group) (*Item, error)
}

Operator is the interface that should be implmented by the underlying queue implementation.

type Option

type Option struct {
	// TaskGroup identifies the task group.
	TaskGroup task.Group
	// Kind is the kind of the queue.
	Kind Kind
	// PollInterval is the desired wait period between consecutive `Pop()` function calls for available item polling.
	PollInterval time.Duration
	// Operator is the QueueOperator.
	Operator Operator
}

Option specifies the option for a queue.

type Queue

type Queue interface {
	// Push pushes a task into the queue.
	Push(ctx context.Context, task *task.Task) (*Item, error)

	// Pop pops the Item object with the highest priority.
	//  it returns ErrNotFound when there's no items in the queue.
	//  it returns ErrNotAvailable when there's no available items in the queue(the scheduled time not yet reached).
	Pop(ctx context.Context) (*Item, error)

	// TryPop pops the Item object with the highest priority.
	// This function can wait up to `timeout` until there's an item become available.
	//  it returns ErrNotFound when there's no items in the queue.
	//  it returns ErrNotAvailable when there's no available items in the queue(the scheduled time not yet reached).
	TryPop(ctx context.Context, timeout time.Duration) (*Item, error)
}

Queue is the task queue.

func New

func New(option Option) (Queue, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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