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
d12c9656
Commit
d12c9656
authored
Nov 26, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
document bitswap more
parent
15a7d870
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
10 deletions
+20
-10
bitswap.go
bitswap.go
+20
-10
No files found.
bitswap.go
View file @
d12c9656
...
...
@@ -22,7 +22,8 @@ import (
var
log
=
eventlog
.
Logger
(
"bitswap"
)
// Number of providers to request for sending a wantlist to
const
maxProvidersPerRequest
=
6
// TODO: if a 'non-nice' strategy is implemented, consider increasing this value
const
maxProvidersPerRequest
=
3
// New initializes a BitSwap instance that communicates over the
// provided BitSwapNetwork. This function registers the returned instance as
...
...
@@ -211,6 +212,7 @@ func (bs *bitswap) loop(parent context.Context) {
for
{
select
{
case
<-
broadcastSignal
.
C
:
// Resend unfulfilled wantlist keys
bs
.
sendWantlistToProviders
(
ctx
,
bs
.
wantlist
.
Keys
())
case
ks
:=
<-
bs
.
batchRequests
:
// TODO: implement batching on len(ks) > X for some X
...
...
@@ -224,6 +226,13 @@ func (bs *bitswap) loop(parent context.Context) {
for
_
,
k
:=
range
ks
{
bs
.
wantlist
.
Add
(
k
)
}
// NB: send want list to providers for the first peer in this list.
// the assumption is made that the providers of the first key in
// the set are likely to have others as well.
// This currently holds true in most every situation, since when
// pinning a file, you store and provide all blocks associated with
// it. Later, this assumption may not hold as true if we implement
// newer bitswap strategies.
providers
:=
bs
.
routing
.
FindProvidersAsync
(
ctx
,
ks
[
0
],
maxProvidersPerRequest
)
err
:=
bs
.
sendWantListTo
(
ctx
,
providers
)
...
...
@@ -263,7 +272,6 @@ func (bs *bitswap) receiveBlock(ctx context.Context, block *blocks.Block) {
func
(
bs
*
bitswap
)
ReceiveMessage
(
ctx
context
.
Context
,
p
peer
.
Peer
,
incoming
bsmsg
.
BitSwapMessage
)
(
peer
.
Peer
,
bsmsg
.
BitSwapMessage
)
{
log
.
Debugf
(
"ReceiveMessage from %s"
,
p
)
log
.
Debugf
(
"Message wantlist: %v"
,
incoming
.
Wantlist
())
if
p
==
nil
{
log
.
Error
(
"Received message from nil peer!"
)
...
...
@@ -279,15 +287,17 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
// Record message bytes in ledger
// TODO: this is bad, and could be easily abused.
// Should only track *useful* messages in ledger
bs
.
strategy
.
MessageReceived
(
p
,
incoming
)
// FIRST
// This call records changes to wantlists, blocks received,
// and number of bytes transfered.
bs
.
strategy
.
MessageReceived
(
p
,
incoming
)
for
_
,
block
:=
range
incoming
.
Blocks
()
{
go
bs
.
receiveBlock
(
ctx
,
block
)
}
go
func
()
{
for
_
,
block
:=
range
incoming
.
Blocks
()
{
bs
.
receiveBlock
(
ctx
,
block
)
}
}()
for
_
,
key
:=
range
incoming
.
Wantlist
()
{
// TODO: might be better to check if we have the block before checking
// if we should send it to someone
if
bs
.
strategy
.
ShouldSendBlockToPeer
(
key
,
p
)
{
if
block
,
errBlockNotFound
:=
bs
.
blockstore
.
Get
(
key
);
errBlockNotFound
!=
nil
{
continue
...
...
@@ -303,12 +313,12 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
}
blkmsg
.
AddBlock
(
block
)
bs
.
strategy
.
MessageSent
(
p
,
blkmsg
)
bs
.
send
(
ctx
,
p
,
blkmsg
)
}
}
}
// TODO: consider changing this function to not return anything
return
nil
,
nil
}
...
...
@@ -326,7 +336,7 @@ func (bs *bitswap) send(ctx context.Context, p peer.Peer, m bsmsg.BitSwapMessage
}
func
(
bs
*
bitswap
)
sendToPeersThatWant
(
ctx
context
.
Context
,
block
*
blocks
.
Block
)
{
log
.
Debugf
(
"Sending %
v
to peers that want it"
,
block
.
Key
()
)
log
.
Debugf
(
"Sending %
s
to peers that want it"
,
block
)
for
_
,
p
:=
range
bs
.
strategy
.
Peers
()
{
if
bs
.
strategy
.
BlockIsWantedByPeer
(
block
.
Key
(),
p
)
{
...
...
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