2019-10-09 22:02:04 +03:00
|
|
|
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-09 22:02:04 +03:00
|
|
|
|
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")
|
2019-10-09 22:02:04 +03:00
|
|
|
)
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
type worker interface {
|
|
|
|
run()
|
|
|
|
finish()
|
2023-03-23 10:16:21 +03:00
|
|
|
lastUsedTime() time.Time
|
2023-03-23 06:40:06 +03:00
|
|
|
inputFunc(func())
|
|
|
|
inputParam(interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type workerQueue interface {
|
2019-10-09 22:02:04 +03:00
|
|
|
len() int
|
|
|
|
isEmpty() bool
|
2023-03-23 06:40:06 +03:00
|
|
|
insert(worker) error
|
|
|
|
detach() worker
|
2023-04-04 08:01:14 +03:00
|
|
|
refresh(duration time.Duration) []worker // clean up the stale workers and return them
|
2019-10-20 13:22:13 +03:00
|
|
|
reset()
|
2019-10-09 22:02:04 +03:00
|
|
|
}
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
type queueType int
|
2019-10-09 22:02:04 +03:00
|
|
|
|
|
|
|
const (
|
2023-03-23 06:40:06 +03:00
|
|
|
queueTypeStack queueType = 1 << iota
|
|
|
|
queueTypeLoopQueue
|
2019-10-09 22:02:04 +03:00
|
|
|
)
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
func newWorkerArray(qType queueType, size int) workerQueue {
|
|
|
|
switch qType {
|
|
|
|
case queueTypeStack:
|
2019-10-09 22:02:04 +03:00
|
|
|
return newWorkerStack(size)
|
2023-03-23 06:40:06 +03:00
|
|
|
case queueTypeLoopQueue:
|
2019-10-09 22:02:04 +03:00
|
|
|
return newWorkerLoopQueue(size)
|
|
|
|
default:
|
|
|
|
return newWorkerStack(size)
|
|
|
|
}
|
|
|
|
}
|