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
ef92b55d
Commit
ef92b55d
authored
Sep 18, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(exch:bitswap) simply get method
parent
640fa135
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
46 deletions
+33
-46
bitswap.go
bitswap.go
+33
-46
No files found.
bitswap.go
View file @
ef92b55d
...
@@ -65,63 +65,38 @@ type bitswap struct {
...
@@ -65,63 +65,38 @@ type bitswap struct {
// deadline enforced by the context
// deadline enforced by the context
//
//
// TODO ensure only one active request per key
// TODO ensure only one active request per key
func
(
bs
*
bitswap
)
Block
(
ctx
context
.
Context
,
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
func
(
bs
*
bitswap
)
Block
(
parent
context
.
Context
,
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
const
maxProviders
=
20
ctx
,
cancelFunc
:=
context
.
WithCancel
(
parent
)
provs_ch
:=
bs
.
routing
.
FindProvidersAsync
(
ctx
,
k
,
maxProviders
)
promise
:=
bs
.
notifications
.
Subscribe
(
ctx
,
k
)
blockChannel
:=
make
(
chan
blocks
.
Block
)
// TODO: when the data is received, shut down this for loop ASAP
go
func
()
{
go
func
()
{
for
p
:=
range
provs_ch
{
const
maxProviders
=
20
go
func
(
pr
*
peer
.
Peer
)
{
peersToQuery
:=
bs
.
routing
.
FindProvidersAsync
(
ctx
,
k
,
maxProviders
)
blk
,
err
:=
bs
.
getBlock
(
ctx
,
k
,
pr
)
message
:=
bsmsg
.
New
()
message
.
AppendWanted
(
k
)
for
i
:=
range
peersToQuery
{
go
func
(
p
*
peer
.
Peer
)
{
response
,
err
:=
bs
.
sender
.
SendRequest
(
ctx
,
p
,
message
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
select
{
// FIXME ensure accounting is handled correctly when
case
blockChannel
<-
*
blk
:
// communication fails. May require slightly different API to
default
:
// get better guarantees. May need shared sequence numbers.
}
bs
.
strategy
.
MessageSent
(
p
,
message
)
}(
p
)
bs
.
ReceiveMessage
(
ctx
,
p
,
response
)
}(
i
)
}
}
}()
}()
select
{
select
{
case
block
:=
<-
blockChannel
:
case
block
:=
<-
promise
:
c
lose
(
blockCh
an
n
el
)
can
c
el
Func
(
)
return
&
block
,
nil
return
&
block
,
nil
case
<-
ctx
.
Done
()
:
case
<-
parent
.
Done
()
:
return
nil
,
ctx
.
Err
()
return
nil
,
parent
.
Err
()
}
}
func
(
bs
*
bitswap
)
getBlock
(
ctx
context
.
Context
,
k
u
.
Key
,
p
*
peer
.
Peer
)
(
*
blocks
.
Block
,
error
)
{
blockChannel
:=
bs
.
notifications
.
Subscribe
(
ctx
,
k
)
message
:=
bsmsg
.
New
()
message
.
AppendWanted
(
k
)
bs
.
send
(
ctx
,
p
,
message
)
block
,
ok
:=
<-
blockChannel
if
!
ok
{
return
nil
,
u
.
ErrTimeout
}
return
&
block
,
nil
}
func
(
bs
*
bitswap
)
sendToPeersThatWant
(
ctx
context
.
Context
,
block
blocks
.
Block
)
{
for
_
,
p
:=
range
bs
.
strategy
.
Peers
()
{
if
bs
.
strategy
.
BlockIsWantedByPeer
(
block
.
Key
(),
p
)
{
if
bs
.
strategy
.
ShouldSendBlockToPeer
(
block
.
Key
(),
p
)
{
message
:=
bsmsg
.
New
()
message
.
AppendBlock
(
block
)
go
bs
.
send
(
ctx
,
p
,
message
)
}
}
}
}
}
}
...
@@ -173,3 +148,15 @@ func (bs *bitswap) send(ctx context.Context, p *peer.Peer, m bsmsg.BitSwapMessag
...
@@ -173,3 +148,15 @@ func (bs *bitswap) send(ctx context.Context, p *peer.Peer, m bsmsg.BitSwapMessag
func
numBytes
(
b
blocks
.
Block
)
int
{
func
numBytes
(
b
blocks
.
Block
)
int
{
return
len
(
b
.
Data
)
return
len
(
b
.
Data
)
}
}
func
(
bs
*
bitswap
)
sendToPeersThatWant
(
ctx
context
.
Context
,
block
blocks
.
Block
)
{
for
_
,
p
:=
range
bs
.
strategy
.
Peers
()
{
if
bs
.
strategy
.
BlockIsWantedByPeer
(
block
.
Key
(),
p
)
{
if
bs
.
strategy
.
ShouldSendBlockToPeer
(
block
.
Key
(),
p
)
{
message
:=
bsmsg
.
New
()
message
.
AppendBlock
(
block
)
go
bs
.
send
(
ctx
,
p
,
message
)
}
}
}
}
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