提交 5d8e1504 作者: rht

Replace WithTimeout with WithCancel whenever possible

License: MIT
Signed-off-by: 's avatarrht <rhtbot@gmail.com>
上级 d32177cd
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"text/tabwriter" "text/tabwriter"
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...@@ -81,7 +80,7 @@ it contains, with the following format: ...@@ -81,7 +80,7 @@ it contains, with the following format:
Links: make([]LsLink, len(dagnode.Links)), Links: make([]LsLink, len(dagnode.Links)),
} }
for j, link := range dagnode.Links { for j, link := range dagnode.Links {
ctx, cancel := context.WithTimeout(req.Context(), time.Minute) ctx, cancel := context.WithCancel(req.Context())
defer cancel() defer cancel()
link.Node, err = link.GetNode(ctx, node.DAG) link.Node, err = link.GetNode(ctx, node.DAG)
if err != nil { if err != nil {
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"io" "io"
"sort" "sort"
"text/tabwriter" "text/tabwriter"
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...@@ -111,7 +110,7 @@ size is the IPFS link size. ...@@ -111,7 +110,7 @@ size is the IPFS link size.
links := make([]LsLink, len(merkleNode.Links)) links := make([]LsLink, len(merkleNode.Links))
output.Objects[hash].Links = links output.Objects[hash].Links = links
for i, link := range merkleNode.Links { for i, link := range merkleNode.Links {
getCtx, cancel := context.WithTimeout(ctx, time.Minute) getCtx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
link.Node, err = link.GetNode(getCtx, node.DAG) link.Node, err = link.GetNode(getCtx, node.DAG)
if err != nil { if err != nil {
......
...@@ -321,8 +321,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { ...@@ -321,8 +321,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) {
log.Warning("trying peer info: ", p) log.Warning("trying peer info: ", p)
ctx, _ := context.WithTimeout(n.Context(), time.Second*10) ctx, _ := context.WithTimeout(n.Context(), time.Second*10)
err := n.PeerHost.Connect(ctx, p) if err := n.PeerHost.Connect(ctx, p); err != nil {
if err != nil {
log.Warning("Failed to connect to peer found by discovery: ", err) log.Warning("Failed to connect to peer found by discovery: ", err)
} }
} }
......
...@@ -15,7 +15,6 @@ package corerepo ...@@ -15,7 +15,6 @@ package corerepo
import ( import (
"fmt" "fmt"
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...@@ -42,7 +41,7 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ...@@ -42,7 +41,7 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
return nil, err return nil, err
} }
ctx, cancel := context.WithTimeout(ctx, time.Minute) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
err = n.Pinning.Pin(ctx, dagnode, recursive) err = n.Pinning.Pin(ctx, dagnode, recursive)
if err != nil { if err != nil {
...@@ -74,7 +73,7 @@ func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool ...@@ -74,7 +73,7 @@ func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool
for _, dagnode := range dagnodes { for _, dagnode := range dagnodes {
k, _ := dagnode.Key() k, _ := dagnode.Key()
ctx, cancel := context.WithTimeout(ctx, time.Minute) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
err := n.Pinning.Unpin(ctx, k, recursive) err := n.Pinning.Unpin(ctx, k, recursive)
if err != nil { if err != nil {
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
gopath "path" gopath "path"
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...@@ -105,7 +104,7 @@ func addNode(n *core.IpfsNode, node *merkledag.Node) error { ...@@ -105,7 +104,7 @@ func addNode(n *core.IpfsNode, node *merkledag.Node) error {
if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage
return err return err
} }
ctx, cancel := context.WithTimeout(n.Context(), time.Minute) ctx, cancel := context.WithCancel(n.Context())
defer cancel() defer cancel()
err := n.Pinning.Pin(ctx, node, true) // ensure we keep it err := n.Pinning.Pin(ctx, node, true) // ensure we keep it
return err return err
......
package coreunix package coreunix
import ( import (
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
...@@ -14,7 +12,7 @@ import ( ...@@ -14,7 +12,7 @@ import (
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) { func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
ukey := key.B58KeyDecode(skey) ukey := key.B58KeyDecode(skey)
ctx, cancel := context.WithTimeout(n.Context(), time.Minute) ctx, cancel := context.WithCancel(n.Context())
defer cancel() defer cancel()
nd, err := n.DAG.Get(ctx, ukey) nd, err := n.DAG.Get(ctx, ukey)
if err != nil { if err != nil {
...@@ -44,7 +42,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error ...@@ -44,7 +42,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) { func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
ukey := key.B58KeyDecode(skey) ukey := key.B58KeyDecode(skey)
ctx, cancel := context.WithTimeout(n.Context(), time.Minute) ctx, cancel := context.WithCancel(n.Context())
defer cancel() defer cancel()
nd, err := n.DAG.Get(ctx, ukey) nd, err := n.DAG.Get(ctx, ukey)
if err != nil { if err != nil {
......
package ipns package ipns
import ( import (
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
...@@ -22,7 +20,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error { ...@@ -22,7 +20,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
return err return err
} }
ctx, cancel := context.WithTimeout(n.Context(), time.Minute) ctx, cancel := context.WithCancel(n.Context())
defer cancel() defer cancel()
err = n.Pinning.Pin(ctx, emptyDir, false) err = n.Pinning.Pin(ctx, emptyDir, false)
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"sync" "sync"
"time"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
...@@ -140,7 +139,7 @@ func (d *Directory) childDir(name string) (*Directory, error) { ...@@ -140,7 +139,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
func (d *Directory) childFromDag(name string) (*dag.Node, error) { func (d *Directory) childFromDag(name string) (*dag.Node, error) {
for _, lnk := range d.node.Links { for _, lnk := range d.node.Links {
if lnk.Name == name { if lnk.Name == name {
ctx, cancel := context.WithTimeout(d.ctx, time.Minute) ctx, cancel := context.WithCancel(d.ctx)
defer cancel() defer cancel()
return lnk.GetNode(ctx, d.fs.dserv) return lnk.GetNode(ctx, d.fs.dserv)
......
...@@ -87,7 +87,7 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]*me ...@@ -87,7 +87,7 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]*me
} }
log.Debug("Resolve dag get.") log.Debug("Resolve dag get.")
ctx, cancel := context.WithTimeout(ctx, time.Minute) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
nd, err := s.DAG.Get(ctx, key.Key(h)) nd, err := s.DAG.Get(ctx, key.Key(h))
if err != nil { if err != nil {
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"errors" "errors"
"io" "io"
"os" "os"
"time"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto" proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
...@@ -460,7 +459,7 @@ func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGSer ...@@ -460,7 +459,7 @@ func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGSer
var modified *mdag.Node var modified *mdag.Node
ndata := new(ft.FSNode) ndata := new(ft.FSNode)
for i, lnk := range nd.Links { for i, lnk := range nd.Links {
_ctx, cancel := context.WithTimeout(ctx, time.Minute) _ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
child, err := lnk.GetNode(ctx, ds) child, err := lnk.GetNode(ctx, ds)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论