1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-10-25 02:16:56 +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