Documentation
ΒΆ
Index ΒΆ
- type ConfigFunc
- type DistributedPriorityQueue
- type DistributedQueue
- type EnqueuedGroupJob
- type EnqueuedJob
- type EnqueuedSingleGroupJob
- type IAcknowledgeable
- type IBaseQueue
- type ICache
- type IDistributedPriorityQueue
- type IDistributedQueue
- type IExternalBaseQueue
- type IExternalQueue
- type IPersistentPriorityQueue
- type IPersistentQueue
- type IPriorityQueue
- type IQueue
- type ISubscribable
- type IVoidWorkerBinder
- type IWorkerBinder
- type Item
- type Job
- type JobConfigFunc
- type PQItem
- type PersistentPriorityQueue
- type PersistentQueue
- type PriorityQueue
- type Queue
- type Result
- type VoidWorkerFunc
- type Worker
- type WorkerErrFunc
- type WorkerFunc
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
Types ΒΆ
type ConfigFunc ΒΆ
type ConfigFunc func(*configs)
func WithAutoCleanupCache ΒΆ
func WithAutoCleanupCache(duration time.Duration) ConfigFunc
func WithCache ΒΆ
func WithCache(cache ICache) ConfigFunc
func WithConcurrency ΒΆ
func WithConcurrency(concurrency int) ConfigFunc
func WithJobIdGenerator ΒΆ
func WithJobIdGenerator(fn func() string) ConfigFunc
type DistributedPriorityQueue ΒΆ
type DistributedPriorityQueue[T, R any] interface { IExternalBaseQueue // Time complexity: O(log n) Add(data T, priority int, configs ...JobConfigFunc) bool }
func NewDistributedPriorityQueue ΒΆ
func NewDistributedPriorityQueue[T, R any](internalQueue IDistributedPriorityQueue) DistributedPriorityQueue[T, R]
type DistributedQueue ΒΆ
type DistributedQueue[T, R any] interface { IExternalBaseQueue // Time complexity: O(1) Add(data T, configs ...JobConfigFunc) bool }
func NewDistributedQueue ΒΆ
func NewDistributedQueue[T, R any](internalQueue IDistributedQueue) DistributedQueue[T, R]
type EnqueuedGroupJob ΒΆ
type EnqueuedJob ΒΆ
type EnqueuedJob[R any] interface { Job // Drain discards the job's result and error values asynchronously. Drain() error // Result blocks until the job completes and returns the result and any error. Result() (R, error) }
EnqueuedJob represents a job that has been enqueued and can wait for a result.
type EnqueuedSingleGroupJob ΒΆ
type EnqueuedSingleGroupJob[R any] interface { Job EnqueuedGroupJob[R] }
type IAcknowledgeable ΒΆ
type IAcknowledgeable interface {
// Returns true if the item was successfully acknowledged, false otherwise.
Acknowledge(ackID string) bool
// DequeueWithAckId dequeues an item from the queue
// Returns the item, a boolean indicating if the operation was successful, and the acknowledgment ID.
DequeueWithAckId() (any, bool, string)
}
IAcknowledgeable is the root interface of acknowledgeable operations.
type IBaseQueue ΒΆ
IBaseQueue is the root interface of queue operations. workers queue needs to implement this interface.
type IDistributedPriorityQueue ΒΆ
type IDistributedPriorityQueue interface {
IPersistentPriorityQueue
ISubscribable
}
IDistributedPriorityQueue is the root interface of distributed priority queue operations.
type IDistributedQueue ΒΆ
type IDistributedQueue interface {
IPersistentQueue
ISubscribable
}
IDistributedQueue is the root interface of distributed queue operations.
type IExternalBaseQueue ΒΆ
type IExternalBaseQueue interface {
// PendingCount returns the number of Jobs pending in the queue.
PendingCount() int
// Purge removes all pending Jobs from the queue.
// Time complexity: O(n) where n is the number of pending Jobs
Purge()
// Close closes the queue and resets all internal states.
// Time complexity: O(n) where n is the number of channels
Close() error
}
type IExternalQueue ΒΆ
type IExternalQueue[T, R any] interface { IExternalBaseQueue // Worker returns the worker. Worker() Worker[T, R] // JobById returns the job with the given id. JobById(id string) (EnqueuedJob[R], error) // GroupsJobById returns the groups job with the given id. GroupsJobById(id string) (EnqueuedSingleGroupJob[R], error) // WaitUntilFinished waits until all pending Jobs in the queue are processed. // Time complexity: O(n) where n is the number of pending Jobs WaitUntilFinished() // WaitAndClose waits until all pending Jobs in the queue are processed and then closes the queue. // Time complexity: O(n) where n is the number of pending Jobs WaitAndClose() error }
IExternalQueue is the root interface of concurrent queue operations.
type IPersistentPriorityQueue ΒΆ
type IPersistentPriorityQueue interface {
IPriorityQueue
IAcknowledgeable
}
IPersistentPriorityQueue is the root interface of persistent priority queue operations.
type IPersistentQueue ΒΆ
type IPersistentQueue interface {
IQueue
IAcknowledgeable
}
type IPriorityQueue ΒΆ
type IPriorityQueue interface {
IBaseQueue
Enqueue(item any, priority int) bool
}
IPriorityQueue is the root interface of priority queue operations.
type IQueue ΒΆ
type IQueue interface {
IBaseQueue
Enqueue(item any) bool
}
IQueue is the root interface of queue operations.
type ISubscribable ΒΆ
type ISubscribable interface {
Subscribe(func(action string))
}
ISubscribable is the root interface of subscribable operations.
type IVoidWorkerBinder ΒΆ
type IVoidWorkerBinder[T any] interface { IWorkerBinder[T, any] // WithDistributedQueue binds the void worker to a DistributedQueue implementation. // Distributed queues allow job processing to be spread across multiple instances or processes, // enabling horizontal scaling of workers. Since void workers don't need to return results, // they are perfect for distributed processing scenarios. // // Parameters: // - dq IDistributedQueue: A distributed queue implementation that satisfies the IDistributedQueue interface. // This could be backed by Redis, RabbitMQ, Kafka, or any other distributed messaging system. // // Returns: // - DistributedQueue[T, any]: A distributed queue that can distribute jobs across multiple workers or instances. // // Example usage: // distributedQueue := NewDistributedQueue() // satisfies IDistributedQueue interface, might be backed by Redis or other systems // distributedQueue := voidWorker.WithDistributedQueue(distributedQueue) // distributedQueue.Add(data) // This job can be processed by any worker instance listening to this queue WithDistributedQueue(dq IDistributedQueue) DistributedQueue[T, any] // WithDistributedPriorityQueue binds the void worker to a DistributedPriorityQueue implementation. // This combines distributed processing with priority-based job ordering. Jobs are distributed // across multiple instances but processed according to priority within each instance. // This is ideal for scenarios where some jobs are more urgent than others, but still need // to be processed in a distributed manner. // // Parameters: // - dq IDistributedPriorityQueue: A distributed priority queue implementation that satisfies // the IDistributedPriorityQueue interface. This could be backed by Redis or other // distributed messaging systems that support priority-based message ordering. // // Returns: // - DistributedPriorityQueue[T, any]: A distributed priority queue that can distribute jobs // across multiple workers while maintaining priority ordering. // // Example usage: // priorityQueue := NewDistributedPriorityQueue() // satisfies IDistributedPriorityQueue interface, might be backed by Redis or other systems // distributedPriorityQueue := voidWorker.WithDistributedPriorityQueue(priorityQueue) // distributedPriorityQueue.Add(data, -1) // This job will be processed with higher priority WithDistributedPriorityQueue(dq IDistributedPriorityQueue) DistributedPriorityQueue[T, any] }
IVoidWorkerBinder extends IWorkerBinder with distributed queue capabilities specifically for void workers that don't return results. This interface is specialized for workers that perform actions but don't need to return any data, making them suitable for distributed processing scenarios where results aren't needed or are handled externally.
The void worker pattern is ideal for fire-and-forget operations like sending notifications, updating external systems, or logging events where no response is required.
func NewVoidWorker ΒΆ
func NewVoidWorker[T any](wf VoidWorkerFunc[T], config ...any) IVoidWorkerBinder[T]
NewVoidWorker creates a worker for operations that don't return any value (void functions). This is the most performant worker type as it doesn't use result channels except for panic handling. VoidWorker is the only worker type that can be bound to distributed queues in addition to standard, priority, and persistent queue types.
Parameters:
- wf: Worker function that processes items without returning anything
- config: Optional configuration parameters (concurrency, cache settings, etc.)
If concurrency in config is less than 1 or not provided, it defaults to number of CPU cores.
Example:
worker := NewVoidWorker(func(data int) {
fmt.Printf("Processing: %d\n", data)
})
queue := worker.BindQueue() // Bind to standard queue
distQueue := worker.Copy().WithDistributedQueue(myDistributedQueue) // Bind to provided distributed queue
type IWorkerBinder ΒΆ
type IWorkerBinder[T, R any] interface { Worker[T, R] // BindQueue binds the worker to a standard Queue implementation. // It creates a new Queue with default settings and connects the worker to it. // This is the simplest way to get a standard FIFO queue working with this worker. // // Returns: // - Queue[T, R]: A fully configured Queue that automatically processes jobs using this worker. // // Example usage: // queue := worker.BindQueue() // queue.Add(data) // Enqueues a job that will be processed by the worker BindQueue() Queue[T, R] // WithQueue binds the worker to a custom Queue implementation. // This method allows you to use your own queue implementation as long as it // satisfies the IQueue interface. This is useful when you need specialized // queueing behavior beyond what the standard queue provides. // // Parameters: // - q IQueue: A custom queue implementation that satisfies the IQueue interface. // // Returns: // - Queue[T, R]: A Queue that uses the provided implementation and processes jobs with this worker. // // Example usage: // customQueue := NewCustomQueue() // satisfies IQueue interface // queue := worker.WithQueue(customQueue) WithQueue(q IQueue) Queue[T, R] // BindPriorityQueue binds the worker to a standard PriorityQueue implementation. // It creates a new PriorityQueue with default settings and connects the worker to it. // Use this when you need to process jobs based on priority rather than FIFO order. // // Returns: // - PriorityQueue[T, R]: A fully configured PriorityQueue that processes jobs using this worker. // // Example usage: // priorityQueue := worker.BindPriorityQueue() // satisfies IPriorityQueue interface // priorityQueue.Add(data, 5) // Enqueue with priority 5 BindPriorityQueue() PriorityQueue[T, R] // WithPriorityQueue binds the worker to a custom PriorityQueue implementation. // This method allows you to use your own priority queue implementation as long as it // satisfies the IPriorityQueue interface. This is useful when you need specialized // priority-based queueing beyond what the standard implementation provides. // // Parameters: // - pq IPriorityQueue: A custom priority queue implementation that satisfies the IPriorityQueue interface. // // Returns: // - PriorityQueue[T, R]: A PriorityQueue that uses the provided implementation and processes jobs with this worker. // // Example usage: // customPriorityQueue := NewCustomPriorityQueue() // priorityQueue := worker.WithPriorityQueue(customPriorityQueue) WithPriorityQueue(pq IPriorityQueue) PriorityQueue[T, R] // WithPersistentQueue binds the worker to a PersistentQueue. // PersistentQueue provides durability guarantees for jobs, ensuring they are not lost // even in the event of application crashes or restarts. This is useful for critical // workloads where job completion must be guaranteed. // // Parameters: // - pq IQueue: A queue implementation that provides persistence capabilities. // // Returns: // - PersistentQueue[T, R]: A persistent queue that ensures jobs are not lost and processes them with the worker. // // Example usage: // persistentQueue := worker.WithPersistentQueue(redisQueue) WithPersistentQueue(pq IPersistentQueue) PersistentQueue[T, R] // WithPersistentPriorityQueue binds the worker to a PersistentPriorityQueue. // This combines the benefits of persistence with priority-based processing. Jobs are // both durable and processed according to their priority values. // // Parameters: // - pq IPersistentPriorityQueue: A priority queue implementation that provides persistence capabilities. // // Returns: // - PersistentPriorityQueue[T, R]: A persistent priority queue that processes jobs based on priority // while ensuring they are not lost, using this worker for processing. // // Example usage: // persistentPriorityQueue := worker.WithPersistentPriorityQueue(persistentPriorityQueue) WithPersistentPriorityQueue(pq IPersistentPriorityQueue) PersistentPriorityQueue[T, R] }
IWorkerBinder is the base interface for binding workers to different queue types. It provides methods to connect workers with various queue implementations, enabling flexible worker-queue configurations. This interface extends the Worker interface with queue binding capabilities for handling tasks of type T and producing results of type R.
func NewErrWorker ΒΆ
func NewErrWorker[T any](wf WorkerErrFunc[T], config ...any) IWorkerBinder[T, any]
NewErrWorker creates a worker for operations that only return errors (no result value). This is useful for operations where you only care about success/failure status. Like NewWorker, it can be bound to standard, priority, and persistent queue types.
Parameters:
- wf: Worker function that processes items and returns only error
- config: Optional configuration parameters (concurrency, cache settings, etc.)
If concurrency in config is less than 1 or not provided, it defaults to number of CPU cores.
Example:
worker := NewErrWorker(func(data int) error {
log.Printf("Processing: %d", data)
return nil
})
queue := worker.BindQueue() // Bind to standard queue
func NewWorker ΒΆ
func NewWorker[T, R any](wf WorkerFunc[T, R], config ...any) IWorkerBinder[T, R]
NewWorker creates a worker that can be bound to standard, priority, and persistent queue types. It accepts a worker function that processes items of type T and returns results of type R.
Parameters:
- wf: Worker function that processes queue items and returns a result and error
- config: Optional configuration parameters (concurrency, cache settings, etc.)
If concurrency in config is less than 1 or not provided, it defaults to number of CPU cores.
Example:
worker := NewWorker(func(data string) (int, error) {
return len(data), nil
}, 4) // 4 concurrent workers
queue := worker.BindQueue() // Bind to standard queue
type Item ΒΆ
Item represents a data item to be processed by a worker It combines a unique identifier with a value of any generic type
type Job ΒΆ
type Job interface {
// ID returns the unique identifier of the job.
ID() string
// IsClosed returns whether the job is closed.
IsClosed() bool
// Status returns the current status of the job.
Status() string
// Json returns the JSON representation of the job.
Json() ([]byte, error)
// contains filtered or unexported methods
}
type JobConfigFunc ΒΆ
type JobConfigFunc func(*jobConfigs)
func WithJobId ΒΆ
func WithJobId(id string) JobConfigFunc
type PersistentPriorityQueue ΒΆ
type PersistentPriorityQueue[T, R any] interface { PriorityQueue[T, R] }
type PersistentQueue ΒΆ
PersistentQueue is an interface that extends Queue to support persistent job operations where jobs can be recovered even after application restarts. All jobs must have unique IDs.
type PriorityQueue ΒΆ
type PriorityQueue[T, R any] interface { IExternalQueue[T, R] // Add adds a new Job with the given priority to the queue and returns a channel to receive the result. // Time complexity: O(log n) Add(data T, priority int, configs ...JobConfigFunc) EnqueuedJob[R] // AddAll adds multiple Jobs with the given priority to the queue and returns a channel to receive all responses. // Time complexity: O(n log n) where n is the number of Jobs added AddAll(items []PQItem[T]) EnqueuedGroupJob[R] }
type Queue ΒΆ
type Queue[T, R any] interface { IExternalQueue[T, R] // Add adds a new Job to the queue and returns a EnqueuedJob to handle the job. // Time complexity: O(1) Add(data T, configs ...JobConfigFunc) EnqueuedJob[R] // AddAll adds multiple Jobs to the queue and returns a EnqueuedGroupJob to handle the job. // Time complexity: O(n) where n is the number of Jobs added AddAll(data []Item[T]) EnqueuedGroupJob[R] }
type Result ΒΆ
Result represents the result of a job, containing the data and any error that occurred.
type VoidWorkerFunc ΒΆ
type VoidWorkerFunc[T any] func(T)
VoidWorkerFunc represents a function that processes a Job and returns nothing.
type Worker ΒΆ
type Worker[T, R any] interface { // IsPaused returns whether the worker is paused. IsPaused() bool // IsStopped returns whether the worker is stopped. IsStopped() bool // IsRunning returns whether the worker is running. IsRunning() bool // Pause pauses the worker. Pause() Worker[T, R] // Copy returns a copy of the worker. Copy(config ...any) IWorkerBinder[T, R] // PauseAndWait pauses the worker and waits until all ongoing processes are done. PauseAndWait() // Stop stops the worker and waits until all ongoing processes are done to gracefully close the channels. // Time complexity: O(n) where n is the number of channels Stop() // Status returns the current status of the worker. // Time complexity: O(1) Status() string // Restart restarts the worker and initializes new worker goroutines based on the concurrency. // Time complexity: O(n) where n is the concurrency Restart() error // Resume continues processing jobs those are pending in the queue. // Time complexity: O(n) where n is the concurrency Resume() error // CurrentProcessingCount returns the number of Jobs currently being processed by the worker. // Time complexity: O(1) CurrentProcessingCount() uint32 }
Worker represents a worker that processes Jobs.
type WorkerErrFunc ΒΆ
WorkerErrFunc represents a function that processes a Job and returns an error.
type WorkerFunc ΒΆ
WorkerFunc represents a function that processes a Job and returns a result and an error.
