Skip to content

Commit

Permalink
cli: read query timeout from config file; fix cayleygraph#613
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jul 27, 2017
1 parent 6dd377f commit f4d9bdb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cayley_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ store:
# backend-specific options
options:
nosync: false
query:
timeout: 30s
load:
ignore_duplicates: false
ignore_missing: false
Expand Down
7 changes: 3 additions & 4 deletions cmd/cayley/command/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ func NewHttpCmd() *cobra.Command {
printBackendInfo()
p := mustSetupProfile(cmd)
defer mustFinishProfile(p)
timeout, err := cmd.Flags().GetDuration("timeout")
if err != nil {
return err
}

timeout := viper.GetDuration(keyQueryTimeout)
if init, err := cmd.Flags().GetBool("init"); err != nil {
return err
} else if init {
Expand Down Expand Up @@ -71,5 +69,6 @@ func NewHttpCmd() *cobra.Command {
cmd.Flags().DurationP("timeout", "t", 30*time.Second, "elapsed time until an individual query times out")
cmd.Flags().StringVar(&chttp.AssetsPath, "assets", "", "explicit path to the HTTP assets")
registerLoadFlags(cmd)
viper.BindPFlag(keyQueryTimeout, cmd.Flags().Lookup("timeout"))
return cmd
}
16 changes: 8 additions & 8 deletions cmd/cayley/command/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"

"github.com/cayleygraph/cayley/clog"
Expand All @@ -20,6 +21,10 @@ import (
"github.com/cayleygraph/cayley/query"
)

const (
keyQueryTimeout = "query.timeout"
)

func getContext() (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
Expand Down Expand Up @@ -64,6 +69,7 @@ func registerQueryFlags(cmd *cobra.Command) {
cmd.Flags().Bool("init", false, "initialize the database before using it")
cmd.Flags().String("lang", "gizmo", `query language to use ("`+strings.Join(langs, `", "`)+`")`)
cmd.Flags().DurationP("timeout", "t", 30*time.Second, "elapsed time until an individual query times out")
viper.BindPFlag(keyQueryTimeout, cmd.Flags().Lookup("timeout"))
registerLoadFlags(cmd)
}

Expand All @@ -85,10 +91,7 @@ func NewReplCmd() *cobra.Command {
ctx, cancel := getContext()
defer cancel()

timeout, err := cmd.Flags().GetDuration("timeout")
if err != nil {
return err
}
timeout := viper.GetDuration("timeout")
lang, _ := cmd.Flags().GetString("lang")
return db.Repl(ctx, h, lang, timeout)
},
Expand Down Expand Up @@ -129,10 +132,7 @@ func NewQueryCmd() *cobra.Command {
ctx, cancel := getContext()
defer cancel()

timeout, err := cmd.Flags().GetDuration("timeout")
if err != nil {
return err
}
timeout := viper.GetDuration("timeout")
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
Expand Down

0 comments on commit f4d9bdb

Please sign in to comment.