2019-10-09 22:02:04 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-10-15 09:50:38 +03:00
|
|
|
// ErrQueueIsFull will be returned when the worker queue is full.
|
2019-10-09 22:02:04 +03:00
|
|
|
ErrQueueIsFull = errors.New("the queue is full")
|
|
|
|
|
2019-10-15 09:50:38 +03:00
|
|
|
// ErrQueueLengthIsZero will be returned when trying to insert item to a released worker queue.
|
2019-10-09 22:02:04 +03:00
|
|
|
ErrQueueLengthIsZero = errors.New("the queue length is zero")
|
|
|
|
)
|
|
|
|
|
|
|
|
type workerArray interface {
|
|
|
|
len() int
|
|
|
|
isEmpty() bool
|
|
|
|
insert(worker *goWorker) error
|
|
|
|
detach() *goWorker
|
|
|
|
findOutExpiry(duration time.Duration) []*goWorker
|
|
|
|
release()
|
|
|
|
}
|
|
|
|
|
|
|
|
type arrayType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
stackType arrayType = 1 << iota
|
|
|
|
loopQueueType
|
|
|
|
)
|
|
|
|
|
|
|
|
func newWorkerArray(aType arrayType, size int) workerArray {
|
|
|
|
switch aType {
|
|
|
|
case stackType:
|
|
|
|
return newWorkerStack(size)
|
|
|
|
case loopQueueType:
|
|
|
|
return newWorkerLoopQueue(size)
|
|
|
|
default:
|
|
|
|
return newWorkerStack(size)
|
|
|
|
}
|
|
|
|
}
|