workers

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWorkers = Workers(runtime.GOMAXPROCS(0))

DefaultWorkers is a default instance of Workers with the size of GOMAXPROCS.

Functions

func Listen added in v1.0.1

func Listen(ctx context.Context, c <-chan func())

Listen listens for jobs using DefaultWorkers from a channel and runs them concurrently. It stops listening when the context is done or the channel is closed.

func Run added in v1.0.1

func Run(ctx context.Context, job Job) error

Run executes jobs using DefaultWorkers from the Job interface until there are no more jobs. It acquires a semaphore weight for each job and releases it when the job is done.

Types

type FuncJob

type FuncJob func()

FuncJob type defines a single function job.

func (FuncJob) Next

func (job FuncJob) Next() (func(), bool)

Next always returns itself with (job, true), unless job is nil.

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer interface defines a set of integer types.

type Job

type Job interface {
	Next() (func(), bool)
}

Job interface defines a method to get the next job function. The returned bool indicates whether there might be more jobs. - (f, true): got a job, and there may be more. - (f, false): got a job, but this is the last one. - (nil, false): no more jobs.

func MapJob

func MapJob[M ~map[K]V, K comparable, V any](m M, f func(K, V)) Job

MapJob creates a Job that iterates over a map and applies a function to each key-value pair.

func RangeJob

func RangeJob[T Integer](start, end T, f func(T)) Job

RangeJob creates a Job that iterates over a range of integers and applies a function to each value.

func SliceJob

func SliceJob[T any](s []T, f func(int, T)) Job

SliceJob creates a Job that iterates over a slice and applies a function to each element.

type JobList

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

JobList is a struct that holds a list of jobs, a worker pool, a function to execute jobs, a channel for signaling, and a boolean indicating if the job list is closed.

func NewJobList

func NewJobList[T any](workers int, f func(T)) *JobList[T]

NewJobList creates a new JobList with the given worker pool and job function.

func (*JobList[T]) Close

func (l *JobList[T]) Close() error

Close stops processing jobs and clears the job list.

func (*JobList[T]) PushBack

func (l *JobList[T]) PushBack(v T) error

PushBack adds a job to the end of the job list and signals the worker pool.

func (*JobList[T]) PushFront

func (l *JobList[T]) PushFront(v T) error

PushFront adds a job to the front of the job list and signals the worker pool.

func (*JobList[T]) Start

func (l *JobList[T]) Start(ctx context.Context) error

Start begins processing jobs in the job list using the provided context.

type Workers

type Workers int

Workers holds the size and semaphore for managing concurrent jobs.

func (Workers) Listen

func (i Workers) Listen(ctx context.Context, c <-chan func())

Listen listens for jobs from a channel and runs them concurrently. It stops listening when the context is done or the channel is closed.

func (Workers) Run

func (i Workers) Run(ctx context.Context, job Job) (err error)

Run executes jobs from the Job interface until there are no more jobs. It acquires a semaphore weight for each job and releases it when the job is done.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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