avoid recover calls for non-lua queries

The defer recover() pattern is expensive and only Lua queries require
it, so for now we'll just check if lua is being used before calling.
This commit is contained in:
Josh Baker 2018-03-05 11:10:40 -07:00
parent 3568edf44c
commit 7538cc73ff
2 changed files with 45 additions and 34 deletions

View File

@ -27,14 +27,16 @@ func (c *Controller) cmdScan(msg *server.Message) (res resp.Value, err error) {
vs := msg.Values[1:] vs := msg.Values[1:]
s, err := c.cmdScanArgs(vs) s, err := c.cmdScanArgs(vs)
defer s.Close() if s.usingLua() {
defer func() { defer s.Close()
if r := recover(); r != nil { defer func() {
res = server.NOMessage if r := recover(); r != nil {
err = errors.New(r.(string)) res = server.NOMessage
return err = errors.New(r.(string))
} return
}() }
}()
}
if err != nil { if err != nil {
return server.NOMessage, err return server.NOMessage, err
} }

View File

@ -42,11 +42,15 @@ func (s liveFenceSwitches) Error() string {
} }
func (s liveFenceSwitches) Close() { func (s liveFenceSwitches) Close() {
for _, whereeval := range s.searchScanBaseTokens.whereevals { for _, whereeval := range s.whereevals {
whereeval.Close() whereeval.Close()
} }
} }
func (s liveFenceSwitches) usingLua() bool {
return len(s.whereevals) > 0
}
func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string) (s liveFenceSwitches, err error) { func (c *Controller) cmdSearchArgs(cmd string, vs []resp.Value, types []string) (s liveFenceSwitches, err error) {
if vs, s.searchScanBaseTokens, err = c.parseSearchScanBaseTokens(cmd, vs); err != nil { if vs, s.searchScanBaseTokens, err = c.parseSearchScanBaseTokens(cmd, vs); err != nil {
return return
@ -295,14 +299,16 @@ func (c *Controller) cmdNearby(msg *server.Message) (res resp.Value, err error)
vs := msg.Values[1:] vs := msg.Values[1:]
wr := &bytes.Buffer{} wr := &bytes.Buffer{}
s, err := c.cmdSearchArgs("nearby", vs, nearbyTypes) s, err := c.cmdSearchArgs("nearby", vs, nearbyTypes)
defer s.Close() if s.usingLua() {
defer func() { defer s.Close()
if r := recover(); r != nil { defer func() {
res = server.NOMessage if r := recover(); r != nil {
err = errors.New(r.(string)) res = server.NOMessage
return err = errors.New(r.(string))
} return
}() }
}()
}
if err != nil { if err != nil {
return server.NOMessage, err return server.NOMessage, err
} }
@ -402,15 +408,16 @@ func (c *Controller) cmdWithinOrIntersects(cmd string, msg *server.Message) (res
wr := &bytes.Buffer{} wr := &bytes.Buffer{}
s, err := c.cmdSearchArgs(cmd, vs, withinOrIntersectsTypes) s, err := c.cmdSearchArgs(cmd, vs, withinOrIntersectsTypes)
defer s.Close() if s.usingLua() {
defer func() { defer s.Close()
if r := recover(); r != nil { defer func() {
res = server.NOMessage if r := recover(); r != nil {
err = errors.New(r.(string)) res = server.NOMessage
return err = errors.New(r.(string))
} return
}() }
}()
}
if err != nil { if err != nil {
return server.NOMessage, err return server.NOMessage, err
} }
@ -485,14 +492,16 @@ func (c *Controller) cmdSearch(msg *server.Message) (res resp.Value, err error)
wr := &bytes.Buffer{} wr := &bytes.Buffer{}
s, err := c.cmdSeachValuesArgs(vs) s, err := c.cmdSeachValuesArgs(vs)
defer s.Close() if s.usingLua() {
defer func() { defer s.Close()
if r := recover(); r != nil { defer func() {
res = server.NOMessage if r := recover(); r != nil {
err = errors.New(r.(string)) res = server.NOMessage
return err = errors.New(r.(string))
} return
}() }
}()
}
if err != nil { if err != nil {
return server.NOMessage, err return server.NOMessage, err
} }