-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumers.go
204 lines (156 loc) · 3.84 KB
/
consumers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
type Payload struct {
Data string `json:"data"`
UUID string `json:"uuid"`
}
type Job struct {
Payload
}
// PendingJobs is a channel to send jobs todo
type PendingJobs chan Job
// CompletedJobs is a channel of jobs that are done
type CompletedJobs chan Job
// Worker represents the worker that executes the job
type Worker struct {
ID int
PendingJobs
CompletedJobs
}
func NewWorker(ID int, pendingJobs PendingJobs, completedJobs CompletedJobs) Worker {
return Worker{
ID: ID,
PendingJobs: pendingJobs,
CompletedJobs: completedJobs,
}
}
func (w Worker) Start() {
// Find an available jobs to work on
for job := range w.PendingJobs {
// Process Job
fmt.Printf("worker %d got the job: %s \n", w.ID, job.UUID)
// Error handling
// Log error
// Add job back to queue
time.Sleep(time.Second * 1)
// Send completed job
w.CompletedJobs <- job
}
}
func (w Worker) StartWithWithWaitGroup(wg *sync.WaitGroup) {
defer wg.Done()
// Find an available jobs to work on
for job := range w.PendingJobs {
// Process Job
fmt.Printf("worker %d got the job: %s \n", w.ID, job.UUID)
// Error handling
// Log error
// Add job back to queue
time.Sleep(time.Second * 1)
// Send completed job
// w.CompletedJobs <- job
fmt.Printf("job %s ✔ \n", job.UUID)
}
}
func StartFooConsumerWithWaitGroup(numberOfWorkers int, jobs []Job) {
numberOfJobs := len(jobs)
pending := make(chan Job, numberOfJobs)
// sync.WaitGroup used to synchronize the group
// of goroutines that are being executed
var wg sync.WaitGroup
wg.Add(numberOfWorkers)
// Multiple workers
// Pending jobs at this point are still empty
// Each worker will work in 1 Job at a time
for w := 1; w <= numberOfWorkers; w++ {
worker := NewWorker(w, pending, nil)
go worker.StartWithWithWaitGroup(&wg)
}
// Enqueue jobs todo in pending
for _, job := range jobs {
pending <- job
}
// Close pending jobs channel
close(pending)
// Wait until all group be finished
wg.Wait()
// Consumer finishes
fmt.Println("foo consumer finished")
}
func StartFooConsumer(numberOfWorkers int, jobs []Job) {
numberOfJobs := len(jobs)
pending := make(chan Job, numberOfJobs)
completed := make(chan Job, numberOfJobs)
// Multiple workers
// Pending jobs at this point are still empty
// Each worker will work in 1 Job at a time
for w := 1; w <= numberOfWorkers; w++ {
worker := NewWorker(w, pending, completed)
go worker.Start()
}
// Enqueue jobs todo in pending
for _, job := range jobs {
pending <- job
}
// Close pending jobs channel
close(pending)
// Wait for jobs all to be completed
for a := 1; a <= numberOfJobs; a++ {
job := <-completed
fmt.Printf("job %s ✔ \n", job.UUID)
}
// Consumer finishes
fmt.Println("foo consumer finished")
}
func main() {
const numberOfWorkers = 100
const numberOfJobs = 100
// jobs := CollectJobs(numberOfJobs)
// StartFooConsumerWithWaitGroup(numberOfWorkers, jobs)
//
// StartFooConsumerWithWaitGroup(numberOfWorkers, jobs)
//
// // ETL Pipeline // // //
for {
// [1]
// Collect
jobs := CollectJobs(numberOfJobs)
// [2]
// Process
StartFooConsumer(numberOfWorkers, jobs)
// [3]
// Wait new jobs
time.Sleep(time.Second * 3)
}
fmt.Println("main finished")
}
func CollectJobs(amount int) []Job {
jobs := []Job{}
for j := 0; j <= amount; j++ {
jobs = append(jobs, Job{
Payload{
Data: FakeString(20),
UUID: FakeString(10),
},
})
}
return jobs
}
func FakeString(length int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
rand.Seed(time.Now().UnixNano())
b := make([]rune, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func FakeNumber(length int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(length)
}