Enable ability to set a global query timeout duration

This commit is contained in:
Tobie Morgan Hitchcock 2018-05-02 02:42:16 +01:00
parent 9f0ea3ee61
commit 7f6d1565ff
3 changed files with 23 additions and 0 deletions

View file

@ -95,6 +95,8 @@ func init() {
startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Sync, "db-sync", 0, flag("sync"))
startCmd.PersistentFlags().DurationVar(&opts.DB.Proc.Shrink, "db-shrink", 0, flag("shrink"))
startCmd.PersistentFlags().DurationVar(&opts.Query.Timeout, "query-timeout", 0, "")
startCmd.PersistentFlags().StringSliceVarP(&opts.Node.Join, "join", "j", nil, flag("join"))
startCmd.PersistentFlags().StringVarP(&opts.DB.Code, "key", "k", "", flag("key"))

View file

@ -74,6 +74,10 @@ type Options struct {
Join []string // Slice of cluster peers to join
}
Query struct {
Timeout time.Duration // Fixed query timeout
}
Format struct {
Type string // Stores the cli output format
}

View file

@ -21,6 +21,7 @@ import (
"runtime/debug"
"github.com/abcum/surreal/cnf"
"github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/log"
"github.com/abcum/surreal/mem"
@ -269,6 +270,22 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf
// can monitor the running time, and ensure
// it runs no longer than specified.
if cnf.Settings.Query.Timeout > 0 {
if ctx.Value(ctxKeyKind) != cnf.AuthKV {
ctx, canc = context.WithTimeout(ctx, cnf.Settings.Query.Timeout)
defer func() {
if tim := ctx.Err(); err == nil && tim != nil {
res, err = nil, &TimerError{timer: cnf.Settings.Query.Timeout}
}
canc()
}()
}
}
// Mark the beginning of this statement so we
// can monitor the running time, and ensure
// it runs no longer than specified.
if stm, ok := stm.(sql.KillableStatement); ok {
if stm.Duration() > 0 {
ctx, canc = context.WithTimeout(ctx, stm.Duration())