1
1
package sessioninterestmanager
2
2
3
3
import (
4
+ "sync"
5
+
4
6
bsswl "github.com/ipfs/go-bitswap/internal/sessionwantlist"
5
7
blocks "github.com/ipfs/go-block-format"
6
8
7
9
cid "github.com/ipfs/go-cid"
8
10
)
9
11
12
+ // SessionInterestManager records the CIDs that each session is interested in.
10
13
type SessionInterestManager struct {
14
+ lk sync.RWMutex
11
15
interested * bsswl.SessionWantlist
12
16
wanted * bsswl.SessionWantlist
13
17
}
@@ -20,29 +24,52 @@ func New() *SessionInterestManager {
20
24
}
21
25
}
22
26
27
+ // When the client asks the session for blocks, the session calls
28
+ // RecordSessionInterest() with those cids.
23
29
func (sim * SessionInterestManager ) RecordSessionInterest (ses uint64 , ks []cid.Cid ) {
30
+ sim .lk .Lock ()
31
+ defer sim .lk .Unlock ()
32
+
24
33
sim .interested .Add (ks , ses )
25
34
sim .wanted .Add (ks , ses )
26
35
}
27
36
37
+ // When the session shuts down it calls RemoveSessionInterest().
28
38
func (sim * SessionInterestManager ) RemoveSessionInterest (ses uint64 ) []cid.Cid {
39
+ sim .lk .Lock ()
40
+ defer sim .lk .Unlock ()
41
+
29
42
sim .wanted .RemoveSession (ses )
30
43
return sim .interested .RemoveSession (ses )
31
44
}
32
45
46
+ // When the session receives blocks, it calls RemoveSessionWants().
33
47
func (sim * SessionInterestManager ) RemoveSessionWants (ses uint64 , wants []cid.Cid ) {
48
+ sim .lk .Lock ()
49
+ defer sim .lk .Unlock ()
50
+
34
51
sim .wanted .RemoveSessionKeys (ses , wants )
35
52
}
36
53
54
+ // The session calls FilterSessionInterested() to filter the sets of keys for
55
+ // those that the session is interested in
37
56
func (sim * SessionInterestManager ) FilterSessionInterested (ses uint64 , ksets ... []cid.Cid ) [][]cid.Cid {
57
+ sim .lk .RLock ()
58
+ defer sim .lk .RUnlock ()
59
+
38
60
kres := make ([][]cid.Cid , len (ksets ))
39
61
for i , ks := range ksets {
40
62
kres [i ] = sim .interested .SessionHas (ses , ks ).Keys ()
41
63
}
42
64
return kres
43
65
}
44
66
67
+ // When bitswap receives blocks it calls SplitWantedUnwanted() to discard
68
+ // unwanted blocks
45
69
func (sim * SessionInterestManager ) SplitWantedUnwanted (blks []blocks.Block ) ([]blocks.Block , []blocks.Block ) {
70
+ sim .lk .RLock ()
71
+ defer sim .lk .RUnlock ()
72
+
46
73
// Get the wanted block keys
47
74
ks := make ([]cid.Cid , len (blks ))
48
75
for _ , b := range blks {
@@ -63,7 +90,12 @@ func (sim *SessionInterestManager) SplitWantedUnwanted(blks []blocks.Block) ([]b
63
90
return wantedBlks , notWantedBlks
64
91
}
65
92
93
+ // When the WantManager receives a message is calls InterestedSessions() to
94
+ // find out which sessions are interested in the message.
66
95
func (sim * SessionInterestManager ) InterestedSessions (blks []cid.Cid , haves []cid.Cid , dontHaves []cid.Cid ) []uint64 {
96
+ sim .lk .RLock ()
97
+ defer sim .lk .RUnlock ()
98
+
67
99
ks := make ([]cid.Cid , 0 , len (blks )+ len (haves )+ len (dontHaves ))
68
100
ks = append (ks , blks ... )
69
101
ks = append (ks , haves ... )
0 commit comments