提交 87407a99 作者: Jeromy

add context to blockservice Get

上级 c2692f3a
...@@ -3,6 +3,9 @@ package blockservice ...@@ -3,6 +3,9 @@ package blockservice
import ( import (
"bytes" "bytes"
"testing" "testing"
"time"
"code.google.com/p/go.net/context"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
...@@ -37,7 +40,8 @@ func TestBlocks(t *testing.T) { ...@@ -37,7 +40,8 @@ func TestBlocks(t *testing.T) {
t.Error("returned key is not equal to block key", err) t.Error("returned key is not equal to block key", err)
} }
b2, err := bs.GetBlock(b.Key()) ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
b2, err := bs.GetBlock(ctx, b.Key())
if err != nil { if err != nil {
t.Error("failed to retrieve block from BlockService", err) t.Error("failed to retrieve block from BlockService", err)
return return
......
...@@ -2,7 +2,6 @@ package blockservice ...@@ -2,7 +2,6 @@ package blockservice
import ( import (
"fmt" "fmt"
"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/go-datastore" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
...@@ -52,7 +51,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) { ...@@ -52,7 +51,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
// GetBlock retrieves a particular block from the service, // GetBlock retrieves a particular block from the service,
// Getting it from the datastore using the key (hash). // Getting it from the datastore using the key (hash).
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) { func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, error) {
log.Debug("BlockService GetBlock: '%s'", k) log.Debug("BlockService GetBlock: '%s'", k)
datai, err := s.Datastore.Get(k.DsKey()) datai, err := s.Datastore.Get(k.DsKey())
if err == nil { if err == nil {
...@@ -67,7 +66,6 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) { ...@@ -67,7 +66,6 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
}, nil }, nil
} else if err == ds.ErrNotFound && s.Remote != nil { } else if err == ds.ErrNotFound && s.Remote != nil {
log.Debug("Blockservice: Searching bitswap.") log.Debug("Blockservice: Searching bitswap.")
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
blk, err := s.Remote.Block(ctx, k) blk, err := s.Remote.Block(ctx, k)
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -5,6 +5,9 @@ import ( ...@@ -5,6 +5,9 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"time"
"code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/jbenet/go-ipfs/blocks" "github.com/jbenet/go-ipfs/blocks"
...@@ -26,7 +29,8 @@ func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out ...@@ -26,7 +29,8 @@ func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out
k := u.Key(h) k := u.Key(h)
log.Debug("BlockGet key: '%q'", k) log.Debug("BlockGet key: '%q'", k)
b, err := n.Blocks.GetBlock(k) ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
b, err := n.Blocks.GetBlock(ctx, k)
if err != nil { if err != nil {
return fmt.Errorf("block get: %v", err) return fmt.Errorf("block get: %v", err)
} }
......
...@@ -2,6 +2,9 @@ package merkledag ...@@ -2,6 +2,9 @@ package merkledag
import ( import (
"fmt" "fmt"
"time"
"code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
...@@ -204,7 +207,8 @@ func (n *DAGService) Get(k u.Key) (*Node, error) { ...@@ -204,7 +207,8 @@ func (n *DAGService) Get(k u.Key) (*Node, error) {
return nil, fmt.Errorf("DAGService is nil") return nil, fmt.Errorf("DAGService is nil")
} }
b, err := n.Blocks.GetBlock(k) ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
b, err := n.Blocks.GetBlock(ctx, k)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -67,7 +67,7 @@ func (c *MultiConn) Add(conns ...Conn) { ...@@ -67,7 +67,7 @@ func (c *MultiConn) Add(conns ...Conn) {
defer c.Unlock() defer c.Unlock()
for _, c2 := range conns { for _, c2 := range conns {
log.Info("MultiConn: adding %s", c2) log.Infof("MultiConn: adding %s", c2)
if c.LocalPeer() != c2.LocalPeer() || c.RemotePeer() != c2.RemotePeer() { if c.LocalPeer() != c2.LocalPeer() || c.RemotePeer() != c2.RemotePeer() {
log.Error(c2) log.Error(c2)
c.Unlock() // ok to unlock (to log). panicing. c.Unlock() // ok to unlock (to log). panicing.
...@@ -82,7 +82,7 @@ func (c *MultiConn) Add(conns ...Conn) { ...@@ -82,7 +82,7 @@ func (c *MultiConn) Add(conns ...Conn) {
c.conns[c2.ID()] = c2 c.conns[c2.ID()] = c2
go c.fanInSingle(c2) go c.fanInSingle(c2)
log.Info("MultiConn: added %s", c2) log.Infof("MultiConn: added %s", c2)
} }
} }
...@@ -146,7 +146,7 @@ func (c *MultiConn) fanOut() { ...@@ -146,7 +146,7 @@ func (c *MultiConn) fanOut() {
// send data out through our "best connection" // send data out through our "best connection"
case m, more := <-c.duplex.Out: case m, more := <-c.duplex.Out:
if !more { if !more {
log.Info("%s out channel closed", c) log.Infof("%s out channel closed", c)
return return
} }
sc := c.BestConn() sc := c.BestConn()
...@@ -156,7 +156,7 @@ func (c *MultiConn) fanOut() { ...@@ -156,7 +156,7 @@ func (c *MultiConn) fanOut() {
} }
i++ i++
log.Info("%s sending (%d)", sc, i) log.Infof("%s sending (%d)", sc, i)
sc.Out() <- m sc.Out() <- m
} }
} }
...@@ -170,7 +170,7 @@ func (c *MultiConn) fanInSingle(child Conn) { ...@@ -170,7 +170,7 @@ func (c *MultiConn) fanInSingle(child Conn) {
// cleanup all data associated with this child Connection. // cleanup all data associated with this child Connection.
defer func() { defer func() {
log.Info("closing: %s", child) log.Infof("closing: %s", child)
// in case it still is in the map, remove it. // in case it still is in the map, remove it.
c.Lock() c.Lock()
...@@ -197,11 +197,11 @@ func (c *MultiConn) fanInSingle(child Conn) { ...@@ -197,11 +197,11 @@ func (c *MultiConn) fanInSingle(child Conn) {
case m, more := <-child.In(): // receiving data case m, more := <-child.In(): // receiving data
if !more { if !more {
log.Info("%s in channel closed", child) log.Infof("%s in channel closed", child)
return // closed return // closed
} }
i++ i++
log.Info("%s received (%d)", child, i) log.Infof("%s received (%d)", child, i)
c.duplex.In <- m c.duplex.In <- m
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论