@@ -221,7 +221,7 @@ func (rb *ResultBuilder) Results() Results {
221
221
const NormalBufSize = 1
222
222
const KeysOnlyBufSize = 128
223
223
224
- func NewResultBuilder (q Query ) * ResultBuilder {
224
+ func NewResultBuilder (ctx context. Context , q Query ) * ResultBuilder {
225
225
bufSize := NormalBufSize
226
226
if q .KeysOnly {
227
227
bufSize = KeysOnlyBufSize
@@ -230,7 +230,7 @@ func NewResultBuilder(q Query) *ResultBuilder {
230
230
Query : q ,
231
231
Output : make (chan Result , bufSize ),
232
232
}
233
- b .ctx , b .cancel = context .WithCancel (context . Background () )
233
+ b .ctx , b .cancel = context .WithCancel (ctx )
234
234
context .AfterFunc (b .ctx , func () {
235
235
close (b .Output )
236
236
})
@@ -243,7 +243,7 @@ func NewResultBuilder(q Query) *ResultBuilder {
243
243
// DEPRECATED: This iterator is impossible to cancel correctly. Canceling it
244
244
// will leave anything trying to write to the result channel hanging.
245
245
func ResultsWithChan (q Query , res <- chan Result ) Results {
246
- return ResultsWithContext (q , func (ctx context.Context , out chan <- Result ) {
246
+ return ResultsWithContext (context . Background (), q , func (ctx context.Context , out chan <- Result ) {
247
247
for {
248
248
select {
249
249
case <- ctx .Done (): // client told us to close early
@@ -263,10 +263,12 @@ func ResultsWithChan(q Query, res <-chan Result) Results {
263
263
})
264
264
}
265
265
266
- // ResultsWithCtxs returns a Results object with the results generated by the
267
- // passed proc function called in a separate goroutine.
268
- func ResultsWithContext (q Query , proc func (context.Context , chan <- Result )) Results {
269
- b := NewResultBuilder (q )
266
+ // ResultsWithContext returns a Results object with the results generated by
267
+ // the passed proc function called in a separate goroutine. The context passed
268
+ // into the proc function is cancelled when the returned Results is closed, or
269
+ // when the parent context, ctx, is canceled.
270
+ func ResultsWithContext (ctx context.Context , q Query , proc func (context.Context , chan <- Result )) Results {
271
+ b := NewResultBuilder (ctx , q )
270
272
271
273
go func () {
272
274
defer b .cancel ()
@@ -386,7 +388,7 @@ func (r *resultsIter) useLegacyResults() {
386
388
return
387
389
}
388
390
389
- b := NewResultBuilder (r .query )
391
+ b := NewResultBuilder (context . Background (), r .query )
390
392
391
393
// go consume all the entries and add them to the results.
392
394
go func (ctx context.Context , cancel context.CancelFunc , out chan <- Result ) {
0 commit comments