Skip to content

Commit 506417d

Browse files
committed
Make sure generateWorkerWithMaximum(maximum) to avoid over generating workers.
1 parent e1772bd commit 506417d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

worker/pool.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ type DefaultWorkerPool struct {
5353

5454
jobQueue *fpgo.BufferedChannelQueue[func()]
5555

56-
workerCount int
57-
spawnWorkerCh fpgo.ChannelQueue[int]
58-
lastAccessTime time.Time
56+
workerCount int
57+
spawnWorkerCh fpgo.ChannelQueue[int]
58+
lastAccessTime time.Time
5959

6060
// Settings
6161

@@ -119,15 +119,15 @@ func (workerPoolSelf *DefaultWorkerPool) trySpawn() {
119119

120120
if workerPoolSelf.workerCount < expectedWorkerCount {
121121
for i := workerPoolSelf.workerCount; i < expectedWorkerCount; i++ {
122-
workerPoolSelf.generateWorker()
122+
workerPoolSelf.generateWorkerWithMaximum(expectedWorkerCount)
123123
}
124124
}
125125
}
126126

127127
// PreAllocWorkerSize PreAllocate Workers
128128
func (workerPoolSelf *DefaultWorkerPool) PreAllocWorkerSize(preAllocWorkerSize int) {
129129
for i := workerPoolSelf.workerCount; i < preAllocWorkerSize; i++ {
130-
workerPoolSelf.generateWorker()
130+
workerPoolSelf.generateWorkerWithMaximum(preAllocWorkerSize)
131131
}
132132
}
133133

@@ -155,13 +155,17 @@ func (workerPoolSelf *DefaultWorkerPool) notifyWorkers() {
155155
}
156156
}
157157

158-
func (workerPoolSelf *DefaultWorkerPool) generateWorker() {
158+
func (workerPoolSelf *DefaultWorkerPool) generateWorkerWithMaximum(maximum int) {
159159
// Initial
160-
workerID := time.Now()
161-
workerPoolSelf.lastAccessTime = workerID
162160
workerPoolSelf.lock.Lock()
161+
defer workerPoolSelf.lock.Unlock()
162+
if workerPoolSelf.workerCount >= maximum ||
163+
workerPoolSelf.workerCount >= workerPoolSelf.workerSizeMaximum {
164+
return
165+
}
166+
// workerID := time.Now()
167+
workerPoolSelf.lastAccessTime = time.Now()
163168
workerPoolSelf.workerCount++
164-
workerPoolSelf.lock.Unlock()
165169

166170
go func() {
167171
// Recover & Recycle

0 commit comments

Comments
 (0)