1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-10-25 10:27:01 +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" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors" "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" 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) http.Error(w, err.Error(), http.StatusInternalServerError)
return 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 tctx, cancel := context.WithTimeout(node.Context(), duration)
*/ defer cancel()
i.ctx.Context = ctx ctx = tctx
req.SetContext(i.ctx) } 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 // call the command
res := i.root.Call(req) res := i.root.Call(req)

View File

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

View File

@ -9,7 +9,6 @@ import (
"strings" "strings"
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"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/blocks" "github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands" 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) k := key.Key(h)
b, err := n.Blocks.GetBlock(context.TODO(), k) b, err := n.Blocks.GetBlock(req.Context().Context, k)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -81,7 +81,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(context.TODO(), time.Minute) ctx, cancel := context.WithTimeout(req.Context().Context, time.Minute)
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 {