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
5c5b77bb
Commit
5c5b77bb
authored
Jul 13, 2015
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow bitswap to attempt to write blocks to disk multiple times
License: MIT Signed-off-by:
Jeromy
<
jeromyj@gmail.com
>
parent
75237256
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
19 deletions
+41
-19
bitswap.go
bitswap.go
+41
-19
No files found.
bitswap.go
View file @
5c5b77bb
...
...
@@ -228,7 +228,9 @@ func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
default
:
}
if
err
:=
bs
.
blockstore
.
Put
(
blk
);
err
!=
nil
{
err
:=
bs
.
tryPutBlock
(
blk
,
4
)
// attempt to store block up to four times
if
err
!=
nil
{
log
.
Errorf
(
"Error writing block to datastore: %s"
,
err
)
return
err
}
...
...
@@ -242,6 +244,18 @@ func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
return
nil
}
func
(
bs
*
Bitswap
)
tryPutBlock
(
blk
*
blocks
.
Block
,
attempts
int
)
error
{
var
err
error
for
i
:=
0
;
i
<
attempts
;
i
++
{
if
err
=
bs
.
blockstore
.
Put
(
blk
);
err
==
nil
{
break
}
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
400
*
(
i
+
1
)))
}
return
err
}
func
(
bs
*
Bitswap
)
connectToProviders
(
ctx
context
.
Context
,
entries
[]
wantlist
.
Entry
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
...
...
@@ -297,38 +311,46 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
wg
.
Add
(
1
)
go
func
(
b
*
blocks
.
Block
)
{
defer
wg
.
Done
()
bs
.
counterLk
.
Lock
()
bs
.
blocksRecvd
++
has
,
err
:=
bs
.
blockstore
.
Has
(
b
.
Key
())
if
err
!=
nil
{
bs
.
counterLk
.
Unlock
()
log
.
Infof
(
"blockstore.Has error: %s"
,
err
)
return
}
if
err
==
nil
&&
has
{
bs
.
dupBlocksRecvd
++
}
brecvd
:=
bs
.
blocksRecvd
bdup
:=
bs
.
dupBlocksRecvd
bs
.
counterLk
.
Unlock
()
if
has
{
return
if
err
:=
bs
.
updateReceiveCounters
(
b
.
Key
());
err
!=
nil
{
return
// ignore error, is either logged previously, or ErrAlreadyHaveBlock
}
k
:=
b
.
Key
()
log
.
Event
(
ctx
,
"Bitswap.GetBlockRequest.End"
,
&
k
)
log
.
Debugf
(
"got block %s from %s
(%d,%d)"
,
b
,
p
,
brecvd
,
bdu
p
)
log
.
Debugf
(
"got block %s from %s
"
,
b
,
p
)
hasBlockCtx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
hasBlockTimeout
)
defer
cancel
()
if
err
:=
bs
.
HasBlock
(
hasBlockCtx
,
b
);
err
!=
nil
{
log
.
Warningf
(
"ReceiveMessage HasBlock error: %s"
,
err
)
}
cancel
()
}(
block
)
}
wg
.
Wait
()
}
var
ErrAlreadyHaveBlock
=
errors
.
New
(
"already have block"
)
func
(
bs
*
Bitswap
)
updateReceiveCounters
(
k
key
.
Key
)
error
{
bs
.
counterLk
.
Lock
()
defer
bs
.
counterLk
.
Unlock
()
bs
.
blocksRecvd
++
has
,
err
:=
bs
.
blockstore
.
Has
(
k
)
if
err
!=
nil
{
log
.
Infof
(
"blockstore.Has error: %s"
,
err
)
return
err
}
if
err
==
nil
&&
has
{
bs
.
dupBlocksRecvd
++
}
if
has
{
return
ErrAlreadyHaveBlock
}
return
nil
}
// Connected/Disconnected warns bitswap about peer connections
func
(
bs
*
Bitswap
)
PeerConnected
(
p
peer
.
ID
)
{
bs
.
wm
.
Connected
(
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