Commit 87407a99 authored by Jeromy's avatar Jeromy

add context to blockservice Get

parent c2692f3a
......@@ -3,6 +3,9 @@ package blockservice
import (
"bytes"
"testing"
"time"
"code.google.com/p/go.net/context"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
blocks "github.com/jbenet/go-ipfs/blocks"
......@@ -37,7 +40,8 @@ func TestBlocks(t *testing.T) {
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 {
t.Error("failed to retrieve block from BlockService", err)
return
......
......@@ -2,7 +2,6 @@ package blockservice
import (
"fmt"
"time"
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"
......@@ -52,7 +51,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
// GetBlock retrieves a particular block from the service,
// 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)
datai, err := s.Datastore.Get(k.DsKey())
if err == nil {
......@@ -67,7 +66,6 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
}, nil
} else if err == ds.ErrNotFound && s.Remote != nil {
log.Debug("Blockservice: Searching bitswap.")
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
blk, err := s.Remote.Block(ctx, k)
if err != nil {
return nil, err
......
......@@ -5,6 +5,9 @@ import (
"io"
"io/ioutil"
"os"
"time"
"code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/jbenet/go-ipfs/blocks"
......@@ -26,7 +29,8 @@ func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out
k := u.Key(h)
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 {
return fmt.Errorf("block get: %v", err)
}
......
......@@ -2,6 +2,9 @@ package merkledag
import (
"fmt"
"time"
"code.google.com/p/go.net/context"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
blocks "github.com/jbenet/go-ipfs/blocks"
......@@ -204,7 +207,8 @@ func (n *DAGService) Get(k u.Key) (*Node, error) {
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 {
return nil, err
}
......
......@@ -67,7 +67,7 @@ func (c *MultiConn) Add(conns ...Conn) {
defer c.Unlock()
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() {
log.Error(c2)
c.Unlock() // ok to unlock (to log). panicing.
......@@ -82,7 +82,7 @@ func (c *MultiConn) Add(conns ...Conn) {
c.conns[c2.ID()] = 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() {
// send data out through our "best connection"
case m, more := <-c.duplex.Out:
if !more {
log.Info("%s out channel closed", c)
log.Infof("%s out channel closed", c)
return
}
sc := c.BestConn()
......@@ -156,7 +156,7 @@ func (c *MultiConn) fanOut() {
}
i++
log.Info("%s sending (%d)", sc, i)
log.Infof("%s sending (%d)", sc, i)
sc.Out() <- m
}
}
......@@ -170,7 +170,7 @@ func (c *MultiConn) fanInSingle(child Conn) {
// cleanup all data associated with this child Connection.
defer func() {
log.Info("closing: %s", child)
log.Infof("closing: %s", child)
// in case it still is in the map, remove it.
c.Lock()
......@@ -197,11 +197,11 @@ func (c *MultiConn) fanInSingle(child Conn) {
case m, more := <-child.In(): // receiving data
if !more {
log.Info("%s in channel closed", child)
log.Infof("%s in channel closed", child)
return // closed
}
i++
log.Info("%s received (%d)", child, i)
log.Infof("%s received (%d)", child, i)
c.duplex.In <- m
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment