Commit 6fe3af11 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Jeromy

fix(bs/notifications) use SubOnceEach to provide uniqueness guarantee

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>

vendor forked pubsub to get SubOnceEach

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 9c59e0f0
...@@ -2,7 +2,7 @@ package notifications ...@@ -2,7 +2,7 @@ package notifications
import ( import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
pubsub "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/tuxychandru/pubsub" pubsub "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/maybebtc/pubsub"
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
...@@ -39,20 +39,16 @@ func (ps *impl) Shutdown() { ...@@ -39,20 +39,16 @@ func (ps *impl) Shutdown() {
func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Block { func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Block {
blocksCh := make(chan *blocks.Block, len(keys)) blocksCh := make(chan *blocks.Block, len(keys))
valuesCh := make(chan interface{}, len(keys)) if len(keys) == 0 {
ps.wrapped.AddSub(valuesCh, toStrings(keys)...) close(blocksCh)
return blocksCh
}
valuesCh := ps.wrapped.SubOnceEach(toStrings(keys)...)
go func() { go func() {
defer func() { defer func() {
ps.wrapped.Unsub(valuesCh, toStrings(keys)...)
close(blocksCh) close(blocksCh)
}() }()
seen := make(map[u.Key]struct{})
i := 0 // req'd because it only counts unique block sends
for { for {
if i >= len(keys) {
return
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
...@@ -64,22 +60,10 @@ func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Blo ...@@ -64,22 +60,10 @@ func (ps *impl) Subscribe(ctx context.Context, keys ...u.Key) <-chan *blocks.Blo
if !ok { if !ok {
return return
} }
if _, ok := seen[block.Key()]; ok {
continue
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case blocksCh <- block: // continue case blocksCh <- block: // continue
// Unsub alone is insufficient for keeping out duplicates.
// It's a race to unsubscribe before pubsub handles the
// next Publish call. Therefore, must also check for
// duplicates manually. Unsub is a performance
// consideration to avoid lots of unnecessary channel
// chatter.
ps.wrapped.Unsub(valuesCh, string(block.Key()))
i++
seen[block.Key()] = struct{}{}
} }
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment