Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop parallel search after first successful chan closes #12

Merged
merged 2 commits into from
Sep 29, 2018

Conversation

magik6k
Copy link
Contributor

@magik6k magik6k commented Sep 27, 2018

@ghost ghost assigned magik6k Sep 27, 2018
@ghost ghost added the status/in-progress In progress label Sep 27, 2018
@Stebalien
Copy link
Member

Can't we just cancel the context? Something like:

diff --git a/parallel.go b/parallel.go
index f7c5015..fe1d976 100644
--- a/parallel.go
+++ b/parallel.go
@@ -164,6 +164,8 @@ func (r Parallel) search(ctx context.Context, do func(routing.IpfsRouting) (<-ch
 	var errs []error
 	var wg sync.WaitGroup
 
+	ctx, cancel := context.WithCancel(ctx)
+
 	for _, ri := range r.Routers {
 		vchan, err := do(ri)
 		switch err {
@@ -177,15 +179,20 @@ func (r Parallel) search(ctx context.Context, do func(routing.IpfsRouting) (<-ch
 
 		go func() {
 			defer wg.Done()
+			sent := 0
 			for {
 				select {
 				case v, ok := <-vchan:
 					if !ok {
+						if sent > 0 {
+							cancel()
+						}
 						return
 					}
 
 					select {
 					case out <- v:
+						sent++
 					case <-ctx.Done():
 						return
 					}
@@ -199,6 +206,7 @@ func (r Parallel) search(ctx context.Context, do func(routing.IpfsRouting) (<-ch
 	go func() {
 		wg.Wait()
 		close(out)
+		cancel()
 	}()
 
 	return out, nil

@Stebalien
Copy link
Member

(same thing really, just uses the context instead of creating and managing a new channel)

@magik6k
Copy link
Contributor Author

magik6k commented Sep 28, 2018

Nope, already tried that, go vet complains about leaking close

@Stebalien
Copy link
Member

Stebalien commented Sep 28, 2018

@magik6k you just need that final cancel in after the close(out) (go vet looks happy with that patch).

@magik6k
Copy link
Contributor Author

magik6k commented Sep 28, 2018

oh.

Looks far better. I squashed all the random the commits, and ran go test ./... -count=400 successfully. Will merge once pubsub PR is ready

@magik6k magik6k merged commit 663b32a into master Sep 29, 2018
@ghost ghost removed the status/in-progress In progress label Sep 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants