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 search after a good value is found #12

Merged
merged 2 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.4: QmeJrgriRkyAfJUuCEhwxboCsmbzrcWEFt1NeqYNUzvZ5x
0.4.5: QmPbLCBNGvyQhs3xK64cbobq7sN1Hdn6Ud9pkhsZME1sqT
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
},
{
"author": "Stebalien",
"hash": "QmdrhAjJKSqpykiqkZt7CyVXUyy3R9W4ptmiW49MCWXLeG",
"hash": "QmcRdSdYkL17qhuQAibF5D14XdQ1iunvaVnXGjDiQTdRm9",
"name": "go-libp2p-routing-helpers",
"version": "0.3.3"
"version": "0.3.4"
},
{
"author": "whyrusleeping",
Expand All @@ -86,6 +86,6 @@
"license": "",
"name": "go-libp2p-pubsub-router",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.4.4"
"version": "0.4.5"
}

11 changes: 8 additions & 3 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ func (p *PubsubValueStore) SearchValue(ctx context.Context, key string, opts ...
return nil, err
}

p.watchLk.Lock()
defer p.watchLk.Unlock()

out := make(chan []byte, 1)
lv, err := p.getLocal(key)
if err == nil {
out <- lv
close(out)
return out, nil
}

p.watchLk.Lock()
wg, ok := p.watching[key]
if !ok {
wg = &watchGroup{
Expand Down Expand Up @@ -229,14 +233,15 @@ func (p *PubsubValueStore) SearchValue(ctx context.Context, key string, opts ...
case <-out:
out <- val
}

// 1 is good enough
return
case <-ctx.Done():
return
}
}
}()

p.watchLk.Unlock()

return out, nil
}

Expand Down
16 changes: 15 additions & 1 deletion pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestWatch(t *testing.T) {
pub, vss := setupTest(ctx, t)

key := "/namespace/key"
key2 := "/namespace/key2"

ch, err := vss[1].SearchValue(ctx, key)
if err != nil {
Expand All @@ -234,11 +235,24 @@ func TestWatch(t *testing.T) {
t.Fatal(err)
}

_, err = vss[1].Cancel(key)
ch, err = vss[1].SearchValue(ctx, key2)
if err != nil {
t.Fatal(err)
}

_, err = vss[1].Cancel(key2)
if err.Error() != "key has active subscriptions" {
t.Fatal("cancel should have failed")
}

// let the flood propagate
time.Sleep(time.Second * 1)

ch, err = vss[1].SearchValue(ctx, key)
if err != nil {
t.Fatal(err)
}

v = string(<-ch)
if v != "valid for key 2" {
t.Errorf("got unexpected value: %s", v)
Expand Down