Use same input args for command functions

This commit is contained in:
tidwall 2021-09-13 10:52:20 -07:00
parent 8829b8ffc3
commit 18438e5783
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
}
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()
vs := msg.Args[1:]
var ok bool

View File

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

View File

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

View File

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

View File

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