algo

package
v1.33.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParallelQueue

type ParallelQueue[T fmt.Stringer] struct {
	// contains filtered or unexported fields
}

ParallelQueue executes parallel tasks on separate goroutines.

func NewParallelQueue

func NewParallelQueue[T fmt.Stringer](opts ...QueueOption) *ParallelQueue[T]

NewParallelQueue returns a new queue instance.

func (*ParallelQueue[T]) Add

func (q *ParallelQueue[T]) Add(item T) bool

Add an item to the queue. This method is thread safe within op() and can be called during Run(). It is NOT safe to call Add() from a different, unassociated thread. This method returns False when queue state is done.

Calling Add(item) from the op() given to Run() guarantees that item will be processed.

func (*ParallelQueue[T]) Run

func (q *ParallelQueue[T]) Run(ctx context.Context, op func(context.Context, T) error) error

Run the queue using op() to process each task. Different op()s must not have interdependencies (e.g. op1 is blocked on op2 completing), as this can result in a deadlock.

Queue execution will stop if op() returns an error or the context is canceled. When an error occurs, goroutines may continue to execute. Use q.WaitForOrphans() to wait for the remaining goroutines.

func (*ParallelQueue[T]) WaitForOrphans

func (q *ParallelQueue[T]) WaitForOrphans(ctx context.Context) error

WaitForOrphans will block until remaining op() goroutines finish. Call this if Run() returns an error and you need to know that all remaining threads of execution are done.

WaitForOrphans will exit early if ctx is cancelled. This `ctx` should be different from the `ctx` given to Run().

type Queue

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

Queue for implementing graph algorithms.

func NewQueue

func NewQueue[N any]() *Queue[N]

NewQueue returns a new serial queue.

func (*Queue[N]) Add

func (q *Queue[N]) Add(n N)

Add an element to the work queue.

func (*Queue[N]) Empty

func (q *Queue[N]) Empty() bool

Empty returns true if the queue is empty.

func (*Queue[N]) Pop

func (q *Queue[N]) Pop() N

Pop an element to process. This may be in any order, not necessarily FIFO.

type QueueOption

type QueueOption func(*config)

func UseTracer

func UseTracer(t Tracer) QueueOption

func WorkerCount

func WorkerCount(n int) QueueOption

type RunInfo

type RunInfo struct {
	// ID for the execution. This is item.String().
	ID string
	// Queued is the timestamp of when the task was Add()ed.
	Queued time.Time
	// Start is the timestamp of when the task was started as a goroutine.
	Start time.Time
	// End is when the task was finished.
	End time.Time
	// Err is the result of the task.
	Err error
}

RunInfo records the details of a task.

type Tracer

type Tracer func(RunInfo)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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