mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-25 02:16:56 +08:00
make offline commands respect timeout
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -335,6 +335,13 @@ func callCommand(ctx context.Context, req cmds.Request, root *cmds.Command, cmd
|
||||
} else {
|
||||
log.Debug("Executing command locally")
|
||||
|
||||
ctx, err := cmds.GetContext(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Context().Context = ctx
|
||||
|
||||
// Okay!!!!! NOW we can call the command.
|
||||
res = root.Call(req)
|
||||
|
||||
|
||||
@ -7,10 +7,8 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
u "github.com/ipfs/go-ipfs/util"
|
||||
@ -108,31 +106,13 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tout, found, err := req.Option("timeout").String()
|
||||
ctx, err := cmds.GetContext(node.Context(), req)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error parsing timeout option: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var ctx context.Context
|
||||
if found {
|
||||
duration, err := time.ParseDuration(tout)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error parsing timeout option: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tctx, cancel := context.WithTimeout(node.Context(), duration)
|
||||
defer cancel()
|
||||
ctx = tctx
|
||||
} else {
|
||||
cctx, cancel := context.WithCancel(node.Context())
|
||||
defer cancel()
|
||||
ctx = cctx
|
||||
}
|
||||
|
||||
//ps: take note of the name clash - commands.Context != context.Context
|
||||
cmdctx := i.ctx
|
||||
cmdctx.Context = ctx
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
"github.com/ipfs/go-ipfs/commands/files"
|
||||
@ -297,3 +298,25 @@ func NewRequest(path []string, opts OptMap, args []string, file files.File, cmd
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func GetContext(base context.Context, req Request) (context.Context, error) {
|
||||
tout, found, err := req.Option("timeout").String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing timeout option: %s", err)
|
||||
}
|
||||
|
||||
var ctx context.Context
|
||||
if found {
|
||||
duration, err := time.ParseDuration(tout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing timeout option: %s", err)
|
||||
}
|
||||
|
||||
tctx, _ := context.WithTimeout(base, duration)
|
||||
ctx = tctx
|
||||
} else {
|
||||
cctx, _ := context.WithCancel(base)
|
||||
ctx = cctx
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
@ -60,7 +60,12 @@ on disk.
|
||||
recursive = false
|
||||
}
|
||||
|
||||
added, err := corerepo.Pin(n, req.Arguments(), recursive)
|
||||
go func() {
|
||||
<-req.Context().Context.Done()
|
||||
log.Error("CONTEXT IS OVER!")
|
||||
}()
|
||||
|
||||
added, err := corerepo.Pin(n, req.Context().Context, req.Arguments(), recursive)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
@ -125,7 +130,7 @@ collected if needed.
|
||||
recursive = false // default
|
||||
}
|
||||
|
||||
removed, err := corerepo.Unpin(n, req.Arguments(), recursive)
|
||||
removed, err := corerepo.Unpin(n, req.Context().Context, req.Arguments(), recursive)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
|
||||
@ -25,10 +25,7 @@ import (
|
||||
path "github.com/ipfs/go-ipfs/path"
|
||||
)
|
||||
|
||||
func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]key.Key, error) {
|
||||
// TODO(cryptix): do we want a ctx as first param for (Un)Pin() as well, just like core.Resolve?
|
||||
ctx := n.Context()
|
||||
|
||||
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]key.Key, error) {
|
||||
dagnodes := make([]*merkledag.Node, 0)
|
||||
for _, fpath := range paths {
|
||||
dagnode, err := core.Resolve(ctx, n, path.Path(fpath))
|
||||
@ -62,9 +59,7 @@ func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]key.Key, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]key.Key, error) {
|
||||
// TODO(cryptix): do we want a ctx as first param for (Un)Pin() as well, just like core.Resolve?
|
||||
ctx := n.Context()
|
||||
func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]key.Key, error) {
|
||||
|
||||
dagnodes := make([]*merkledag.Node, 0)
|
||||
for _, fpath := range paths {
|
||||
|
||||
@ -242,7 +242,7 @@ test_expect_success "some are no longer there" '
|
||||
|
||||
test_expect_success "recursive pin fails without objects" '
|
||||
ipfs pin rm "$HASH_DIR1" &&
|
||||
test_must_fail ipfs pin add -r "$HASH_DIR1" 2>err_expected8 &&
|
||||
test_must_fail ipfs pin add -r "$HASH_DIR1" --timeout=500ms 2>err_expected8 &&
|
||||
grep "context deadline exceeded" err_expected8
|
||||
'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user