This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
notifications.go
137 lines (117 loc) · 2.64 KB
/
notifications.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package notifications
import (
"context"
"sync"
pubsub "github.com/cskr/pubsub"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
)
const bufferSize = 16
// PubSub is a simple interface for publishing blocks and being able to subscribe
// for cids. It's used internally by bitswap to decouple receiving blocks
// and actually providing them back to the GetBlocks caller.
type PubSub interface {
Publish(block blocks.Block)
Subscribe(ctx context.Context, keys ...cid.Cid) <-chan blocks.Block
Shutdown()
}
// New generates a new PubSub interface.
func New() PubSub {
return &impl{
wrapped: *pubsub.New(bufferSize),
closed: make(chan struct{}),
}
}
type impl struct {
lk sync.RWMutex
wrapped pubsub.PubSub
closed chan struct{}
}
func (ps *impl) Publish(block blocks.Block) {
ps.lk.RLock()
defer ps.lk.RUnlock()
select {
case <-ps.closed:
return
default:
}
ps.wrapped.Pub(block, block.Cid().KeyString())
}
func (ps *impl) Shutdown() {
ps.lk.Lock()
defer ps.lk.Unlock()
select {
case <-ps.closed:
return
default:
}
close(ps.closed)
ps.wrapped.Shutdown()
}
// Subscribe returns a channel of blocks for the given |keys|. |blockChannel|
// is closed if the |ctx| times out or is cancelled, or after receiving the blocks
// corresponding to |keys|.
func (ps *impl) Subscribe(ctx context.Context, keys ...cid.Cid) <-chan blocks.Block {
blocksCh := make(chan blocks.Block, len(keys))
valuesCh := make(chan interface{}, len(keys)) // provide our own channel to control buffer, prevent blocking
if len(keys) == 0 {
close(blocksCh)
return blocksCh
}
// prevent shutdown
ps.lk.RLock()
defer ps.lk.RUnlock()
select {
case <-ps.closed:
close(blocksCh)
return blocksCh
default:
}
// AddSubOnceEach listens for each key in the list, and closes the channel
// once all keys have been received
ps.wrapped.AddSubOnceEach(valuesCh, toStrings(keys)...)
go func() {
defer func() {
close(blocksCh)
ps.lk.RLock()
defer ps.lk.RUnlock()
// Don't touch the pubsub instance if we're
// already closed.
select {
case <-ps.closed:
return
default:
}
ps.wrapped.Unsub(valuesCh)
}()
for {
select {
case <-ctx.Done():
return
case <-ps.closed:
case val, ok := <-valuesCh:
if !ok {
return
}
block, ok := val.(blocks.Block)
if !ok {
return
}
select {
case <-ctx.Done():
return
case blocksCh <- block: // continue
case <-ps.closed:
}
}
}
}()
return blocksCh
}
func toStrings(keys []cid.Cid) []string {
strs := make([]string, 0, len(keys))
for _, key := range keys {
strs = append(strs, key.KeyString())
}
return strs
}