-
Notifications
You must be signed in to change notification settings - Fork 12
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
[Low prio] Add Watch() method to watch pubsub updates #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,17 @@ type PubsubValueStore struct { | |
mx sync.Mutex | ||
subs map[string]*floodsub.Subscription | ||
|
||
watchMux sync.RWMutex | ||
watch map[string]*watchChannels | ||
|
||
Validator record.Validator | ||
} | ||
|
||
type watchChannels struct { | ||
mux sync.RWMutex | ||
channels []chan []byte | ||
} | ||
|
||
// NewPubsubPublisher constructs a new Publisher that publishes IPNS records through pubsub. | ||
// The constructor interface is complicated by the need to bootstrap the pubsub topic. | ||
// This could be greatly simplified if the pubsub implementation handled bootstrap itself | ||
|
@@ -51,6 +59,7 @@ func NewPubsubValueStore(ctx context.Context, host p2phost.Host, cr routing.Cont | |
ps: ps, | ||
Validator: validator, | ||
subs: make(map[string]*floodsub.Subscription), | ||
watch: make(map[string]*watchChannels), | ||
} | ||
} | ||
|
||
|
@@ -138,6 +147,23 @@ func (p *PubsubValueStore) Subscribe(key string) error { | |
return nil | ||
} | ||
|
||
func (p *PubsubValueStore) Watch(key string) <-chan []byte { | ||
p.watchMux.Lock() | ||
defer p.watchMux.Unlock() | ||
wChs, ok := p.watch[key] | ||
if !ok { | ||
wChs = &watchChannels{ | ||
channels: make([]chan []byte, 0), | ||
} | ||
p.watch[key] = wChs | ||
} | ||
newCh := make(chan []byte) | ||
wChs.mux.Lock() | ||
wChs.channels = append(wChs.channels, newCh) | ||
wChs.mux.Unlock() | ||
return newCh | ||
} | ||
|
||
func (p *PubsubValueStore) getLocal(key string) ([]byte, error) { | ||
dsval, err := p.ds.Get(dshelp.NewKeyFromBinary([]byte(key))) | ||
if err != nil { | ||
|
@@ -189,6 +215,8 @@ func (p *PubsubValueStore) Cancel(name string) bool { | |
delete(p.subs, name) | ||
} | ||
|
||
p.cancelWatchers(name) | ||
|
||
return ok | ||
} | ||
|
||
|
@@ -209,10 +237,46 @@ func (p *PubsubValueStore) handleSubscription(sub *floodsub.Subscription, key st | |
if err != nil { | ||
log.Warningf("PubsubResolve: error writing update for %s: %s", key, err) | ||
} | ||
p.notifyWatchers(key, msg.GetData()) | ||
} | ||
} | ||
} | ||
|
||
func (p *PubsubValueStore) notifyWatchers(key string, data []byte) { | ||
p.watchMux.RLock() | ||
watchChannels, ok := p.watch[key] | ||
if !ok { | ||
p.watchMux.RUnlock() | ||
return | ||
} | ||
watchChannels.mux.RLock() | ||
p.watchMux.RUnlock() | ||
|
||
defer watchChannels.mux.RUnlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could be wrong but there is no difference between deferring the RUnlock here as opposed to right under the call to RLock? And it would be clearer if the defer was right after the lock. For example: watchChannels.mux.RLock()
defer watchChannels.mux.RUnlock()
p.watchMux.RUnlock() |
||
for _, ch := range watchChannels.channels { | ||
select { | ||
case ch <- data: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd consider:
That is, select {
case ch <- data:
case <-ch:
ch <- data
} That way, the user always gets the latest value. |
||
default: | ||
} | ||
} | ||
} | ||
|
||
func (p *PubsubValueStore) cancelWatchers(key string) { | ||
p.watchMux.Lock() | ||
defer p.watchMux.Unlock() | ||
watchChannels, ok := p.watch[key] | ||
if !ok { | ||
return | ||
} | ||
|
||
watchChannels.mux.Lock() | ||
for _, ch := range watchChannels.channels { | ||
close(ch) | ||
} | ||
watchChannels.mux.Unlock() | ||
delete(p.watch, key) | ||
} | ||
|
||
// rendezvous with peers in the name topic through provider records | ||
// Note: rendezvous/boostrap should really be handled by the pubsub implementation itself! | ||
func bootstrapPubsub(ctx context.Context, cr routing.ContentRouting, host p2phost.Host, name string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc?