-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (61 loc) · 1.43 KB
/
main.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
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"sync"
"time"
)
type JobInput struct {
startTime time.Time
endTime time.Time
}
type JobResponse struct {
}
type Response struct {
finalOutput string
}
func startWorker(jobInputChan <-chan JobInput, jobOutputChan chan<- JobResponse, wg *sync.WaitGroup) {
defer wg.Done()
for jobInput := range jobInputChan {
jobOutputChan <- businessFunctionalityJob(jobInput)
}
}
func businessFunctionality(jobInputChan chan<- JobInput) {
jobInputs := []JobInput{JobInput{}, JobInput{}}
for _, jobInput := range jobInputs {
jobInputChan <- jobInput
}
close(jobInputChan)
}
func combineResponses(jobResponses []JobResponse) Response {
return Response{finalOutput: "Well done!!"}
}
func businessFunctionalityJob(jobInput JobInput) JobResponse {
fmt.Println("Executing Job..")
return JobResponse{}
}
func main() {
num_of_workers := 3
jobsChan := make(chan JobInput, 10)
resultsChan := make(chan JobResponse, 10)
wg := sync.WaitGroup{}
for i := 0; i < num_of_workers; i++ {
wg.Add(1)
go startWorker(jobsChan, resultsChan, &wg)
}
go businessFunctionality(jobsChan)
var responses []JobResponse
wgResp := sync.WaitGroup{}
wgResp.Add(1)
go func() {
defer wgResp.Done()
for resp := range resultsChan {
responses = append(responses, resp)
}
}()
wg.Wait()
close(resultsChan)
wgResp.Wait()
fmt.Println(combineResponses(responses).finalOutput)
}