ants/worker_queue.go

50 lines
959 B
Go
Raw Normal View History

package ants
import (
"errors"
"time"
)
var (
2019-10-20 13:22:13 +03:00
// errQueueIsFull will be returned when the worker queue is full.
errQueueIsFull = errors.New("the queue is full")
2019-10-20 13:22:13 +03:00
// errQueueIsReleased will be returned when trying to insert item to a released worker queue.
errQueueIsReleased = errors.New("the queue length is zero")
)
type worker interface {
run()
finish()
lastUsedTime() time.Time
inputFunc(func())
inputParam(interface{})
}
type workerQueue interface {
len() int
isEmpty() bool
insert(worker) error
detach() worker
refresh(duration time.Duration) []worker // clean up the stale workers and return them
2019-10-20 13:22:13 +03:00
reset()
}
type queueType int
const (
queueTypeStack queueType = 1 << iota
queueTypeLoopQueue
)
2023-06-08 19:12:59 +03:00
func newWorkerQueue(qType queueType, size int) workerQueue {
switch qType {
case queueTypeStack:
return newWorkerStack(size)
case queueTypeLoopQueue:
return newWorkerLoopQueue(size)
default:
return newWorkerStack(size)
}
}