mirror of https://github.com/panjf2000/ants.git
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()
|
||||
return false
|
||||
}
|
||||
worker.recycleTime = p.nowTime()
|
||||
worker.lastUsed = p.nowTime()
|
||||
p.lock.Lock()
|
||||
|
||||
// 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()
|
||||
return false
|
||||
}
|
||||
worker.recycleTime = p.nowTime()
|
||||
worker.lastUsed = p.nowTime()
|
||||
p.lock.Lock()
|
||||
|
||||
// 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 chan func()
|
||||
|
||||
// recycleTime will be updated when putting a worker back into queue.
|
||||
recycleTime time.Time
|
||||
// lastUsed will be updated when putting a worker back into queue.
|
||||
lastUsed time.Time
|
||||
}
|
||||
|
||||
// run starts a goroutine to repeat the process
|
||||
|
@ -77,7 +77,7 @@ func (w *goWorker) finish() {
|
|||
}
|
||||
|
||||
func (w *goWorker) when() time.Time {
|
||||
return w.recycleTime
|
||||
return w.lastUsed
|
||||
}
|
||||
|
||||
func (w *goWorker) inputFunc(fn func()) {
|
||||
|
|
|
@ -37,8 +37,8 @@ type goWorkerWithFunc struct {
|
|||
// args is a job should be done.
|
||||
args chan interface{}
|
||||
|
||||
// recycleTime will be updated when putting a worker back into queue.
|
||||
recycleTime time.Time
|
||||
// lastUsed will be updated when putting a worker back into queue.
|
||||
lastUsed time.Time
|
||||
}
|
||||
|
||||
// run starts a goroutine to repeat the process
|
||||
|
@ -77,7 +77,7 @@ func (w *goWorkerWithFunc) finish() {
|
|||
}
|
||||
|
||||
func (w *goWorkerWithFunc) when() time.Time {
|
||||
return w.recycleTime
|
||||
return w.lastUsed
|
||||
}
|
||||
|
||||
func (w *goWorkerWithFunc) inputFunc(func()) {
|
||||
|
|
|
@ -23,7 +23,7 @@ func TestLoopQueue(t *testing.T) {
|
|||
q := newWorkerLoopQueue(size)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
||||
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
@ -35,14 +35,14 @@ func TestLoopQueue(t *testing.T) {
|
|||
time.Sleep(time.Second)
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
||||
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
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")
|
||||
|
||||
q.staleWorkers(time.Second)
|
||||
|
@ -56,14 +56,14 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||
// 1
|
||||
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, -1, q.binarySearch(expiry1), "index should be -1")
|
||||
|
||||
// 2
|
||||
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")
|
||||
|
||||
|
@ -73,15 +73,15 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||
|
||||
// more
|
||||
for i := 0; i < 5; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
|
||||
expiry3 := time.Now()
|
||||
_ = q.insert(&goWorker{recycleTime: expiry3})
|
||||
_ = q.insert(&goWorker{lastUsed: expiry3})
|
||||
|
||||
var err error
|
||||
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")
|
||||
|
@ -92,10 +92,10 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||
}
|
||||
|
||||
expiry4 := time.Now()
|
||||
_ = q.insert(&goWorker{recycleTime: expiry4})
|
||||
_ = q.insert(&goWorker{lastUsed: expiry4})
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
// head = 6, tail = 5, insert direction ->
|
||||
// [expiry4, time, time, time, time, nil/tail, time/head, time, time, time]
|
||||
|
@ -105,14 +105,14 @@ func TestRotatedArraySearch(t *testing.T) {
|
|||
_ = q.detach()
|
||||
}
|
||||
expiry5 := time.Now()
|
||||
_ = q.insert(&goWorker{recycleTime: expiry5})
|
||||
_ = q.insert(&goWorker{lastUsed: expiry5})
|
||||
|
||||
// head = 6, tail = 5, insert direction ->
|
||||
// [expiry4, time, time, time, time, expiry5, nil/tail, nil, nil, time/head]
|
||||
assert.EqualValues(t, 5, q.binarySearch(expiry5), "index should be 5")
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
// head = 9, tail = 9, insert direction ->
|
||||
// [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]
|
||||
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]...)
|
||||
time.Sleep(u)
|
||||
|
||||
for i := 0; i < size/2; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
workers := q.staleWorkers(u)
|
||||
|
||||
|
@ -146,7 +146,7 @@ func TestRetrieveExpiry(t *testing.T) {
|
|||
time.Sleep(u)
|
||||
|
||||
for i := 0; i < size/2; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
expirew = expirew[:0]
|
||||
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]
|
||||
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++ {
|
||||
_ = q.detach()
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
time.Sleep(u)
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ func TestWorkerStack(t *testing.T) {
|
|||
q := newWorkerArray(queueType(-1), 0)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
||||
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ func TestWorkerStack(t *testing.T) {
|
|||
|
||||
expired := time.Now()
|
||||
|
||||
err := q.insert(&goWorker{recycleTime: expired})
|
||||
err := q.insert(&goWorker{lastUsed: expired})
|
||||
if err != nil {
|
||||
t.Fatal("Enqueue error")
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ func TestWorkerStack(t *testing.T) {
|
|||
time.Sleep(time.Second)
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
||||
err := q.insert(&goWorker{lastUsed: time.Now()})
|
||||
if err != nil {
|
||||
t.Fatal("Enqueue error")
|
||||
}
|
||||
|
@ -57,14 +57,14 @@ func TestSearch(t *testing.T) {
|
|||
// 1
|
||||
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, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
||||
|
||||
// 2
|
||||
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")
|
||||
|
||||
|
@ -74,15 +74,15 @@ func TestSearch(t *testing.T) {
|
|||
|
||||
// more
|
||||
for i := 0; i < 5; i++ {
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
_ = q.insert(&goWorker{lastUsed: time.Now()})
|
||||
}
|
||||
|
||||
expiry3 := time.Now()
|
||||
|
||||
_ = q.insert(&goWorker{recycleTime: expiry3})
|
||||
_ = q.insert(&goWorker{lastUsed: expiry3})
|
||||
|
||||
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")
|
||||
|
|
Loading…
Reference in New Issue