forked from shettyh/threadpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
48 lines (43 loc) · 1.13 KB
/
worker.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
package threadpool
// Worker type holds the job channel and passed worker threadpool
type Worker struct {
jobChannel chan interface{}
workerPool chan chan interface{}
closeHandle chan bool
}
// NewWorker creates the new worker
func NewWorker(workerPool chan chan interface{}, closeHandle chan bool) *Worker {
return &Worker{workerPool: workerPool, jobChannel: make(chan interface{}), closeHandle: closeHandle}
}
// Start starts the worker by listening to the job channel
func (w Worker) Start() {
go func() {
for {
// Put the worker to the worker threadpool
w.workerPool <- w.jobChannel
select {
// Wait for the job
case job := <-w.jobChannel:
// Got the job
w.executeJob(job)
case <-w.closeHandle:
// Exit the go routine when the closeHandle channel is closed
return
}
}
}()
}
// executeJob executes the job based on the type
func (w Worker) executeJob(job interface{}) {
// Execute the job based on the task type
switch task := job.(type) {
case Runnable:
task.Run()
break
case callableTask:
response := task.Task.Call()
task.Handle.done = true
task.Handle.response <- response
break
}
}