Skip to content

Commit

Permalink
Update dynamic worker adjustment logic and documentation
Browse files Browse the repository at this point in the history
- Corrected the logic for adjusting the number of workers in the pool. Now, when the task queue is empty and the number of workers is more than the minimum, the number of workers is halved until it reaches the minimum.
- Updated the README to reflect the changes in the worker adjustment logic. The documentation now correctly describes the conditions under which the number of workers is increased or decreased.
- Removed the unused 'max' function from the code.
  • Loading branch information
daniel-hutao committed Sep 11, 2023
1 parent 0af6042 commit 4653dd3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func main() {
}
```


## Dynamic Worker Adjustment

GoPool supports dynamic worker adjustment. This means that the number of workers in the pool can increase or decrease based on the number of tasks in the queue. This feature can be enabled by setting the MinWorkers option when creating the pool.
Expand Down Expand Up @@ -230,7 +231,7 @@ func main() {
}
```

In this example, the pool starts with 50 workers. If the number of tasks in the queue exceeds (MaxWorkers - MinWorkers) / 2 + MinWorkers, the pool will add more workers. If the number of tasks in the queue is less than MinWorkers, the pool will remove some workers.
In this example, the pool starts with 50 workers. If the number of tasks in the queue exceeds 3/4 of the current number of workers and the current number of workers is less than MaxWorkers, the pool will double the number of workers until it reaches MaxWorkers. If the number of tasks in the queue is zero and the current number of workers is more than MinWorkers, the pool will halve the number of workers until it reaches MinWorkers.

## Task Timeout Handling

Expand Down
9 changes: 1 addition & 8 deletions gopool.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (p *goPool) adjustWorkers() {
}
} else if len(p.taskQueue) == 0 && len(p.workers) > p.minWorkers {
// Halve the number of workers until it reaches the minimum
removeWorkers := max((len(p.workers)-p.minWorkers)/2, p.minWorkers)
removeWorkers := (len(p.workers) - p.minWorkers + 1) / 2
p.workers = p.workers[:len(p.workers)-removeWorkers]
p.workerStack = p.workerStack[:len(p.workerStack)-removeWorkers]
}
Expand Down Expand Up @@ -199,13 +199,6 @@ func min(a, b int) int {
return b
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

// Running returns the number of workers that are currently working.
func (p *goPool) Running() int {
p.lock.Lock()
Expand Down

0 comments on commit 4653dd3

Please sign in to comment.