@@ -16,6 +16,7 @@ import (
16
16
bsmsg "github.com/ipfs/go-bitswap/message"
17
17
bsmq "github.com/ipfs/go-bitswap/messagequeue"
18
18
bsnet "github.com/ipfs/go-bitswap/network"
19
+ notifications "github.com/ipfs/go-bitswap/notifications"
19
20
bspm "github.com/ipfs/go-bitswap/peermanager"
20
21
bspqm "github.com/ipfs/go-bitswap/providerquerymanager"
21
22
bssession "github.com/ipfs/go-bitswap/session"
@@ -116,16 +117,18 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
116
117
pqm := bspqm .New (ctx , network )
117
118
118
119
sessionFactory := func (ctx context.Context , id uint64 , pm bssession.PeerManager , srs bssession.RequestSplitter ,
120
+ notif notifications.PubSub ,
119
121
provSearchDelay time.Duration ,
120
122
rebroadcastDelay delay.D ) bssm.Session {
121
- return bssession .New (ctx , id , wm , pm , srs , provSearchDelay , rebroadcastDelay )
123
+ return bssession .New (ctx , id , wm , pm , srs , notif , provSearchDelay , rebroadcastDelay )
122
124
}
123
125
sessionPeerManagerFactory := func (ctx context.Context , id uint64 ) bssession.PeerManager {
124
126
return bsspm .New (ctx , id , network .ConnectionManager (), pqm )
125
127
}
126
128
sessionRequestSplitterFactory := func (ctx context.Context ) bssession.RequestSplitter {
127
129
return bssrs .New (ctx )
128
130
}
131
+ notif := notifications .New ()
129
132
130
133
bs := & Bitswap {
131
134
blockstore : bstore ,
@@ -136,7 +139,8 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
136
139
provideKeys : make (chan cid.Cid , provideKeysBufferSize ),
137
140
wm : wm ,
138
141
pqm : pqm ,
139
- sm : bssm .New (ctx , sessionFactory , sessionPeerManagerFactory , sessionRequestSplitterFactory ),
142
+ sm : bssm .New (ctx , sessionFactory , sessionPeerManagerFactory , sessionRequestSplitterFactory , notif ),
143
+ notif : notif ,
140
144
counters : new (counters ),
141
145
dupMetric : dupHist ,
142
146
allMetric : allHist ,
@@ -163,6 +167,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork,
163
167
go func () {
164
168
<- px .Closing () // process closes first
165
169
cancelFunc ()
170
+ notif .Shutdown ()
166
171
}()
167
172
procctx .CloseAfterContext (px , ctx ) // parent cancelled first
168
173
@@ -187,6 +192,9 @@ type Bitswap struct {
187
192
// NB: ensure threadsafety
188
193
blockstore blockstore.Blockstore
189
194
195
+ // manages channels of outgoing blocks for sessions
196
+ notif notifications.PubSub
197
+
190
198
// newBlocks is a channel for newly added blocks to be provided to the
191
199
// network. blocks pushed down this channel get buffered and fed to the
192
200
// provideKeys channel later on to avoid too much network activity
@@ -307,18 +315,38 @@ func (bs *Bitswap) receiveBlocksFrom(from peer.ID, blks []blocks.Block) error {
307
315
// to the same node. We should address this soon, but i'm not going to do
308
316
// it now as it requires more thought and isnt causing immediate problems.
309
317
310
- // Send all blocks (including duplicates) to any sessions that want them.
318
+ allKs := make ([]cid.Cid , 0 , len (blks ))
319
+ for _ , b := range blks {
320
+ allKs = append (allKs , b .Cid ())
321
+ }
322
+
323
+ wantedKs := allKs
324
+ if len (blks ) != len (wanted ) {
325
+ wantedKs = make ([]cid.Cid , 0 , len (wanted ))
326
+ for _ , b := range wanted {
327
+ wantedKs = append (wantedKs , b .Cid ())
328
+ }
329
+ }
330
+
331
+ // Send all block keys (including duplicates) to any sessions that want them.
311
332
// (The duplicates are needed by sessions for accounting purposes)
312
- bs .sm .ReceiveBlocksFrom (from , blks )
333
+ bs .sm .ReceiveFrom (from , allKs )
313
334
314
- // Send wanted blocks to decision engine
315
- bs .engine .AddBlocks (wanted )
335
+ // Send wanted block keys to decision engine
336
+ bs .engine .AddBlocks (wantedKs )
337
+
338
+ // Publish the block to any Bitswap clients that had requested blocks.
339
+ // (the sessions use this pubsub mechanism to inform clients of received
340
+ // blocks)
341
+ for _ , b := range wanted {
342
+ bs .notif .Publish (b )
343
+ }
316
344
317
345
// If the reprovider is enabled, send wanted blocks to reprovider
318
346
if bs .provideEnabled {
319
- for _ , b := range wanted {
347
+ for _ , k := range wantedKs {
320
348
select {
321
- case bs .newBlocks <- b . Cid () :
349
+ case bs .newBlocks <- k :
322
350
// send block off to be reprovided
323
351
case <- bs .process .Closing ():
324
352
return bs .process .Close ()
0 commit comments