1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-10-24 15:12:55 +08:00

add a global timeout flag for to be setting timeouts

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2015-07-20 12:34:34 -07:00
parent 13d49d9abb
commit 6083007987
4 changed files with 38 additions and 19 deletions

View File

@ -7,6 +7,7 @@ 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"
@ -106,20 +107,36 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithCancel(node.Context())
defer cancel()
/*
TODO(cryptix): the next line looks very fishy to me..
It looks like the the context for the command request beeing prepared here is shared across all incoming requests..
I assume it really isn't because ServeHTTP() doesn't take a pointer receiver, but it's really subtule..
tout, found, err := req.Option("timeout").String()
if err != nil {
err = fmt.Errorf("error parsing timeout option: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Shouldn't the context be just put on the command request?
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
}
ps: take note of the name clash - commands.Context != context.Context
*/
i.ctx.Context = ctx
req.SetContext(i.ctx)
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
req.SetContext(cmdctx)
// call the command
res := i.root.Call(req)

View File

@ -154,22 +154,25 @@ func (ov OptionValue) String() (value string, found bool, err error) {
// Flag names
const (
EncShort = "enc"
EncLong = "encoding"
RecShort = "r"
RecLong = "recursive"
ChanOpt = "stream-channels"
EncShort = "enc"
EncLong = "encoding"
RecShort = "r"
RecLong = "recursive"
ChanOpt = "stream-channels"
TimeoutOpt = "timeout"
)
// options that are used by this package
var OptionEncodingType = StringOption(EncShort, EncLong, "The encoding type the output should be encoded with (json, xml, or text)")
var OptionRecursivePath = BoolOption(RecShort, RecLong, "Add directory paths recursively")
var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output")
var OptionTimeout = StringOption(TimeoutOpt, "set a global timeout on the command")
// global options, added to every command
var globalOptions = []Option{
OptionEncodingType,
OptionStreamChannels,
OptionTimeout,
}
// the above array of Options, wrapped in a Command

View File

@ -9,7 +9,6 @@ import (
"strings"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
@ -178,7 +177,7 @@ func getBlockForKey(req cmds.Request, skey string) (*blocks.Block, error) {
}
k := key.Key(h)
b, err := n.Blocks.GetBlock(context.TODO(), k)
b, err := n.Blocks.GetBlock(req.Context().Context, k)
if err != nil {
return nil, err
}

View File

@ -81,7 +81,7 @@ it contains, with the following format:
Links: make([]LsLink, len(dagnode.Links)),
}
for j, link := range dagnode.Links {
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
ctx, cancel := context.WithTimeout(req.Context().Context, time.Minute)
defer cancel()
link.Node, err = link.GetNode(ctx, node.DAG)
if err != nil {