@@ -27,14 +27,13 @@ type Job struct {
27
27
28
28
// Stats holds basic stats for the worker
29
29
type Stats struct {
30
- ProcessedJobs int64
30
+ ProcessedJobs uint
31
31
TotalJobsExecutionTime int64 // NOTE: nanoseconds
32
32
}
33
33
34
- // Config holds needed data to start worker pool
35
- type Config struct {
36
- NWorkers int
37
- MaxJobs int64
34
+ // Strategy interface that holds implementation of balancing strategy
35
+ type BalancingStrategy interface {
36
+ NextWorkerId (workersStats []Stats ) int
38
37
}
39
38
40
39
type workerStats struct {
@@ -43,12 +42,36 @@ type workerStats struct {
43
42
}
44
43
45
44
// workerPool holds data needed for pool operation
46
- type workerPool struct {
45
+ type WorkerPool struct {
47
46
mx sync.RWMutex
48
- config Config
47
+ nWorkers uint
49
48
workersChan []chan Job
50
49
workersStats []* workerStats
51
50
terminateChan []chan bool
51
+ balancer BalancingStrategy
52
+ }
53
+
54
+ // NewWorkerPool creates new worker pool
55
+ func NewWorkerPool (nWorkers , maxJobs uint , balancer BalancingStrategy ) * WorkerPool {
56
+ pool := & WorkerPool {
57
+ nWorkers : nWorkers ,
58
+ workersChan : make ([]chan Job , nWorkers ),
59
+ workersStats : make ([]* workerStats , nWorkers ),
60
+ terminateChan : make ([]chan bool , nWorkers ),
61
+ balancer : balancer ,
62
+ }
63
+ var w uint
64
+ for w = 0 ; w < nWorkers ; w ++ {
65
+ pool .workersChan [w ] = make (chan Job , maxJobs )
66
+ pool .terminateChan [w ] = make (chan bool )
67
+ pool .workersStats [w ] = & workerStats {}
68
+ go worker (
69
+ pool .workersChan [w ],
70
+ pool .terminateChan [w ],
71
+ pool .workersStats [w ],
72
+ )
73
+ }
74
+ return pool
52
75
}
53
76
54
77
func worker (jobs chan Job , terminate chan bool , s * workerStats ) {
@@ -74,7 +97,7 @@ func worker(jobs chan Job, terminate chan bool, s *workerStats) {
74
97
}
75
98
76
99
// getWorkerStats by specified worker id
77
- func (wp * workerPool ) getWorkerStats (workerId int ) (Stats , error ) {
100
+ func (wp * WorkerPool ) getWorkerStats (workerId int ) (Stats , error ) {
78
101
resStats := Stats {}
79
102
if workerId > len (wp .workersChan ) || workerId < 0 {
80
103
return resStats , workerIndexIsOutOfBoundsErr
@@ -91,7 +114,7 @@ func (wp *workerPool) getWorkerStats(workerId int) (Stats, error) {
91
114
}
92
115
93
116
// terminateWorker sends termination signal to the specified worker
94
- func (wp * workerPool ) terminateWorker (workerId int ) error {
117
+ func (wp * WorkerPool ) terminateWorker (workerId int ) error {
95
118
if workerId > len (wp .terminateChan ) || workerId < 0 {
96
119
return workerIndexIsOutOfBoundsErr
97
120
}
@@ -102,7 +125,7 @@ func (wp *workerPool) terminateWorker(workerId int) error {
102
125
}
103
126
104
127
// reloadWorker terminates worker by id, and spawns new one
105
- func (wp * workerPool ) reloadWorker (workerId int ) error {
128
+ func (wp * WorkerPool ) reloadWorker (workerId int ) error {
106
129
if workerId > len (wp .workersChan ) || workerId < 0 {
107
130
return workerIndexIsOutOfBoundsErr
108
131
}
@@ -121,57 +144,20 @@ func (wp *workerPool) reloadWorker(workerId int) error {
121
144
return nil
122
145
}
123
146
124
- // Strategy interface that holds implementation of balancing strategy
125
- type BalancingStrategy interface {
126
- NextWorkerId (workersStats []Stats ) int
127
- }
128
-
129
- // Manager spawns workers and schedule jobs
130
- type Manager struct {
131
- wp * workerPool
132
- balancer BalancingStrategy
133
- }
134
-
135
- // New creates new worker pool
136
- func New (config Config , balancer BalancingStrategy ) * Manager {
137
- pool := & workerPool {
138
- config : config ,
139
- workersChan : make ([]chan Job , config .NWorkers ),
140
- workersStats : make ([]* workerStats , config .NWorkers ),
141
- terminateChan : make ([]chan bool , config .NWorkers ),
142
- }
143
- for w := 0 ; w < config .NWorkers ; w ++ {
144
- pool .workersChan [w ] = make (chan Job , config .MaxJobs )
145
- pool .terminateChan [w ] = make (chan bool )
146
- pool .workersStats [w ] = & workerStats {}
147
- go worker (
148
- pool .workersChan [w ],
149
- pool .terminateChan [w ],
150
- pool .workersStats [w ],
151
- )
152
- }
153
- manager := & Manager {
154
- wp : pool ,
155
- balancer : balancer ,
156
- }
157
- // TODO: add routine that reloads workers
158
- return manager
159
- }
160
-
161
147
// ScheduleJob puts job in a queue
162
- func (m * Manager ) ScheduleJob (f JobFunc ) chan Result {
163
- m . wp .mx .RLock ()
164
- config := m . wp .config
165
- workersStats := make ([]Stats , config . NWorkers )
166
- for i , s := range m . wp .workersStats {
148
+ func (wp * WorkerPool ) ScheduleJob (f JobFunc ) chan Result {
149
+ wp .mx .RLock ()
150
+ nWorkers := wp .nWorkers
151
+ workersStats := make ([]Stats , nWorkers )
152
+ for i , s := range wp .workersStats {
167
153
s .mx .RLock ()
168
154
workersStats [i ] = s .Stats
169
155
s .mx .RUnlock ()
170
156
}
171
- m . wp .mx .RUnlock ()
157
+ wp .mx .RUnlock ()
172
158
ch := make (chan Result , 1 )
173
- nextWorkerId := m .balancer .NextWorkerId (workersStats )
174
- m . wp .workersChan [nextWorkerId ] <- Job {f , ch }
159
+ nextWorkerId := wp .balancer .NextWorkerId (workersStats )
160
+ wp .workersChan [nextWorkerId ] <- Job {f , ch }
175
161
return ch
176
162
}
177
163
0 commit comments