tile38/internal/server/readonly.go

51 lines
790 B
Go
Raw Normal View History

package server
2016-03-05 02:08:16 +03:00
import (
2016-03-29 15:53:53 +03:00
"time"
2016-03-05 02:08:16 +03:00
2017-10-05 18:20:40 +03:00
"github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/log"
2016-03-05 02:08:16 +03:00
)
2022-09-27 03:58:51 +03:00
// READONLY yes|no
func (s *Server) cmdREADONLY(msg *Message) (resp.Value, error) {
2016-03-29 15:53:53 +03:00
start := time.Now()
2017-10-05 18:20:40 +03:00
2022-09-27 03:58:51 +03:00
// >> Args
args := msg.Args
if len(args) != 2 {
return retrerr(errInvalidNumberOfArguments)
2016-03-05 02:08:16 +03:00
}
2022-09-27 03:58:51 +03:00
switch args[1] {
case "yes", "no":
2016-03-05 02:08:16 +03:00
default:
2022-09-27 03:58:51 +03:00
return retrerr(errInvalidArgument(args[1]))
}
// >> Operation
var updated bool
if args[1] == "yes" {
if !s.config.readOnly() {
2022-09-27 03:58:51 +03:00
updated = true
s.config.setReadOnly(true)
2016-03-29 15:53:53 +03:00
log.Info("read only")
2016-03-05 02:08:16 +03:00
}
2022-09-27 03:58:51 +03:00
} else {
if s.config.readOnly() {
2022-09-27 03:58:51 +03:00
updated = true
s.config.setReadOnly(false)
2016-03-29 15:53:53 +03:00
log.Info("read write")
2016-03-05 02:08:16 +03:00
}
}
2022-09-27 03:58:51 +03:00
if updated {
s.config.write(false)
2016-03-05 02:08:16 +03:00
}
2022-09-27 03:58:51 +03:00
// >> Response
return OKMessage(msg, start), nil
2016-03-05 02:08:16 +03:00
}