Skip to content

Commit d052982

Browse files
committed
Replace goprocess with context
- context.Context and context.AfterFunc provide all functionality previously provided by goprocess - Close functions do not need to return error
1 parent 0dab3e6 commit d052982

File tree

8 files changed

+72
-121
lines changed

8 files changed

+72
-121
lines changed

keytransform/keytransform.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)
9494
}
9595
return r, true
9696
},
97-
Close: func() error {
98-
return cqr.Close()
97+
Close: func() {
98+
cqr.Close()
9999
},
100100
})
101101
return dsq.NaiveQueryApply(nq, qr), nil

mount/mount.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,8 @@ func (qr *queryResults) advance() bool {
101101
qr.next = query.Result{}
102102
r, more := qr.results.NextSync()
103103
if !more {
104-
err := qr.results.Close()
104+
qr.results.Close()
105105
qr.results = nil
106-
if err != nil {
107-
// One more result, the error.
108-
qr.next = query.Result{Error: err}
109-
return true
110-
}
111106
return false
112107
}
113108

@@ -145,19 +140,11 @@ func (h *querySet) Pop() interface{} {
145140
return last
146141
}
147142

148-
func (h *querySet) close() error {
149-
var errs []error
143+
func (h *querySet) close() {
150144
for _, qr := range h.heads {
151-
err := qr.results.Close()
152-
if err != nil {
153-
errs = append(errs, err)
154-
}
145+
qr.results.Close()
155146
}
156147
h.heads = nil
157-
if len(errs) > 0 {
158-
return errs[0]
159-
}
160-
return nil
161148
}
162149

163150
func (h *querySet) addResults(mount ds.Key, results query.Results) {
@@ -339,7 +326,7 @@ func (d *Datastore) Query(ctx context.Context, master query.Query) (query.Result
339326
results, err := dstore.Query(ctx, qi)
340327

341328
if err != nil {
342-
_ = queries.close()
329+
queries.close()
343330
return nil, err
344331
}
345332
queries.addResults(mount, results)

mount/mount_test.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,7 @@ func TestQuerySimple(t *testing.T) {
263263
t.Errorf("did not see wanted key %q in %+v", myKey, entries)
264264
}
265265

266-
err = res.Close()
267-
if err != nil {
268-
t.Errorf("result.Close failed %d", err)
269-
}
266+
res.Close()
270267
}
271268

272269
func TestQueryAcrossMounts(t *testing.T) {
@@ -307,10 +304,7 @@ func TestQueryAcrossMounts(t *testing.T) {
307304
}
308305
entries, err := res.Rest()
309306
if err != nil {
310-
err = res.Close()
311-
if err != nil {
312-
t.Errorf("result.Close failed %d", err)
313-
}
307+
res.Close()
314308
t.Fatalf("Query Results.Rest fail: %v\n", err)
315309
}
316310
if len(entries) != len(values) {
@@ -407,10 +401,7 @@ func TestQueryAcrossMountsWithSort(t *testing.T) {
407401
}
408402
}
409403

410-
err = res.Close()
411-
if err != nil {
412-
t.Errorf("result.Close failed %d", err)
413-
}
404+
res.Close()
414405
}
415406

416407
func TestQueryLimitAcrossMountsWithSort(t *testing.T) {
@@ -473,10 +464,7 @@ func TestQueryLimitAcrossMountsWithSort(t *testing.T) {
473464
}
474465
}
475466

476-
err = res.Close()
477-
if err != nil {
478-
t.Errorf("result.Close failed %d", err)
479-
}
467+
res.Close()
480468
}
481469

482470
func TestQueryLimitAndOffsetAcrossMountsWithSort(t *testing.T) {
@@ -540,10 +528,7 @@ func TestQueryLimitAndOffsetAcrossMountsWithSort(t *testing.T) {
540528
}
541529
}
542530

543-
err = res.Close()
544-
if err != nil {
545-
t.Errorf("result.Close failed %d", err)
546-
}
531+
res.Close()
547532
}
548533

549534
func TestQueryFilterAcrossMountsWithSort(t *testing.T) {
@@ -606,10 +591,7 @@ func TestQueryFilterAcrossMountsWithSort(t *testing.T) {
606591
}
607592
}
608593

609-
err = res.Close()
610-
if err != nil {
611-
t.Errorf("result.Close failed %d", err)
612-
}
594+
res.Close()
613595
}
614596

615597
func TestQueryLimitAndOffsetWithNoData(t *testing.T) {
@@ -639,10 +621,7 @@ func TestQueryLimitAndOffsetWithNoData(t *testing.T) {
639621
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
640622
}
641623

642-
err = res.Close()
643-
if err != nil {
644-
t.Errorf("result.Close failed %d", err)
645-
}
624+
res.Close()
646625
}
647626

648627
func TestQueryLimitWithNotEnoughData(t *testing.T) {
@@ -682,10 +661,7 @@ func TestQueryLimitWithNotEnoughData(t *testing.T) {
682661
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
683662
}
684663

685-
err = res.Close()
686-
if err != nil {
687-
t.Errorf("result.Close failed %d", err)
688-
}
664+
res.Close()
689665
}
690666

691667
func TestQueryOffsetWithNotEnoughData(t *testing.T) {
@@ -722,10 +698,7 @@ func TestQueryOffsetWithNotEnoughData(t *testing.T) {
722698
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
723699
}
724700

725-
err = res.Close()
726-
if err != nil {
727-
t.Errorf("result.Close failed %d", err)
728-
}
701+
res.Close()
729702
}
730703

731704
func TestLookupPrio(t *testing.T) {

namespace/namespace_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ func (ks *DSSuite) TestQuery(c *C) {
119119
c.Check(string(ent.Value), Equals, string(expect[i].Value))
120120
}
121121

122-
err = qres.Close()
123-
c.Check(err, Equals, nil)
122+
qres.Close()
124123

125124
qres, err = nsds.Query(ctx, dsq.Query{Prefix: "bar"})
126125
c.Check(err, Equals, nil)

query/query.go

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package query
22

33
import (
4+
"context"
45
"fmt"
56
"time"
6-
7-
goprocess "github.com/jbenet/goprocess"
87
)
98

109
/*
@@ -149,14 +148,16 @@ type Results interface {
149148
Next() <-chan Result // returns a channel to wait for the next result
150149
NextSync() (Result, bool) // blocks and waits to return the next result, second parameter returns false when results are exhausted
151150
Rest() ([]Entry, error) // waits till processing finishes, returns all entries at once.
152-
Close() error // client may call Close to signal early exit
151+
Close() // client may call Close to signal early exit
153152
}
154153

155154
// results implements Results
156155
type results struct {
157156
query Query
158-
proc goprocess.Process
159157
res <-chan Result
158+
159+
ctx context.Context
160+
cancel context.CancelFunc
160161
}
161162

162163
func (r *results) Next() <-chan Result {
@@ -176,12 +177,12 @@ func (r *results) Rest() ([]Entry, error) {
176177
}
177178
es = append(es, e.Entry)
178179
}
179-
<-r.proc.Closed() // wait till the processing finishes.
180+
<-r.ctx.Done() // wait till the processing finishes.
180181
return es, nil
181182
}
182183

183-
func (r *results) Close() error {
184-
return r.proc.Close()
184+
func (r *results) Close() {
185+
r.cancel()
185186
}
186187

187188
func (r *results) Query() Query {
@@ -199,17 +200,21 @@ func (r *results) Query() Query {
199200
// - datastores must respect <-Process.Closing(), which intermediates
200201
// an early close signal from the client.
201202
type ResultBuilder struct {
202-
Query Query
203-
process goprocess.Process
204-
Output chan Result
203+
Query Query
204+
Output chan Result
205+
206+
ctx context.Context
207+
cancel context.CancelFunc
205208
}
206209

207210
// Results returns a Results to to this builder.
208211
func (rb *ResultBuilder) Results() Results {
209212
return &results{
210213
query: rb.Query,
211-
proc: rb.process,
212214
res: rb.Output,
215+
216+
ctx: rb.ctx,
217+
cancel: rb.cancel,
213218
}
214219
}
215220

@@ -225,9 +230,9 @@ func NewResultBuilder(q Query) *ResultBuilder {
225230
Query: q,
226231
Output: make(chan Result, bufSize),
227232
}
228-
b.process = goprocess.WithTeardown(func() error {
233+
b.ctx, b.cancel = context.WithCancel(context.Background())
234+
context.AfterFunc(b.ctx, func() {
229235
close(b.Output)
230-
return nil
231236
})
232237
return b
233238
}
@@ -238,10 +243,11 @@ func NewResultBuilder(q Query) *ResultBuilder {
238243
// DEPRECATED: This iterator is impossible to cancel correctly. Canceling it
239244
// will leave anything trying to write to the result channel hanging.
240245
func ResultsWithChan(q Query, res <-chan Result) Results {
241-
proc := func(worker goprocess.Process, out chan<- Result) {
246+
proc := func(ctx context.Context, cancel context.CancelFunc, out chan<- Result) {
247+
defer cancel()
242248
for {
243249
select {
244-
case <-worker.Closing(): // client told us to close early
250+
case <-ctx.Done(): // client told us to close early
245251
return
246252
case e, more := <-res:
247253
if !more {
@@ -250,7 +256,7 @@ func ResultsWithChan(q Query, res <-chan Result) Results {
250256

251257
select {
252258
case out <- e:
253-
case <-worker.Closing(): // client told us to close early
259+
case <-ctx.Done(): // client told us to close early
254260
return
255261
}
256262
}
@@ -260,11 +266,8 @@ func ResultsWithChan(q Query, res <-chan Result) Results {
260266
b := NewResultBuilder(q)
261267

262268
// go consume all the entries and add them to the results.
263-
b.process.Go(func(worker goprocess.Process) {
264-
proc(worker, b.Output)
265-
})
269+
go proc(b.ctx, b.cancel, b.Output)
266270

267-
go b.process.CloseAfterChildren() //nolint
268271
return b.Results()
269272
}
270273

@@ -287,12 +290,12 @@ func ResultsReplaceQuery(r Results, q Query) Results {
287290
switch r := r.(type) {
288291
case *results:
289292
// note: not using field names to make sure all fields are copied
290-
return &results{q, r.proc, r.res}
293+
return &results{q, r.res, r.ctx, r.cancel}
291294
case *resultsIter:
292295
// note: not using field names to make sure all fields are copied
293296
lr := r.legacyResults
294297
if lr != nil {
295-
lr = &results{q, lr.proc, lr.res}
298+
lr = &results{q, lr.res, lr.ctx, lr.cancel}
296299
}
297300
return &resultsIter{q, r.next, r.close, lr}
298301
default:
@@ -316,19 +319,17 @@ func ResultsFromIterator(q Query, iter Iterator) Results {
316319
}
317320
}
318321

319-
func noopClose() error {
320-
return nil
321-
}
322+
func noopClose() {}
322323

323324
type Iterator struct {
324325
Next func() (Result, bool)
325-
Close func() error // note: might be called more than once
326+
Close func() // note: might be called more than once
326327
}
327328

328329
type resultsIter struct {
329330
query Query
330331
next func() (Result, bool)
331-
close func() error
332+
close func()
332333
legacyResults *results
333334
}
334335

@@ -340,13 +341,12 @@ func (r *resultsIter) Next() <-chan Result {
340341
func (r *resultsIter) NextSync() (Result, bool) {
341342
if r.legacyResults != nil {
342343
return r.legacyResults.NextSync()
343-
} else {
344-
res, ok := r.next()
345-
if !ok {
346-
r.close()
347-
}
348-
return res, ok
349344
}
345+
res, ok := r.next()
346+
if !ok {
347+
r.close()
348+
}
349+
return res, ok
350350
}
351351

352352
func (r *resultsIter) Rest() ([]Entry, error) {
@@ -364,11 +364,11 @@ func (r *resultsIter) Rest() ([]Entry, error) {
364364
return es, nil
365365
}
366366

367-
func (r *resultsIter) Close() error {
367+
func (r *resultsIter) Close() {
368368
if r.legacyResults != nil {
369-
return r.legacyResults.Close()
369+
r.legacyResults.Close()
370370
} else {
371-
return r.close()
371+
r.close()
372372
}
373373
}
374374

@@ -384,22 +384,21 @@ func (r *resultsIter) useLegacyResults() {
384384
b := NewResultBuilder(r.query)
385385

386386
// go consume all the entries and add them to the results.
387-
b.process.Go(func(worker goprocess.Process) {
387+
go func(ctx context.Context, cancel context.CancelFunc, out chan<- Result) {
388+
defer cancel()
388389
defer r.close()
389390
for {
390391
e, ok := r.next()
391392
if !ok {
392-
break
393+
return
393394
}
394395
select {
395-
case b.Output <- e:
396-
case <-worker.Closing(): // client told us to close early
396+
case out <- e:
397+
case <-ctx.Done(): // client told us to close early
397398
return
398399
}
399400
}
400-
})
401-
402-
go b.process.CloseAfterChildren() //nolint
401+
}(b.ctx, b.cancel, b.Output)
403402

404403
r.legacyResults = b.Results().(*results)
405404
}

0 commit comments

Comments
 (0)