mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
workerpool: test cleaner
This commit is contained in:
+12
-3
@@ -23,6 +23,8 @@ type workerPool struct {
|
||||
|
||||
LogAllErrors bool
|
||||
|
||||
MaxIdleWorkerDuration time.Duration
|
||||
|
||||
Logger Logger
|
||||
|
||||
lock sync.Mutex
|
||||
@@ -50,13 +52,13 @@ func (wp *workerPool) Start() {
|
||||
go func() {
|
||||
var scratch []*workerChan
|
||||
for {
|
||||
wp.clean(&scratch)
|
||||
select {
|
||||
case <-stopCh:
|
||||
return
|
||||
default:
|
||||
time.Sleep(10 * time.Second)
|
||||
time.Sleep(wp.getMaxIdleWorkerDuration())
|
||||
}
|
||||
wp.clean(&scratch)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -82,9 +84,16 @@ func (wp *workerPool) Stop() {
|
||||
wp.lock.Unlock()
|
||||
}
|
||||
|
||||
const maxIdleWorkerDuration = 60 * time.Second
|
||||
func (wp *workerPool) getMaxIdleWorkerDuration() time.Duration {
|
||||
if wp.MaxIdleWorkerDuration <= 0 {
|
||||
return 10 * time.Second
|
||||
}
|
||||
return wp.MaxIdleWorkerDuration
|
||||
}
|
||||
|
||||
func (wp *workerPool) clean(scratch *[]*workerChan) {
|
||||
maxIdleWorkerDuration := wp.getMaxIdleWorkerDuration()
|
||||
|
||||
// Clean least recently used workers if they didn't serve connections
|
||||
// for more than maxIdleWorkerDuration.
|
||||
currentTime := time.Now()
|
||||
|
||||
+3
-2
@@ -204,8 +204,9 @@ func testWorkerPoolPanicErrorMulti(t *testing.T) {
|
||||
}
|
||||
return nil
|
||||
},
|
||||
MaxWorkersCount: 1000,
|
||||
Logger: &customLogger{},
|
||||
MaxWorkersCount: 1000,
|
||||
MaxIdleWorkerDuration: time.Millisecond,
|
||||
Logger: &customLogger{},
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
|
||||
Reference in New Issue
Block a user