@@ -75,12 +75,14 @@ type exchangeRowIter struct {
75
75
parallelism int
76
76
partitions sql.PartitionIter
77
77
tree sql.Node
78
- mut sync.Mutex
79
- tokens chan struct {}
78
+ mut sync.RWMutex
79
+ tokensChan chan struct {}
80
80
started bool
81
81
rows chan sql.Row
82
82
err chan error
83
- quit chan struct {}
83
+
84
+ quitMut sync.RWMutex
85
+ quitChan chan struct {}
84
86
}
85
87
86
88
func newExchangeRowIter (
@@ -97,34 +99,40 @@ func newExchangeRowIter(
97
99
started : false ,
98
100
tree : tree ,
99
101
partitions : iter ,
100
- quit : make (chan struct {}),
102
+ quitChan : make (chan struct {}),
101
103
}
102
104
}
103
105
104
106
func (it * exchangeRowIter ) releaseToken () {
105
107
it .mut .Lock ()
106
108
defer it .mut .Unlock ()
107
109
108
- if it .tokens != nil {
109
- it .tokens <- struct {}{}
110
+ if it .tokensChan != nil {
111
+ it .tokensChan <- struct {}{}
110
112
}
111
113
}
112
114
113
115
func (it * exchangeRowIter ) closeTokens () {
114
116
it .mut .Lock ()
115
117
defer it .mut .Unlock ()
116
118
117
- close (it .tokens )
118
- it .tokens = nil
119
+ close (it .tokensChan )
120
+ it .tokensChan = nil
121
+ }
122
+
123
+ func (it * exchangeRowIter ) tokens () chan struct {} {
124
+ it .mut .RLock ()
125
+ defer it .mut .RUnlock ()
126
+ return it .tokensChan
119
127
}
120
128
121
129
func (it * exchangeRowIter ) fillTokens () {
122
130
it .mut .Lock ()
123
131
defer it .mut .Unlock ()
124
132
125
- it .tokens = make (chan struct {}, it .parallelism )
133
+ it .tokensChan = make (chan struct {}, it .parallelism )
126
134
for i := 0 ; i < it .parallelism ; i ++ {
127
- it .tokens <- struct {}{}
135
+ it .tokensChan <- struct {}{}
128
136
}
129
137
}
130
138
@@ -142,7 +150,7 @@ func (it *exchangeRowIter) start() {
142
150
it .err <- context .Canceled
143
151
it .closeTokens ()
144
152
return
145
- case <- it .quit :
153
+ case <- it .quit () :
146
154
it .closeTokens ()
147
155
return
148
156
case p , ok := <- partitions :
@@ -179,9 +187,9 @@ func (it *exchangeRowIter) iterPartitions(ch chan<- sql.Partition) {
179
187
case <- it .ctx .Done ():
180
188
it .err <- context .Canceled
181
189
return
182
- case <- it .quit :
190
+ case <- it .quit () :
183
191
return
184
- case <- it .tokens :
192
+ case <- it .tokens () :
185
193
}
186
194
187
195
p , err := it .partitions .Next ()
@@ -226,7 +234,7 @@ func (it *exchangeRowIter) iterPartition(p sql.Partition) {
226
234
case <- it .ctx .Done ():
227
235
it .err <- context .Canceled
228
236
return
229
- case <- it .quit :
237
+ case <- it .quit () :
230
238
return
231
239
default :
232
240
}
@@ -263,17 +271,25 @@ func (it *exchangeRowIter) Next() (sql.Row, error) {
263
271
}
264
272
}
265
273
266
- func (it * exchangeRowIter ) Close () (err error ) {
267
- if it .quit != nil {
268
- close (it .quit )
269
- it .quit = nil
274
+ func (it * exchangeRowIter ) quit () chan struct {} {
275
+ it .quitMut .RLock ()
276
+ defer it .quitMut .RUnlock ()
277
+ return it .quitChan
278
+ }
279
+
280
+ func (it * exchangeRowIter ) Close () error {
281
+ it .quitMut .Lock ()
282
+ if it .quitChan != nil {
283
+ close (it .quitChan )
284
+ it .quitChan = nil
270
285
}
286
+ it .quitMut .Unlock ()
271
287
272
288
if it .partitions != nil {
273
- err = it .partitions .Close ()
289
+ return it .partitions .Close ()
274
290
}
275
291
276
- return err
292
+ return nil
277
293
}
278
294
279
295
type exchangePartition struct {
0 commit comments