workers

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWorkers = NewWorkers(int64(runtime.GOMAXPROCS(0)))

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

Functions

This section is empty.

Types

type FuncJob

type FuncJob func()

FuncJob type defines a function job.

func (FuncJob) Next

func (job FuncJob) Next() func()

Next returns the function itself for FuncJob.

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()
}

Job interface defines a method to get the next job function.

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](w *Workers, 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 struct {
	// contains filtered or unexported fields
}

Workers struct holds the size and semaphore for managing concurrent jobs.

func NewWorkers

func NewWorkers(n int64) *Workers

NewWorkers creates a new Workers instance with a given size.

func (*Workers) Listen

func (w *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 (w *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.

func (Workers) Size

func (w Workers) Size() int64

Size returns the size of the Workers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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