diff --git a/internal/server/crud.go b/internal/server/crud.go index 5c0e1c03..08142c4f 100644 --- a/internal/server/crud.go +++ b/internal/server/crud.go @@ -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 diff --git a/internal/server/expire.go b/internal/server/expire.go index 16030c65..81b88d8a 100644 --- a/internal/server/expire.go +++ b/internal/server/expire.go @@ -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) } diff --git a/internal/server/hooks.go b/internal/server/hooks.go index 71e5dd58..c6697d8a 100644 --- a/internal/server/hooks.go +++ b/internal/server/hooks.go @@ -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:] diff --git a/internal/server/scripts.go b/internal/server/scripts.go index 98e16d94..7e0b739e 100644 --- a/internal/server/scripts.go +++ b/internal/server/scripts.go @@ -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": diff --git a/internal/server/server.go b/internal/server/server.go index 831589ce..0dee910a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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":