mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 01:07:06 +08:00
Add info command
This commit is contained in:
@@ -132,6 +132,9 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
cmdName := strings.ToLower(string(cmdLine[0]))
|
cmdName := strings.ToLower(string(cmdLine[0]))
|
||||||
|
if cmdName == "info" {
|
||||||
|
return database2.Info(c, cmdLine)
|
||||||
|
}
|
||||||
if cmdName == "auth" {
|
if cmdName == "auth" {
|
||||||
return database2.Auth(c, cmdLine[1:])
|
return database2.Auth(c, cmdLine[1:])
|
||||||
}
|
}
|
||||||
|
@@ -8,6 +8,7 @@ type CmdLine = [][]byte
|
|||||||
func makeRouter() map[string]CmdFunc {
|
func makeRouter() map[string]CmdFunc {
|
||||||
routerMap := make(map[string]CmdFunc)
|
routerMap := make(map[string]CmdFunc)
|
||||||
routerMap["ping"] = ping
|
routerMap["ping"] = ping
|
||||||
|
routerMap["info"] = info
|
||||||
|
|
||||||
routerMap["prepare"] = execPrepare
|
routerMap["prepare"] = execPrepare
|
||||||
routerMap["commit"] = execCommit
|
routerMap["commit"] = execCommit
|
||||||
|
@@ -11,6 +11,10 @@ func ping(cluster *Cluster, c redis.Connection, cmdLine CmdLine) redis.Reply {
|
|||||||
return cluster.db.Exec(c, cmdLine)
|
return cluster.db.Exec(c, cmdLine)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func info(cluster *Cluster, c redis.Connection, cmdLine CmdLine) redis.Reply {
|
||||||
|
return cluster.db.Exec(c, cmdLine)
|
||||||
|
}
|
||||||
|
|
||||||
/*----- utils -------*/
|
/*----- utils -------*/
|
||||||
|
|
||||||
func makeArgs(cmd string, args ...string) [][]byte {
|
func makeArgs(cmd string, args ...string) [][]byte {
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/hdt3213/godis/lib/utils"
|
"github.com/hdt3213/godis/lib/utils"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -136,4 +137,9 @@ func SetupConfig(configFilename string) {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
Properties = parse(file)
|
Properties = parse(file)
|
||||||
Properties.RunID = utils.RandString(40)
|
Properties.RunID = utils.RandString(40)
|
||||||
|
configFilePath, err := filepath.Abs(configFilename)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Properties.CfPath = configFilePath
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/hdt3213/godis/config"
|
"github.com/hdt3213/godis/config"
|
||||||
"github.com/hdt3213/godis/interface/redis"
|
"github.com/hdt3213/godis/interface/redis"
|
||||||
"github.com/hdt3213/godis/redis/protocol"
|
"github.com/hdt3213/godis/redis/protocol"
|
||||||
|
"github.com/hdt3213/godis/tcp"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -25,21 +26,28 @@ func Ping(c redis.Connection, args [][]byte) redis.Reply {
|
|||||||
// Info the information of the godis server returned by the INFO command
|
// Info the information of the godis server returned by the INFO command
|
||||||
func Info(c redis.Connection, args [][]byte) redis.Reply {
|
func Info(c redis.Connection, args [][]byte) redis.Reply {
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
return protocol.MakeBulkReply(GenGodisInfoString())
|
infoCommandList := [...]string{"server", "client", "cluster"}
|
||||||
|
var allSection []byte
|
||||||
|
for _, s := range infoCommandList {
|
||||||
|
allSection = append(allSection, GenGodisInfoString(s)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return protocol.MakeBulkReply(allSection)
|
||||||
} else if len(args) == 2 {
|
} else if len(args) == 2 {
|
||||||
section := strings.ToLower(string(args[1]))
|
section := strings.ToLower(string(args[1]))
|
||||||
switch section {
|
switch section {
|
||||||
case "server":
|
case "server":
|
||||||
reply := GenGodisInfoString()
|
reply := GenGodisInfoString("server")
|
||||||
return protocol.MakeBulkReply(reply)
|
return protocol.MakeBulkReply(reply)
|
||||||
|
case "client":
|
||||||
|
return protocol.MakeBulkReply(GenGodisInfoString("client"))
|
||||||
|
case "cluster":
|
||||||
|
return protocol.MakeBulkReply(GenGodisInfoString("cluster"))
|
||||||
default:
|
default:
|
||||||
return protocol.MakeNullBulkReply()
|
return protocol.MakeNullBulkReply()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return protocol.MakeErrReply("ERR wrong number of arguments for 'info' command")
|
|
||||||
}
|
}
|
||||||
|
return protocol.MakeErrReply("ERR wrong number of arguments for 'info' command")
|
||||||
return &protocol.NullBulkReply{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth validate client's password
|
// Auth validate client's password
|
||||||
@@ -65,8 +73,10 @@ func isAuthenticated(c redis.Connection) bool {
|
|||||||
return c.GetPassword() == config.Properties.RequirePass
|
return c.GetPassword() == config.Properties.RequirePass
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenGodisInfoString() []byte {
|
func GenGodisInfoString(section string) []byte {
|
||||||
startUpTimeFromNow := getGodisRunningTime()
|
startUpTimeFromNow := getGodisRunningTime()
|
||||||
|
switch section {
|
||||||
|
case "server":
|
||||||
s := fmt.Sprintf("# Server\r\n"+
|
s := fmt.Sprintf("# Server\r\n"+
|
||||||
"godis_version:%s\r\n"+
|
"godis_version:%s\r\n"+
|
||||||
//"godis_git_sha1:%s\r\n"+
|
//"godis_git_sha1:%s\r\n"+
|
||||||
@@ -103,6 +113,35 @@ func GenGodisInfoString() []byte {
|
|||||||
//TODO,
|
//TODO,
|
||||||
config.Properties.CfPath)
|
config.Properties.CfPath)
|
||||||
return []byte(s)
|
return []byte(s)
|
||||||
|
case "client":
|
||||||
|
s := fmt.Sprintf("# Clients\r\n"+
|
||||||
|
"connected_clients:%d\r\n",
|
||||||
|
//"client_recent_max_input_buffer:%d\r\n"+
|
||||||
|
//"client_recent_max_output_buffer:%d\r\n"+
|
||||||
|
//"blocked_clients:%d\n",
|
||||||
|
tcp.ClientCounter,
|
||||||
|
//TODO,
|
||||||
|
//TODO,
|
||||||
|
//TODO,
|
||||||
|
)
|
||||||
|
return []byte(s)
|
||||||
|
case "cluster":
|
||||||
|
if getGodisRunningMode() == config.ClusterMode {
|
||||||
|
s := fmt.Sprintf("# Cluster\r\n"+
|
||||||
|
"cluster_enabled:%s\r\n",
|
||||||
|
"1",
|
||||||
|
)
|
||||||
|
return []byte(s)
|
||||||
|
} else {
|
||||||
|
s := fmt.Sprintf("# Cluster\r\n"+
|
||||||
|
"cluster_enabled:%s\r\n",
|
||||||
|
"0",
|
||||||
|
)
|
||||||
|
return []byte(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// getGodisRunningMode return godis running mode
|
// getGodisRunningMode return godis running mode
|
||||||
|
@@ -46,6 +46,10 @@ func TestInfo(t *testing.T) {
|
|||||||
asserts.AssertNotError(t, ret)
|
asserts.AssertNotError(t, ret)
|
||||||
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "server"))
|
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "server"))
|
||||||
asserts.AssertNotError(t, ret)
|
asserts.AssertNotError(t, ret)
|
||||||
|
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "client"))
|
||||||
|
asserts.AssertNotError(t, ret)
|
||||||
|
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "cluster"))
|
||||||
|
asserts.AssertNotError(t, ret)
|
||||||
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "SeRvEr"))
|
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "SeRvEr"))
|
||||||
asserts.AssertNotError(t, ret)
|
asserts.AssertNotError(t, ret)
|
||||||
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "abc", "bde"))
|
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "abc", "bde"))
|
||||||
|
@@ -25,6 +25,9 @@ type Config struct {
|
|||||||
Timeout time.Duration `yaml:"timeout"`
|
Timeout time.Duration `yaml:"timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClientCounter Record the number of clients in the current Godis server
|
||||||
|
var ClientCounter int
|
||||||
|
|
||||||
// ListenAndServeWithSignal binds port and handle requests, blocking until receive stop signal
|
// ListenAndServeWithSignal binds port and handle requests, blocking until receive stop signal
|
||||||
func ListenAndServeWithSignal(cfg *Config, handler tcp.Handler) error {
|
func ListenAndServeWithSignal(cfg *Config, handler tcp.Handler) error {
|
||||||
closeChan := make(chan struct{})
|
closeChan := make(chan struct{})
|
||||||
@@ -75,10 +78,12 @@ func ListenAndServe(listener net.Listener, handler tcp.Handler, closeChan <-chan
|
|||||||
}
|
}
|
||||||
// handle
|
// handle
|
||||||
logger.Info("accept link")
|
logger.Info("accept link")
|
||||||
|
ClientCounter++
|
||||||
waitDone.Add(1)
|
waitDone.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
waitDone.Done()
|
waitDone.Done()
|
||||||
|
ClientCounter--
|
||||||
}()
|
}()
|
||||||
handler.Handle(ctx, conn)
|
handler.Handle(ctx, conn)
|
||||||
}()
|
}()
|
||||||
|
Reference in New Issue
Block a user