Skip to content

Commit 919c9b1

Browse files
committed
separate query closing and query closed signals
1 parent 682b39f commit 919c9b1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

query/query.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ type results struct {
158158
query Query
159159
res <-chan Result
160160

161-
ctx context.Context
162161
cancel context.CancelFunc
162+
closed chan struct{}
163163
}
164164

165165
func (r *results) Next() <-chan Result {
@@ -179,7 +179,7 @@ func (r *results) Rest() ([]Entry, error) {
179179
}
180180
es = append(es, e.Entry)
181181
}
182-
<-r.ctx.Done() // wait till the processing finishes.
182+
<-r.Done() // wait till the processing finishes.
183183
return es, nil
184184
}
185185

@@ -192,7 +192,7 @@ func (r *results) Query() Query {
192192
}
193193

194194
func (r *results) Done() <-chan struct{} {
195-
return r.ctx.Done()
195+
return r.closed
196196
}
197197

198198
// ResultBuilder is what implementors use to construct results
@@ -211,6 +211,7 @@ type ResultBuilder struct {
211211

212212
ctx context.Context
213213
cancel context.CancelFunc
214+
closed chan struct{}
214215
wg sync.WaitGroup
215216
}
216217

@@ -220,8 +221,8 @@ func (rb *ResultBuilder) Results() Results {
220221
query: rb.Query,
221222
res: rb.Output,
222223

223-
ctx: rb.ctx,
224224
cancel: rb.cancel,
225+
closed: rb.closed,
225226
}
226227
}
227228

@@ -236,11 +237,14 @@ func NewResultBuilder(q Query) *ResultBuilder {
236237
b := &ResultBuilder{
237238
Query: q,
238239
Output: make(chan Result, bufSize),
240+
closed: make(chan struct{}),
239241
}
242+
240243
b.ctx, b.cancel = context.WithCancel(context.Background())
241244
context.AfterFunc(b.ctx, func() {
242245
b.wg.Wait()
243246
close(b.Output)
247+
close(b.closed)
244248
})
245249
return b
246250
}
@@ -307,12 +311,12 @@ func ResultsReplaceQuery(r Results, q Query) Results {
307311
switch r := r.(type) {
308312
case *results:
309313
// note: not using field names to make sure all fields are copied
310-
return &results{q, r.res, r.ctx, r.cancel}
314+
return &results{q, r.res, r.cancel, r.closed}
311315
case *resultsIter:
312316
// note: not using field names to make sure all fields are copied
313317
lr := r.legacyResults
314318
if lr != nil {
315-
lr = &results{q, lr.res, lr.ctx, lr.cancel}
319+
lr = &results{q, lr.res, lr.cancel, lr.closed}
316320
}
317321
return &resultsIter{q, r.next, r.close, lr}
318322
default:

0 commit comments

Comments
 (0)