tile38/internal/server/readonly.go

45 lines
849 B
Go
Raw Permalink Normal View History

package server
2016-03-05 02:08:16 +03:00
import (
"strings"
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
)
func (s *Server) cmdReadOnly(msg *Message) (res resp.Value, err error) {
2016-03-29 15:53:53 +03:00
start := time.Now()
vs := msg.Args[1:]
2016-03-05 02:08:16 +03:00
var arg string
2016-03-29 15:53:53 +03:00
var ok bool
2017-10-05 18:20:40 +03:00
2016-03-29 15:53:53 +03:00
if vs, arg, ok = tokenval(vs); !ok || arg == "" {
return NOMessage, errInvalidNumberOfArguments
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
if len(vs) != 0 {
return NOMessage, errInvalidNumberOfArguments
2016-03-05 02:08:16 +03:00
}
2016-03-29 15:53:53 +03:00
update := false
2016-03-05 02:08:16 +03:00
switch strings.ToLower(arg) {
default:
return NOMessage, errInvalidArgument(arg)
2016-03-05 02:08:16 +03:00
case "yes":
if !s.config.readOnly() {
2016-03-29 15:53:53 +03:00
update = 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
}
case "no":
if s.config.readOnly() {
2016-03-29 15:53:53 +03:00
update = 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
}
}
2016-03-29 15:53:53 +03:00
if update {
s.config.write(false)
2016-03-05 02:08:16 +03:00
}
return OKMessage(msg, start), nil
2016-03-05 02:08:16 +03:00
}