Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-bitswap
Commits
288ceffb
Unverified
Commit
288ceffb
authored
Mar 24, 2020
by
dirkmc
Committed by
GitHub
Mar 24, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: race in SessionInterestManager (#324)
parent
93801a74
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
0 deletions
+32
-0
internal/sessioninterestmanager/sessioninterestmanager.go
internal/sessioninterestmanager/sessioninterestmanager.go
+32
-0
No files found.
internal/sessioninterestmanager/sessioninterestmanager.go
View file @
288ceffb
package
sessioninterestmanager
import
(
"sync"
bsswl
"github.com/ipfs/go-bitswap/internal/sessionwantlist"
blocks
"github.com/ipfs/go-block-format"
cid
"github.com/ipfs/go-cid"
)
// SessionInterestManager records the CIDs that each session is interested in.
type
SessionInterestManager
struct
{
lk
sync
.
RWMutex
interested
*
bsswl
.
SessionWantlist
wanted
*
bsswl
.
SessionWantlist
}
...
...
@@ -20,21 +24,39 @@ func New() *SessionInterestManager {
}
}
// When the client asks the session for blocks, the session calls
// RecordSessionInterest() with those cids.
func
(
sim
*
SessionInterestManager
)
RecordSessionInterest
(
ses
uint64
,
ks
[]
cid
.
Cid
)
{
sim
.
lk
.
Lock
()
defer
sim
.
lk
.
Unlock
()
sim
.
interested
.
Add
(
ks
,
ses
)
sim
.
wanted
.
Add
(
ks
,
ses
)
}
// When the session shuts down it calls RemoveSessionInterest().
func
(
sim
*
SessionInterestManager
)
RemoveSessionInterest
(
ses
uint64
)
[]
cid
.
Cid
{
sim
.
lk
.
Lock
()
defer
sim
.
lk
.
Unlock
()
sim
.
wanted
.
RemoveSession
(
ses
)
return
sim
.
interested
.
RemoveSession
(
ses
)
}
// When the session receives blocks, it calls RemoveSessionWants().
func
(
sim
*
SessionInterestManager
)
RemoveSessionWants
(
ses
uint64
,
wants
[]
cid
.
Cid
)
{
sim
.
lk
.
Lock
()
defer
sim
.
lk
.
Unlock
()
sim
.
wanted
.
RemoveSessionKeys
(
ses
,
wants
)
}
// The session calls FilterSessionInterested() to filter the sets of keys for
// those that the session is interested in
func
(
sim
*
SessionInterestManager
)
FilterSessionInterested
(
ses
uint64
,
ksets
...
[]
cid
.
Cid
)
[][]
cid
.
Cid
{
sim
.
lk
.
RLock
()
defer
sim
.
lk
.
RUnlock
()
kres
:=
make
([][]
cid
.
Cid
,
len
(
ksets
))
for
i
,
ks
:=
range
ksets
{
kres
[
i
]
=
sim
.
interested
.
SessionHas
(
ses
,
ks
)
.
Keys
()
...
...
@@ -42,7 +64,12 @@ func (sim *SessionInterestManager) FilterSessionInterested(ses uint64, ksets ...
return
kres
}
// When bitswap receives blocks it calls SplitWantedUnwanted() to discard
// unwanted blocks
func
(
sim
*
SessionInterestManager
)
SplitWantedUnwanted
(
blks
[]
blocks
.
Block
)
([]
blocks
.
Block
,
[]
blocks
.
Block
)
{
sim
.
lk
.
RLock
()
defer
sim
.
lk
.
RUnlock
()
// Get the wanted block keys
ks
:=
make
([]
cid
.
Cid
,
len
(
blks
))
for
_
,
b
:=
range
blks
{
...
...
@@ -63,7 +90,12 @@ func (sim *SessionInterestManager) SplitWantedUnwanted(blks []blocks.Block) ([]b
return
wantedBlks
,
notWantedBlks
}
// When the WantManager receives a message is calls InterestedSessions() to
// find out which sessions are interested in the message.
func
(
sim
*
SessionInterestManager
)
InterestedSessions
(
blks
[]
cid
.
Cid
,
haves
[]
cid
.
Cid
,
dontHaves
[]
cid
.
Cid
)
[]
uint64
{
sim
.
lk
.
RLock
()
defer
sim
.
lk
.
RUnlock
()
ks
:=
make
([]
cid
.
Cid
,
0
,
len
(
blks
)
+
len
(
haves
)
+
len
(
dontHaves
))
ks
=
append
(
ks
,
blks
...
)
ks
=
append
(
ks
,
haves
...
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment