Skip to content

Commit edbcbe4

Browse files
committed
update min Go version to v1.22
With Go v1.22, we can benefit from improved for loops (range over integers) and don't need to care about the closures and goroutines pitfall. Signed-off-by: Robin Hahling <[email protected]>
1 parent d439420 commit edbcbe4

File tree

6 files changed

+14
-16
lines changed

6 files changed

+14
-16
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
build:
1515
strategy:
1616
matrix:
17-
go-version: ["1.15", "1.19", "1.20"]
17+
go-version: ["1.22", "1.23", "1.24"]
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Install Go

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func IsPrime(n int64) bool {
5353
func main() {
5454
wp := workerpool.New(runtime.NumCPU())
5555
for i, n := 0, int64(1_000_000_000_000_000_000); n < 1_000_000_000_000_000_100; i, n = i+1, n+1 {
56-
n := n // https://golang.org/doc/faq#closures_and_goroutines
5756
id := fmt.Sprintf("task #%d", i)
5857
// Use Submit to submit tasks for processing. Submit blocks when no
5958
// worker is available to pick up the task.

example_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func IsPrime(n int64) bool {
2828
func Example() {
2929
wp := workerpool.New(runtime.NumCPU())
3030
for i, n := 0, int64(1_000_000_000_000_000_000); n < 1_000_000_000_000_000_100; i, n = i+1, n+1 {
31-
n := n // https://golang.org/doc/faq#closures_and_goroutines
3231
id := fmt.Sprintf("task #%d", i)
3332
// Use Submit to submit tasks for processing. Submit blocks when no
3433
// worker is available to pick up the task.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/cilium/workerpool
22

3-
go 1.15
3+
go 1.22

workerpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (wp *WorkerPool) Close() error {
171171
// only be called once during the lifetime of a WorkerPool.
172172
func (wp *WorkerPool) run(ctx context.Context) {
173173
for t := range wp.tasks {
174-
t := t
174+
175175
result := taskResult{id: t.id}
176176
wp.results = append(wp.results, &result)
177177
wp.workers <- struct{}{}

workerpool_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestWorkerPoolConcurrentTasksCount(t *testing.T) {
120120
// working is written to by each task as soon as possible.
121121
working := make(chan struct{})
122122
// NOTE: schedule one more task than we have workers, hence n+1.
123-
for i := 0; i < n+1; i++ {
123+
for i := range n + 1 {
124124
id := fmt.Sprintf("task #%2d", i)
125125
err := wp.Submit(id, func(ctx context.Context) error {
126126
select {
@@ -137,7 +137,7 @@ func TestWorkerPoolConcurrentTasksCount(t *testing.T) {
137137
}
138138

139139
// ensure that n workers are busy.
140-
for i := 0; i < n; i++ {
140+
for i := range n {
141141
select {
142142
case <-working:
143143
case <-time.After(100 * time.Millisecond):
@@ -164,7 +164,7 @@ func TestWorkerPool(t *testing.T) {
164164
working := make(chan struct{})
165165
var wg sync.WaitGroup
166166
wg.Add(numTasks - 1)
167-
for i := 0; i < numTasks-1; i++ {
167+
for i := range numTasks - 1 {
168168
id := fmt.Sprintf("task #%2d", i)
169169
err := wp.Submit(id, func(_ context.Context) error {
170170
defer wg.Done()
@@ -178,7 +178,7 @@ func TestWorkerPool(t *testing.T) {
178178
}
179179

180180
// ensure n workers are busy
181-
for i := 0; i < n; i++ {
181+
for range n {
182182
<-working
183183
}
184184

@@ -231,7 +231,7 @@ func TestWorkerPool(t *testing.T) {
231231

232232
<-ready
233233
// un-block the worker routines
234-
for i := 0; i < numTasks-1; i++ {
234+
for range numTasks - 1 {
235235
<-done
236236
}
237237
// The last task was blocked in wp.run() and not yet scheduled on a worker.
@@ -254,7 +254,7 @@ func TestConcurrentDrain(t *testing.T) {
254254
done := make(chan struct{})
255255
var wg sync.WaitGroup
256256
wg.Add(numTasks)
257-
for i := 0; i < numTasks; i++ {
257+
for i := range numTasks {
258258
id := fmt.Sprintf("task #%2d", i)
259259
err := wp.Submit(id, func(_ context.Context) error {
260260
defer wg.Done()
@@ -306,7 +306,7 @@ func TestConcurrentDrain(t *testing.T) {
306306
}
307307

308308
// un-block the worker routines to allow the test to complete
309-
for i := 0; i < numTasks; i++ {
309+
for range numTasks {
310310
<-done
311311
}
312312

@@ -402,7 +402,7 @@ func TestWorkerPoolClose(t *testing.T) {
402402
working := make(chan struct{})
403403
var wg sync.WaitGroup
404404
wg.Add(n)
405-
for i := 0; i < n; i++ {
405+
for i := range n {
406406
id := fmt.Sprintf("task #%2d", i)
407407
err := wp.Submit(id, func(ctx context.Context) error {
408408
working <- struct{}{}
@@ -416,7 +416,7 @@ func TestWorkerPoolClose(t *testing.T) {
416416
}
417417

418418
// ensure n workers are busy
419-
for i := 0; i < n; i++ {
419+
for range n {
420420
<-working
421421
}
422422

@@ -436,7 +436,7 @@ func TestWorkerPoolNewWithContext(t *testing.T) {
436436
var wg sync.WaitGroup
437437
// Create n tasks waiting on the context to be cancelled.
438438
wg.Add(n)
439-
for i := 0; i < n; i++ {
439+
for i := range n {
440440
id := fmt.Sprintf("task #%2d", i)
441441
err := wp.Submit(id, func(ctx context.Context) error {
442442
working <- struct{}{}
@@ -450,7 +450,7 @@ func TestWorkerPoolNewWithContext(t *testing.T) {
450450
}
451451

452452
// ensure n workers are busy
453-
for i := 0; i < n; i++ {
453+
for range n {
454454
<-working
455455
}
456456

0 commit comments

Comments
 (0)