@@ -16,6 +16,9 @@ import (
16
16
17
17
const activeWantsLimit = 16
18
18
19
+ // Session holds state for an individual bitswap transfer operation.
20
+ // This allows bitswap to make smarter decisions about who to send wantlist
21
+ // info to, and who to request blocks from
19
22
type Session struct {
20
23
ctx context.Context
21
24
tofetch []* cid.Cid
@@ -40,8 +43,12 @@ type Session struct {
40
43
notif notifications.PubSub
41
44
42
45
uuid logging.Loggable
46
+
47
+ id uint64
43
48
}
44
49
50
+ // NewSession creates a new bitswap session whose lifetime is bounded by the
51
+ // given context
45
52
func (bs * Bitswap ) NewSession (ctx context.Context ) * Session {
46
53
s := & Session {
47
54
activePeers : make (map [peer.ID ]struct {}),
@@ -54,6 +61,7 @@ func (bs *Bitswap) NewSession(ctx context.Context) *Session {
54
61
notif : notifications .New (),
55
62
uuid : loggables .Uuid ("GetBlockRequest" ),
56
63
baseTickDelay : time .Millisecond * 500 ,
64
+ id : bs .getNextSessionID (),
57
65
}
58
66
59
67
cache , _ := lru .New (2048 )
@@ -73,11 +81,11 @@ type blkRecv struct {
73
81
blk blocks.Block
74
82
}
75
83
76
- func (s * Session ) ReceiveBlock (from peer.ID , blk blocks.Block ) {
84
+ func (s * Session ) receiveBlockFrom (from peer.ID , blk blocks.Block ) {
77
85
s .incoming <- blkRecv {from : from , blk : blk }
78
86
}
79
87
80
- func (s * Session ) InterestedIn (c * cid.Cid ) bool {
88
+ func (s * Session ) interestedIn (c * cid.Cid ) bool {
81
89
return s .interest .Contains (c .KeyString ())
82
90
}
83
91
@@ -134,14 +142,14 @@ func (s *Session) run(ctx context.Context) {
134
142
135
143
case <- s .tick .C :
136
144
var live []* cid.Cid
137
- for c , _ := range s .liveWants {
145
+ for c := range s .liveWants {
138
146
cs , _ := cid .Cast ([]byte (c ))
139
147
live = append (live , cs )
140
148
s .liveWants [c ] = time .Now ()
141
149
}
142
150
143
151
// Broadcast these keys to everyone we're connected to
144
- s .bs .wm .WantBlocks (ctx , live , nil )
152
+ s .bs .wm .WantBlocks (ctx , live , nil , s . id )
145
153
146
154
if len (live ) > 0 {
147
155
go func () {
@@ -181,7 +189,7 @@ func (s *Session) wantBlocks(ctx context.Context, ks []*cid.Cid) {
181
189
for _ , c := range ks {
182
190
s .liveWants [c .KeyString ()] = time .Now ()
183
191
}
184
- s .bs .wm .WantBlocks (ctx , ks , s .activePeersArr )
192
+ s .bs .wm .WantBlocks (ctx , ks , s .activePeersArr , s . id )
185
193
}
186
194
187
195
func (s * Session ) cancel (keys []* cid.Cid ) {
@@ -211,11 +219,15 @@ func (s *Session) fetch(ctx context.Context, keys []*cid.Cid) {
211
219
}
212
220
}
213
221
222
+ // GetBlocks fetches a set of blocks within the context of this session and
223
+ // returns a channel that found blocks will be returned on. No order is
224
+ // guaranteed on the returned blocks.
214
225
func (s * Session ) GetBlocks (ctx context.Context , keys []* cid.Cid ) (<- chan blocks.Block , error ) {
215
226
ctx = logging .ContextWithLoggable (ctx , s .uuid )
216
227
return getBlocksImpl (ctx , keys , s .notif , s .fetch , s .cancelWants )
217
228
}
218
229
230
+ // GetBlock fetches a single block
219
231
func (s * Session ) GetBlock (parent context.Context , k * cid.Cid ) (blocks.Block , error ) {
220
232
return getBlock (parent , k , s .GetBlocks )
221
233
}
0 commit comments