Skip to content

Commit 36156ae

Browse files
authored
Merge pull request #2 from alesr/remove-task-type-call-do-directly
Remove Task type with pool now calling Do directly
2 parents 99d863e + 62e7fbe commit 36156ae

4 files changed

Lines changed: 116 additions & 90 deletions

File tree

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ jobs:
6363
- name: Run go vet
6464
run: go vet ./...
6565

66+
lint:
67+
name: Lint
68+
needs: deps
69+
runs-on: ubuntu-latest
70+
steps:
71+
- uses: actions/checkout@v4
72+
73+
- uses: actions/setup-go@v5
74+
with:
75+
go-version-file: go.mod
76+
cache: true
77+
78+
- name: Install staticcheck
79+
run: go install honnef.co/go/tools/cmd/staticcheck@latest
80+
81+
- name: Run staticcheck
82+
run: staticcheck ./...
83+
6684
test:
6785
name: Test
6886
needs: deps

example_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,14 @@ func Example() {
2929

3030
var bodyCount atomic.Int32
3131

32-
// list of tasks to perform
3332
bazookas := []bazooka{
3433
{ammo: 69, targetID: "foo-id", bodyCount: &bodyCount},
3534
{ammo: 42, targetID: "bar-id", bodyCount: &bodyCount},
3635
{ammo: 11, targetID: "qux-id", bodyCount: &bodyCount},
3736
}
3837

39-
// loop over the tasks initializing and
40-
// submitting the tasks to the pool
41-
4238
for _, bazz := range bazookas {
43-
task := workerpool.Task[*bazooka]{
44-
Fn: func(input *bazooka) {
45-
input.Do(context.TODO())
46-
},
47-
Input: &bazz,
48-
}
49-
pool.Submit(task)
39+
pool.Submit(&bazz)
5040
}
5141

5242
if err := pool.GracefulShutdown(); err != nil {

workerpool.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,24 @@ import (
77
"sync/atomic"
88
)
99

10-
// Input wraps a task's execution.
11-
type Input interface {
10+
// Task defines the interface for a task to be executed by a worker pool.
11+
type Task interface {
1212
Do(context.Context)
1313
}
1414

15-
// Task bundles a function with its input.
16-
type Task[T Input] struct {
17-
Fn func(T)
18-
Input T
19-
}
20-
2115
// Option configures a Pool.
22-
type Option[T Input] func(*Pool[T])
16+
type Option[T Task] func(*Pool[T])
2317

2418
// WithBuffer sets the task channel buffer size.
25-
func WithBuffer[T Input](size int) Option[T] {
19+
func WithBuffer[T Task](size int) Option[T] {
2620
return func(p *Pool[T]) {
2721
p.buffer = size
2822
}
2923
}
3024

3125
// Pool maintains fixed worker goroutines processing tasks from a channel.
32-
type Pool[T Input] struct {
33-
tasks chan Task[T] // channel for tasks waiting to be processed
26+
type Pool[T Task] struct {
27+
tasks chan T // channel for tasks waiting to be processed
3428
buffer int // size of the task channel
3529
wg sync.WaitGroup // wait group for worker goroutines
3630

@@ -47,7 +41,7 @@ type Pool[T Input] struct {
4741
// New creates a pool with numOfWorkers workers.
4842
// The context can be used to stop the pool immediately, skipping any buffered
4943
// tasks. In-flight tasks will still run to completion.
50-
func New[T Input](ctx context.Context, numOfWorkers int, opts ...Option[T]) *Pool[T] {
44+
func New[T Task](ctx context.Context, numOfWorkers int, opts ...Option[T]) *Pool[T] {
5145
if numOfWorkers <= 0 {
5246
numOfWorkers = 1
5347
}
@@ -64,7 +58,7 @@ func New[T Input](ctx context.Context, numOfWorkers int, opts ...Option[T]) *Poo
6458
opt(p)
6559
}
6660

67-
p.tasks = make(chan Task[T], p.buffer)
61+
p.tasks = make(chan T, p.buffer)
6862

6963
p.wg.Add(numOfWorkers)
7064
for range numOfWorkers {
@@ -86,20 +80,20 @@ func (p *Pool[T]) worker() {
8680
for {
8781
select {
8882
case task := <-p.tasks:
89-
task.Fn(task.Input)
83+
task.Do(p.ctx)
9084
default:
9185
return
9286
}
9387
}
9488
case task := <-p.tasks:
95-
task.Fn(task.Input)
89+
task.Do(p.ctx)
9690
}
9791
}
9892
}
9993

10094
// Submit sends a task to the pool. Blocks if the task channel is full.
10195
// Returns false if the pool is shutting down or the context was cancelled.
102-
func (p *Pool[T]) Submit(task Task[T]) bool {
96+
func (p *Pool[T]) Submit(task T) bool {
10397
select {
10498
case <-p.ctx.Done(): // forcefully terminate via ctx
10599
return false

0 commit comments

Comments
 (0)