@@ -46,73 +46,66 @@ func Process(inputChan <-chan *OrderedInput, wf WorkFunction, options *Options)
46
46
// Go routine to print data in order
47
47
go func () {
48
48
var current uint64
49
- outputMap := make (map [uint64 ]* processInput )
50
- for {
51
- select {
52
- case item , ok := <- aggregatorChan :
53
- if ok {
54
- if item .order != current {
55
- outputMap [item .order ] = item
56
- continue
57
- }
58
- for {
59
- if item == nil {
60
- break
61
- }
62
- outputChan <- & OrderedOutput {Value : item .value }
63
- item .wg .Done ()
64
- delete (outputMap , current )
65
- current ++
66
- item = outputMap [current ]
67
- }
68
- } else {
69
- aggregatorChan = nil
70
- }
49
+ outputMap := make (map [uint64 ]* processInput , options .PoolSize )
50
+ defer func () {
51
+ close (outputChan )
52
+ doneSemaphoreChan <- true
53
+ }()
54
+ for item := range aggregatorChan {
55
+ if item .order != current {
56
+ outputMap [item .order ] = item
57
+ continue
71
58
}
72
- if aggregatorChan == nil {
73
- close (outputChan )
74
- doneSemaphoreChan <- true
59
+ for {
60
+ if item == nil {
61
+ break
62
+ }
63
+ outputChan <- & OrderedOutput {Value : item .value }
64
+ item .wg .Done ()
65
+ delete (outputMap , current )
66
+ current ++
67
+ item = outputMap [current ]
75
68
}
76
69
}
77
70
}()
78
71
79
- inputClosedSemaphoreChan := make (chan bool )
72
+ poolWg := sync.WaitGroup {}
73
+ poolWg .Add (processors )
80
74
// Create a goroutine pool
81
75
for i := 0 ; i < processors ; i ++ {
82
- go func () {
76
+ go func (worker int ) {
77
+ defer func () {
78
+ poolWg .Done ()
79
+ }()
83
80
for input := range processChan {
84
81
wg .Add (1 )
85
82
input .value = wf (input .value )
86
83
input .wg = & wg
87
84
aggregatorChan <- input
88
- select {
89
- case <- inputClosedSemaphoreChan :
90
- wg .Wait ()
91
- close (aggregatorChan )
92
- default :
93
- continue
94
- }
95
85
}
96
- }()
86
+ }(i )
97
87
}
98
88
99
- var order uint64
100
- for {
101
- select {
102
- case input , ok := <- inputChan :
103
- if ok {
104
- processChan <- & processInput {input .Value , order , nil }
105
- order ++
106
- } else {
107
- inputChan = nil
89
+ go func () {
90
+ poolWg .Wait ()
91
+ close (aggregatorChan )
92
+ }()
93
+
94
+ go func () {
95
+ var order uint64
96
+ for {
97
+ select {
98
+ case input , ok := <- inputChan :
99
+ if ok {
100
+ processChan <- & processInput {input .Value , order , nil }
101
+ order ++
102
+ } else {
103
+ close (processChan )
104
+ return
105
+ }
108
106
}
109
107
}
110
- if inputChan == nil {
111
- close (processChan )
112
- inputClosedSemaphoreChan <- true
113
- break
114
- }
115
- }
108
+ }()
116
109
<- doneSemaphoreChan
117
110
}()
118
111
return outputChan
0 commit comments