-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerpool.go
81 lines (69 loc) · 1.61 KB
/
workerpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package workerpool
import (
"sync"
)
type CallbackFunc func(workerID int, job Job) Result
// Job represents the job to be run
type Job struct {
ID int
Data interface{}
}
// Result represents the result of a job
type Result struct {
JobID int
Data interface{}
Error error
}
// WorkerPool struct to manage workers and jobs
type WorkerPool struct {
numWorkers int
jobs chan Job
results chan Result
wg sync.WaitGroup
callback CallbackFunc
done chan struct{}
}
// New creates a new WorkerPool
func New(numWorkers, numJobs int, callbackFunc CallbackFunc) *WorkerPool {
return &WorkerPool{
numWorkers: numWorkers,
jobs: make(chan Job, numJobs),
results: make(chan Result, numJobs),
callback: callbackFunc,
done: make(chan struct{}),
}
}
// Start initializes and runs the worker pool
func (wp *WorkerPool) Start() {
for w := 1; w <= wp.numWorkers; w++ {
wp.wg.Add(1)
go wp.worker(w)
}
// Goroutine to close results channel after all jobs are done
go func() {
wp.wg.Wait()
close(wp.results)
close(wp.done)
}()
}
// worker function processes jobs from the jobs channel and sends results to the results channel
func (wp *WorkerPool) worker(id int) {
defer wp.wg.Done()
for job := range wp.jobs {
result := wp.callback(id, job)
wp.results <- result
}
}
// AddJob sends a job to the jobs channel
func (wp *WorkerPool) AddJob(job Job) {
wp.jobs <- job
}
// Wait waits for all workers to finish
func (wp *WorkerPool) Wait() {
close(wp.jobs)
<-wp.done
}
// Results returns the results channel
func (wp *WorkerPool) Results() <-chan Result {
return wp.results
}