2019-10-09 19:59:19 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type loopQueue struct {
|
2023-03-23 06:40:06 +03:00
|
|
|
items []worker
|
|
|
|
expiry []worker
|
2019-10-20 13:22:13 +03:00
|
|
|
head int
|
|
|
|
tail int
|
|
|
|
size int
|
|
|
|
isFull bool
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
func newWorkerLoopQueue(size int) *loopQueue {
|
2019-10-20 13:22:13 +03:00
|
|
|
return &loopQueue{
|
2023-03-23 06:40:06 +03:00
|
|
|
items: make([]worker, size),
|
2019-10-20 13:22:13 +03:00
|
|
|
size: size,
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wq *loopQueue) len() int {
|
2019-10-20 13:22:13 +03:00
|
|
|
if wq.size == 0 {
|
2019-10-09 19:59:19 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
if wq.head == wq.tail {
|
|
|
|
if wq.isFull {
|
|
|
|
return wq.size
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if wq.tail > wq.head {
|
|
|
|
return wq.tail - wq.head
|
|
|
|
}
|
|
|
|
|
|
|
|
return wq.size - wq.head + wq.tail
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wq *loopQueue) isEmpty() bool {
|
2019-10-20 13:22:13 +03:00
|
|
|
return wq.head == wq.tail && !wq.isFull
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
func (wq *loopQueue) insert(w worker) error {
|
2019-10-20 13:22:13 +03:00
|
|
|
if wq.size == 0 {
|
|
|
|
return errQueueIsReleased
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
if wq.isFull {
|
|
|
|
return errQueueIsFull
|
|
|
|
}
|
2023-03-23 06:40:06 +03:00
|
|
|
wq.items[wq.tail] = w
|
2019-10-20 13:22:13 +03:00
|
|
|
wq.tail++
|
|
|
|
|
|
|
|
if wq.tail == wq.size {
|
|
|
|
wq.tail = 0
|
|
|
|
}
|
|
|
|
if wq.tail == wq.head {
|
|
|
|
wq.isFull = true
|
|
|
|
}
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
func (wq *loopQueue) detach() worker {
|
2019-10-20 13:22:13 +03:00
|
|
|
if wq.isEmpty() {
|
2019-10-09 19:59:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w := wq.items[wq.head]
|
2020-08-29 13:51:56 +03:00
|
|
|
wq.items[wq.head] = nil
|
2019-10-20 13:22:13 +03:00
|
|
|
wq.head++
|
|
|
|
if wq.head == wq.size {
|
|
|
|
wq.head = 0
|
|
|
|
}
|
|
|
|
wq.isFull = false
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
func (wq *loopQueue) staleWorkers(duration time.Duration) []worker {
|
2022-01-31 05:49:03 +03:00
|
|
|
expiryTime := time.Now().Add(-duration)
|
|
|
|
index := wq.binarySearch(expiryTime)
|
|
|
|
if index == -1 {
|
2019-10-09 22:02:04 +03:00
|
|
|
return nil
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
wq.expiry = wq.expiry[:0]
|
|
|
|
|
2022-01-31 05:49:03 +03:00
|
|
|
if wq.head <= index {
|
|
|
|
wq.expiry = append(wq.expiry, wq.items[wq.head:index+1]...)
|
|
|
|
for i := wq.head; i < index+1; i++ {
|
|
|
|
wq.items[i] = nil
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
2022-01-31 05:49:03 +03:00
|
|
|
} else {
|
|
|
|
wq.expiry = append(wq.expiry, wq.items[0:index+1]...)
|
|
|
|
wq.expiry = append(wq.expiry, wq.items[wq.head:]...)
|
|
|
|
for i := 0; i < index+1; i++ {
|
|
|
|
wq.items[i] = nil
|
2019-10-20 13:22:13 +03:00
|
|
|
}
|
2022-01-31 05:49:03 +03:00
|
|
|
for i := wq.head; i < wq.size; i++ {
|
|
|
|
wq.items[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
head := (index + 1) % wq.size
|
|
|
|
wq.head = head
|
|
|
|
if len(wq.expiry) > 0 {
|
2019-10-20 13:22:13 +03:00
|
|
|
wq.isFull = false
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
2019-10-20 13:22:13 +03:00
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
return wq.expiry
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 05:49:03 +03:00
|
|
|
func (wq *loopQueue) binarySearch(expiryTime time.Time) int {
|
|
|
|
var mid, nlen, basel, tmid int
|
|
|
|
nlen = len(wq.items)
|
|
|
|
|
|
|
|
// if no need to remove work, return -1
|
2023-03-23 10:16:21 +03:00
|
|
|
if wq.isEmpty() || expiryTime.Before(wq.items[wq.head].lastUsedTime()) {
|
2022-01-31 05:49:03 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// example
|
|
|
|
// size = 8, head = 7, tail = 4
|
|
|
|
// [ 2, 3, 4, 5, nil, nil, nil, 1] true position
|
|
|
|
// 0 1 2 3 4 5 6 7
|
|
|
|
// tail head
|
|
|
|
//
|
|
|
|
// 1 2 3 4 nil nil nil 0 mapped position
|
|
|
|
// r l
|
|
|
|
|
|
|
|
// base algorithm is a copy from worker_stack
|
|
|
|
// map head and tail to effective left and right
|
|
|
|
r := (wq.tail - 1 - wq.head + nlen) % nlen
|
|
|
|
basel = wq.head
|
|
|
|
l := 0
|
|
|
|
for l <= r {
|
|
|
|
mid = l + ((r - l) >> 1)
|
|
|
|
// calculate true mid position from mapped mid position
|
|
|
|
tmid = (mid + basel + nlen) % nlen
|
2023-03-23 10:16:21 +03:00
|
|
|
if expiryTime.Before(wq.items[tmid].lastUsedTime()) {
|
2022-01-31 05:49:03 +03:00
|
|
|
r = mid - 1
|
|
|
|
} else {
|
|
|
|
l = mid + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// return true position from mapped position
|
|
|
|
return (r + basel + nlen) % nlen
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
func (wq *loopQueue) reset() {
|
|
|
|
if wq.isEmpty() {
|
2019-10-09 19:59:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:40:06 +03:00
|
|
|
retry:
|
2019-10-20 13:22:13 +03:00
|
|
|
if w := wq.detach(); w != nil {
|
2023-03-23 06:40:06 +03:00
|
|
|
w.finish()
|
|
|
|
goto retry
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
wq.items = wq.items[:0]
|
2019-10-20 13:22:13 +03:00
|
|
|
wq.size = 0
|
2019-10-09 19:59:19 +03:00
|
|
|
wq.head = 0
|
|
|
|
wq.tail = 0
|
|
|
|
}
|