pqueue

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package pqueue implements a priority queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcquireMulti

func AcquireMulti[T any](ctx context.Context, e T, qList ...*Queue[T]) (context.Context, func(), error)

AcquireMulti is used to simultaneously lock multiple queues without the risk of deadlock. The returned context needs to be used on calls to [Acquire] or [TryAcquire] which will immediately succeed since the resource is already acquired. Attempting to acquire other resources with [Acquire], [TryAcquire], or AcquireMulti using the returned context and will fail for being outside of the transaction. The returned function must be called to release the resources. The returned function is not thread safe, ensure no other simultaneous calls to [Acquire] or [TryAcquire] using the returned context have finished before it is called.

Types

type Opts

type Opts[T any] struct {
	Max  int                           // maximum concurrent entries, defaults to 1.
	Next func(queued, active []*T) int // function to lookup index of next queued entry to release, defaults to oldest entry.
}

Opts is used to configure a new priority queue.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](opts Opts[T]) *Queue[T]

New creates a new priority queue.

func (*Queue[T]) Acquire

func (q *Queue[T]) Acquire(ctx context.Context, e T) (func(), error)

Acquire adds a new entry to the queue and returns once it is ready. The returned function must be called when the queued job completes to release the next entry. If there is any error, the returned function will be nil.

func (*Queue[T]) TryAcquire

func (q *Queue[T]) TryAcquire(ctx context.Context, e T) (func(), error)

TryAcquire attempts to add an entry on to the list of active entries. If the returned function is nil, the queue was not available. If the returned function is not nil, it must be called when the job is complete to release the next entry.

Jump to

Keyboard shortcuts

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