mirror of
https://github.com/panjf2000/ants.git
synced 2025-12-16 18:11:03 +00:00
chore: rename the field 'recycleTime' of worker to 'lastUsed'
This commit is contained in:
parent
7a56a5c082
commit
e425c7b917
2
pool.go
2
pool.go
@ -389,7 +389,7 @@ func (p *Pool) revertWorker(worker *goWorker) bool {
|
|||||||
p.cond.Broadcast()
|
p.cond.Broadcast()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
worker.recycleTime = p.nowTime()
|
worker.lastUsed = p.nowTime()
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
|
|
||||||
// To avoid memory leaks, add a double check in the lock scope.
|
// To avoid memory leaks, add a double check in the lock scope.
|
||||||
|
|||||||
@ -395,7 +395,7 @@ func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
|
|||||||
p.cond.Broadcast()
|
p.cond.Broadcast()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
worker.recycleTime = p.nowTime()
|
worker.lastUsed = p.nowTime()
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
|
|
||||||
// To avoid memory leaks, add a double check in the lock scope.
|
// To avoid memory leaks, add a double check in the lock scope.
|
||||||
|
|||||||
@ -37,8 +37,8 @@ type goWorker struct {
|
|||||||
// task is a job should be done.
|
// task is a job should be done.
|
||||||
task chan func()
|
task chan func()
|
||||||
|
|
||||||
// recycleTime will be updated when putting a worker back into queue.
|
// lastUsed will be updated when putting a worker back into queue.
|
||||||
recycleTime time.Time
|
lastUsed time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
@ -77,7 +77,7 @@ func (w *goWorker) finish() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *goWorker) when() time.Time {
|
func (w *goWorker) when() time.Time {
|
||||||
return w.recycleTime
|
return w.lastUsed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *goWorker) inputFunc(fn func()) {
|
func (w *goWorker) inputFunc(fn func()) {
|
||||||
|
|||||||
@ -37,8 +37,8 @@ type goWorkerWithFunc struct {
|
|||||||
// args is a job should be done.
|
// args is a job should be done.
|
||||||
args chan interface{}
|
args chan interface{}
|
||||||
|
|
||||||
// recycleTime will be updated when putting a worker back into queue.
|
// lastUsed will be updated when putting a worker back into queue.
|
||||||
recycleTime time.Time
|
lastUsed time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// run starts a goroutine to repeat the process
|
// run starts a goroutine to repeat the process
|
||||||
@ -77,7 +77,7 @@ func (w *goWorkerWithFunc) finish() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *goWorkerWithFunc) when() time.Time {
|
func (w *goWorkerWithFunc) when() time.Time {
|
||||||
return w.recycleTime
|
return w.lastUsed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *goWorkerWithFunc) inputFunc(func()) {
|
func (w *goWorkerWithFunc) inputFunc(func()) {
|
||||||
|
|||||||
@ -23,7 +23,7 @@ func TestLoopQueue(t *testing.T) {
|
|||||||
q := newWorkerLoopQueue(size)
|
q := newWorkerLoopQueue(size)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -35,14 +35,14 @@ func TestLoopQueue(t *testing.T) {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
for i := 0; i < 6; i++ {
|
for i := 0; i < 6; i++ {
|
||||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert.EqualValues(t, 10, q.len(), "Len error")
|
assert.EqualValues(t, 10, q.len(), "Len error")
|
||||||
|
|
||||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
assert.Error(t, err, "Enqueue, error")
|
assert.Error(t, err, "Enqueue, error")
|
||||||
|
|
||||||
q.staleWorkers(time.Second)
|
q.staleWorkers(time.Second)
|
||||||
@ -56,14 +56,14 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||||||
// 1
|
// 1
|
||||||
expiry1 := time.Now()
|
expiry1 := time.Now()
|
||||||
|
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
|
|
||||||
assert.EqualValues(t, 0, q.binarySearch(time.Now()), "index should be 0")
|
assert.EqualValues(t, 0, q.binarySearch(time.Now()), "index should be 0")
|
||||||
assert.EqualValues(t, -1, q.binarySearch(expiry1), "index should be -1")
|
assert.EqualValues(t, -1, q.binarySearch(expiry1), "index should be -1")
|
||||||
|
|
||||||
// 2
|
// 2
|
||||||
expiry2 := time.Now()
|
expiry2 := time.Now()
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
|
|
||||||
assert.EqualValues(t, -1, q.binarySearch(expiry1), "index should be -1")
|
assert.EqualValues(t, -1, q.binarySearch(expiry1), "index should be -1")
|
||||||
|
|
||||||
@ -73,15 +73,15 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||||||
|
|
||||||
// more
|
// more
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
expiry3 := time.Now()
|
expiry3 := time.Now()
|
||||||
_ = q.insert(&goWorker{recycleTime: expiry3})
|
_ = q.insert(&goWorker{lastUsed: expiry3})
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
for err != errQueueIsFull {
|
for err != errQueueIsFull {
|
||||||
err = q.insert(&goWorker{recycleTime: time.Now()})
|
err = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, 7, q.binarySearch(expiry3), "index should be 7")
|
assert.EqualValues(t, 7, q.binarySearch(expiry3), "index should be 7")
|
||||||
@ -92,10 +92,10 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expiry4 := time.Now()
|
expiry4 := time.Now()
|
||||||
_ = q.insert(&goWorker{recycleTime: expiry4})
|
_ = q.insert(&goWorker{lastUsed: expiry4})
|
||||||
|
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
// head = 6, tail = 5, insert direction ->
|
// head = 6, tail = 5, insert direction ->
|
||||||
// [expiry4, time, time, time, time, nil/tail, time/head, time, time, time]
|
// [expiry4, time, time, time, time, nil/tail, time/head, time, time, time]
|
||||||
@ -105,14 +105,14 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||||||
_ = q.detach()
|
_ = q.detach()
|
||||||
}
|
}
|
||||||
expiry5 := time.Now()
|
expiry5 := time.Now()
|
||||||
_ = q.insert(&goWorker{recycleTime: expiry5})
|
_ = q.insert(&goWorker{lastUsed: expiry5})
|
||||||
|
|
||||||
// head = 6, tail = 5, insert direction ->
|
// head = 6, tail = 5, insert direction ->
|
||||||
// [expiry4, time, time, time, time, expiry5, nil/tail, nil, nil, time/head]
|
// [expiry4, time, time, time, time, expiry5, nil/tail, nil, nil, time/head]
|
||||||
assert.EqualValues(t, 5, q.binarySearch(expiry5), "index should be 5")
|
assert.EqualValues(t, 5, q.binarySearch(expiry5), "index should be 5")
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
// head = 9, tail = 9, insert direction ->
|
// head = 9, tail = 9, insert direction ->
|
||||||
// [expiry4, time, time, time, time, expiry5, time, time, time, time/head/tail]
|
// [expiry4, time, time, time, time, expiry5, time, time, time, time/head/tail]
|
||||||
@ -130,13 +130,13 @@ func TestRetrieveExpiry(t *testing.T) {
|
|||||||
|
|
||||||
// test [ time+1s, time+1s, time+1s, time+1s, time+1s, time, time, time, time, time]
|
// test [ time+1s, time+1s, time+1s, time+1s, time+1s, time, time, time, time, time]
|
||||||
for i := 0; i < size/2; i++ {
|
for i := 0; i < size/2; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
expirew = append(expirew, q.items[:size/2]...)
|
expirew = append(expirew, q.items[:size/2]...)
|
||||||
time.Sleep(u)
|
time.Sleep(u)
|
||||||
|
|
||||||
for i := 0; i < size/2; i++ {
|
for i := 0; i < size/2; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
workers := q.staleWorkers(u)
|
workers := q.staleWorkers(u)
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ func TestRetrieveExpiry(t *testing.T) {
|
|||||||
time.Sleep(u)
|
time.Sleep(u)
|
||||||
|
|
||||||
for i := 0; i < size/2; i++ {
|
for i := 0; i < size/2; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
expirew = expirew[:0]
|
expirew = expirew[:0]
|
||||||
expirew = append(expirew, q.items[size/2:]...)
|
expirew = append(expirew, q.items[size/2:]...)
|
||||||
@ -157,13 +157,13 @@ func TestRetrieveExpiry(t *testing.T) {
|
|||||||
|
|
||||||
// test [ time+1s, time+1s, time+1s, nil, nil, time+1s, time+1s, time+1s, time+1s, time+1s]
|
// test [ time+1s, time+1s, time+1s, nil, nil, time+1s, time+1s, time+1s, time+1s, time+1s]
|
||||||
for i := 0; i < size/2; i++ {
|
for i := 0; i < size/2; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
for i := 0; i < size/2; i++ {
|
for i := 0; i < size/2; i++ {
|
||||||
_ = q.detach()
|
_ = q.detach()
|
||||||
}
|
}
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
time.Sleep(u)
|
time.Sleep(u)
|
||||||
|
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func TestWorkerStack(t *testing.T) {
|
|||||||
q := newWorkerArray(queueType(-1), 0)
|
q := newWorkerArray(queueType(-1), 0)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ func TestWorkerStack(t *testing.T) {
|
|||||||
|
|
||||||
expired := time.Now()
|
expired := time.Now()
|
||||||
|
|
||||||
err := q.insert(&goWorker{recycleTime: expired})
|
err := q.insert(&goWorker{lastUsed: expired})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Enqueue error")
|
t.Fatal("Enqueue error")
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func TestWorkerStack(t *testing.T) {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
for i := 0; i < 6; i++ {
|
for i := 0; i < 6; i++ {
|
||||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Enqueue error")
|
t.Fatal("Enqueue error")
|
||||||
}
|
}
|
||||||
@ -57,14 +57,14 @@ func TestSearch(t *testing.T) {
|
|||||||
// 1
|
// 1
|
||||||
expiry1 := time.Now()
|
expiry1 := time.Now()
|
||||||
|
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
|
|
||||||
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, time.Now()), "index should be 0")
|
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, time.Now()), "index should be 0")
|
||||||
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
||||||
|
|
||||||
// 2
|
// 2
|
||||||
expiry2 := time.Now()
|
expiry2 := time.Now()
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
|
|
||||||
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
||||||
|
|
||||||
@ -74,15 +74,15 @@ func TestSearch(t *testing.T) {
|
|||||||
|
|
||||||
// more
|
// more
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
expiry3 := time.Now()
|
expiry3 := time.Now()
|
||||||
|
|
||||||
_ = q.insert(&goWorker{recycleTime: expiry3})
|
_ = q.insert(&goWorker{lastUsed: expiry3})
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
|
assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user