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
df164fa9
Commit
df164fa9
authored
Sep 18, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refac(exchange) replace timeout -> context in API
parent
c0ab7e46
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
14 deletions
+11
-14
bitswap.go
bitswap.go
+5
-10
offline.go
offline.go
+3
-2
offline_test.go
offline_test.go
+3
-2
No files found.
bitswap.go
View file @
df164fa9
...
@@ -2,7 +2,6 @@ package bitswap
...
@@ -2,7 +2,6 @@ package bitswap
import
(
import
(
"errors"
"errors"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
ds
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
...
@@ -65,18 +64,14 @@ type bitswap struct {
...
@@ -65,18 +64,14 @@ type bitswap struct {
strategy
strategy
.
Strategy
strategy
strategy
.
Strategy
}
}
// GetBlock attempts to retrieve a particular block from peers, within timeout.
// GetBlock attempts to retrieve a particular block from peers within the
func
(
bs
*
bitswap
)
Block
(
k
u
.
Key
,
timeout
time
.
Duration
)
(
// deadline enforced by the context
func
(
bs
*
bitswap
)
Block
(
ctx
context
.
Context
,
k
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
*
blocks
.
Block
,
error
)
{
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
timeout
)
// TODO replace timeout with ctx in routing interface
begin
:=
time
.
Now
()
tleft
:=
timeout
-
time
.
Now
()
.
Sub
(
begin
)
provs_ch
:=
bs
.
routing
.
FindProvidersAsync
(
ctx
,
k
,
20
)
provs_ch
:=
bs
.
routing
.
FindProvidersAsync
(
ctx
,
k
,
20
)
blockChannel
:=
make
(
chan
blocks
.
Block
)
blockChannel
:=
make
(
chan
blocks
.
Block
)
after
:=
time
.
After
(
tleft
)
// TODO: when the data is received, shut down this for loop ASAP
// TODO: when the data is received, shut down this for loop ASAP
go
func
()
{
go
func
()
{
...
@@ -98,8 +93,8 @@ func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
...
@@ -98,8 +93,8 @@ func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
case
block
:=
<-
blockChannel
:
case
block
:=
<-
blockChannel
:
close
(
blockChannel
)
close
(
blockChannel
)
return
&
block
,
nil
return
&
block
,
nil
case
<-
after
:
case
<-
ctx
.
Done
()
:
return
nil
,
u
.
Err
Timeout
return
nil
,
ctx
.
Err
()
}
}
}
}
...
...
offline.go
View file @
df164fa9
...
@@ -2,7 +2,8 @@ package bitswap
...
@@ -2,7 +2,8 @@ package bitswap
import
(
import
(
"errors"
"errors"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
blocks
"github.com/jbenet/go-ipfs/blocks"
blocks
"github.com/jbenet/go-ipfs/blocks"
exchange
"github.com/jbenet/go-ipfs/exchange"
exchange
"github.com/jbenet/go-ipfs/exchange"
...
@@ -21,7 +22,7 @@ type offlineExchange struct {
...
@@ -21,7 +22,7 @@ type offlineExchange struct {
// Block returns nil to signal that a block could not be retrieved for the
// Block returns nil to signal that a block could not be retrieved for the
// given key.
// given key.
// NB: This function may return before the timeout expires.
// NB: This function may return before the timeout expires.
func
(
_
*
offlineExchange
)
Block
(
k
u
.
Key
,
timeout
time
.
Duration
)
(
*
blocks
.
Block
,
error
)
{
func
(
_
*
offlineExchange
)
Block
(
context
.
Context
,
u
.
Key
)
(
*
blocks
.
Block
,
error
)
{
return
nil
,
errors
.
New
(
"Block unavailable. Operating in offline mode"
)
return
nil
,
errors
.
New
(
"Block unavailable. Operating in offline mode"
)
}
}
...
...
offline_test.go
View file @
df164fa9
...
@@ -2,7 +2,8 @@ package bitswap
...
@@ -2,7 +2,8 @@ package bitswap
import
(
import
(
"testing"
"testing"
"time"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
u
"github.com/jbenet/go-ipfs/util"
u
"github.com/jbenet/go-ipfs/util"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
testutil
"github.com/jbenet/go-ipfs/util/testutil"
...
@@ -10,7 +11,7 @@ import (
...
@@ -10,7 +11,7 @@ import (
func
TestBlockReturnsErr
(
t
*
testing
.
T
)
{
func
TestBlockReturnsErr
(
t
*
testing
.
T
)
{
off
:=
NewOfflineExchange
()
off
:=
NewOfflineExchange
()
_
,
err
:=
off
.
Block
(
u
.
Key
(
"foo"
),
time
.
Second
)
_
,
err
:=
off
.
Block
(
context
.
TODO
(),
u
.
Key
(
"foo"
)
)
if
err
!=
nil
{
if
err
!=
nil
{
return
// as desired
return
// as desired
}
}
...
...
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