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:
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user