Use same input args for command functions

This commit is contained in:
tidwall 2021-09-13 10:52:20 -07:00
parent 7ff0d18868
commit 19deea10c0
5 changed files with 28 additions and 23 deletions

View File

@ -441,7 +441,8 @@ func (server *Server) cmdDrop(msg *Message) (res resp.Value, d commandDetails, e
return return
} }
func (server *Server) cmdRename(msg *Message, nx bool) (res resp.Value, d commandDetails, err error) { func (server *Server) cmdRename(msg *Message) (res resp.Value, d commandDetails, err error) {
nx := msg.Command() == "renamenx"
start := time.Now() start := time.Now()
vs := msg.Args[1:] vs := msg.Args[1:]
var ok bool var ok bool

View File

@ -73,7 +73,7 @@ func (s *Server) backgroundExpireHooks(now time.Time) {
}) })
for _, msg := range msgs { for _, msg := range msgs {
_, d, err := s.cmdDelHook(msg, msg.Args[0] == "delchan") _, d, err := s.cmdDelHook(msg)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -26,9 +26,10 @@ func byHookName(a, b interface{}) bool {
return a.(*Hook).Name < b.(*Hook).Name return a.(*Hook).Name < b.(*Hook).Name
} }
func (s *Server) cmdSetHook(msg *Message, chanCmd bool) ( func (s *Server) cmdSetHook(msg *Message) (
res resp.Value, d commandDetails, err error, res resp.Value, d commandDetails, err error,
) { ) {
channel := msg.Command() == "setchan"
start := time.Now() start := time.Now()
vs := msg.Args[1:] vs := msg.Args[1:]
var name, urls, cmd string var name, urls, cmd string
@ -37,7 +38,7 @@ func (s *Server) cmdSetHook(msg *Message, chanCmd bool) (
return NOMessage, d, errInvalidNumberOfArguments return NOMessage, d, errInvalidNumberOfArguments
} }
var endpoints []string var endpoints []string
if chanCmd { if channel {
endpoints = []string{"local://" + name} endpoints = []string{"local://" + name}
} else { } else {
if vs, urls, ok = tokenval(vs); !ok || urls == "" { if vs, urls, ok = tokenval(vs); !ok || urls == "" {
@ -129,7 +130,7 @@ func (s *Server) cmdSetHook(msg *Message, chanCmd bool) (
Message: cmsg, Message: cmsg,
epm: s.epc, epm: s.epc,
Metas: metas, Metas: metas,
channel: chanCmd, channel: channel,
cond: sync.NewCond(&sync.Mutex{}), cond: sync.NewCond(&sync.Mutex{}),
counter: &s.statsTotalMsgsSent, counter: &s.statsTotalMsgsSent,
} }
@ -137,7 +138,7 @@ func (s *Server) cmdSetHook(msg *Message, chanCmd bool) (
hook.expires = hook.expires =
time.Now().Add(time.Duration(expires * float64(time.Second))) time.Now().Add(time.Duration(expires * float64(time.Second)))
} }
if !chanCmd { if !channel {
hook.db = s.qdb hook.db = s.qdb
} }
var wr bytes.Buffer var wr bytes.Buffer
@ -151,7 +152,7 @@ func (s *Server) cmdSetHook(msg *Message, chanCmd bool) (
} }
prevHook, _ := s.hooks.Get(&Hook{Name: name}).(*Hook) prevHook, _ := s.hooks.Get(&Hook{Name: name}).(*Hook)
if prevHook != nil { if prevHook != nil {
if prevHook.channel != chanCmd { if prevHook.channel != channel {
return NOMessage, d, return NOMessage, d,
errors.New("hooks and channels cannot share the same name") errors.New("hooks and channels cannot share the same name")
} }
@ -240,9 +241,10 @@ func byHookExpires(a, b interface{}) bool {
return ha.Name < hb.Name return ha.Name < hb.Name
} }
func (s *Server) cmdDelHook(msg *Message, chanCmd bool) ( func (s *Server) cmdDelHook(msg *Message) (
res resp.Value, d commandDetails, err error, res resp.Value, d commandDetails, err error,
) { ) {
channel := msg.Command() == "delchan"
start := time.Now() start := time.Now()
vs := msg.Args[1:] vs := msg.Args[1:]
@ -255,7 +257,7 @@ func (s *Server) cmdDelHook(msg *Message, chanCmd bool) (
return NOMessage, d, errInvalidNumberOfArguments return NOMessage, d, errInvalidNumberOfArguments
} }
hook, _ := s.hooks.Get(&Hook{Name: name}).(*Hook) hook, _ := s.hooks.Get(&Hook{Name: name}).(*Hook)
if hook != nil && hook.channel == chanCmd { if hook != nil && hook.channel == channel {
hook.Close() hook.Close()
// remove hook from maps // remove hook from maps
s.hooks.Delete(hook) s.hooks.Delete(hook)
@ -295,9 +297,10 @@ func (s *Server) cmdDelHook(msg *Message, chanCmd bool) (
return return
} }
func (s *Server) cmdPDelHook(msg *Message, channel bool) ( func (s *Server) cmdPDelHook(msg *Message) (
res resp.Value, d commandDetails, err error, res resp.Value, d commandDetails, err error,
) { ) {
channel := msg.Command() == "pdelchan"
start := time.Now() start := time.Now()
vs := msg.Args[1:] vs := msg.Args[1:]
@ -378,9 +381,10 @@ func (s *Server) forEachHookByPattern(
}) })
} }
func (s *Server) cmdHooks(msg *Message, channel bool) ( func (s *Server) cmdHooks(msg *Message) (
res resp.Value, err error, res resp.Value, err error,
) { ) {
channel := msg.Command() == "chans"
start := time.Now() start := time.Now()
vs := msg.Args[1:] vs := msg.Args[1:]

View File

@ -604,9 +604,9 @@ func (s *Server) commandInScript(msg *Message) (
case "expire": case "expire":
res, d, err = s.cmdExpire(msg) res, d, err = s.cmdExpire(msg)
case "rename": case "rename":
res, d, err = s.cmdRename(msg, false) res, d, err = s.cmdRename(msg)
case "renamenx": case "renamenx":
res, d, err = s.cmdRename(msg, true) res, d, err = s.cmdRename(msg)
case "persist": case "persist":
res, d, err = s.cmdPersist(msg) res, d, err = s.cmdPersist(msg)
case "ttl": case "ttl":

View File

@ -1036,25 +1036,25 @@ func (server *Server) command(msg *Message, client *Client) (
case "flushdb": case "flushdb":
res, d, err = server.cmdFlushDB(msg) res, d, err = server.cmdFlushDB(msg)
case "rename": case "rename":
res, d, err = server.cmdRename(msg, false) res, d, err = server.cmdRename(msg)
case "renamenx": case "renamenx":
res, d, err = server.cmdRename(msg, true) res, d, err = server.cmdRename(msg)
case "sethook": case "sethook":
res, d, err = server.cmdSetHook(msg, false) res, d, err = server.cmdSetHook(msg)
case "delhook": case "delhook":
res, d, err = server.cmdDelHook(msg, false) res, d, err = server.cmdDelHook(msg)
case "pdelhook": case "pdelhook":
res, d, err = server.cmdPDelHook(msg, false) res, d, err = server.cmdPDelHook(msg)
case "hooks": case "hooks":
res, err = server.cmdHooks(msg, false) res, err = server.cmdHooks(msg)
case "setchan": case "setchan":
res, d, err = server.cmdSetHook(msg, true) res, d, err = server.cmdSetHook(msg)
case "delchan": case "delchan":
res, d, err = server.cmdDelHook(msg, true) res, d, err = server.cmdDelHook(msg)
case "pdelchan": case "pdelchan":
res, d, err = server.cmdPDelHook(msg, true) res, d, err = server.cmdPDelHook(msg)
case "chans": case "chans":
res, err = server.cmdHooks(msg, true) res, err = server.cmdHooks(msg)
case "expire": case "expire":
res, d, err = server.cmdExpire(msg) res, d, err = server.cmdExpire(msg)
case "persist": case "persist":