-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_manager.go
60 lines (46 loc) · 1.22 KB
/
job_manager.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
package maistry
import (
"time"
)
type JobManager struct {
// A pool of workers channels that are registered with the dispatcher
allocatedWorkerChan chan jobChannel
// Used to throttle
throttler <-chan time.Time
// To communicate from PushJob() To Run()
jobChan jobChannel
MaxJps int
name string
}
func (jm *JobManager) Run() {
go func() {
for {
// Throttle the loop according to input Jps
<-jm.throttler
// Wait for a job from PushJob()
job := <-jm.jobChan
// Get a worker (type JobChannel) from Worker Pool
worker := <-jm.allocatedWorkerChan
// Push the job to job channel
worker <- job
}
}()
}
// PushJob - blocking call to JobManager
func (jm *JobManager) PushJob(j Job) {
jm.jobChan <- j
}
// NewJobManager - jps (Jobs per second) - JobManager will throttle job execution according if it crosses maxJps
func NewJobManager(maxJps int, name string) *JobManager {
throttler := time.Tick(time.Duration(int(1000/maxJps)) * time.Millisecond)
return &JobManager{
// Zero size WorkerPool
allocatedWorkerChan: make(chan jobChannel),
// To throttle
throttler: throttler,
// For communicating from PushJob() To Run()
jobChan: make(jobChannel),
MaxJps: maxJps,
name: name,
}
}