2014-07-31 10:38:20 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
2014-07-31 14:28:19 +04:00
|
|
|
"github.com/siddontang/go-log/log"
|
2014-07-31 10:38:20 +04:00
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
|
|
|
"io"
|
|
|
|
"net"
|
2014-07-31 14:28:19 +04:00
|
|
|
"runtime"
|
2014-07-31 10:38:20 +04:00
|
|
|
"strconv"
|
2014-07-31 14:28:19 +04:00
|
|
|
"strings"
|
2014-07-31 10:38:20 +04:00
|
|
|
)
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
var errReadRequest = errors.New("invalid request protocol")
|
|
|
|
|
|
|
|
type respClient struct {
|
|
|
|
app *App
|
|
|
|
ldb *ledis.Ledis
|
|
|
|
db *ledis.DB
|
|
|
|
|
2014-07-31 10:38:20 +04:00
|
|
|
conn net.Conn
|
2014-07-31 14:28:19 +04:00
|
|
|
rb *bufio.Reader
|
|
|
|
|
|
|
|
req *requestContext
|
2014-07-31 10:38:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
type respWriter struct {
|
2014-07-31 10:38:20 +04:00
|
|
|
buff *bufio.Writer
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func newClientRESP(conn net.Conn, app *App) {
|
|
|
|
c := new(respClient)
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
c.app = app
|
|
|
|
c.conn = conn
|
|
|
|
c.ldb = app.ldb
|
|
|
|
c.db, _ = app.ldb.Select(0)
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
c.rb = bufio.NewReaderSize(conn, 256)
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
c.req = newRequestContext(app)
|
|
|
|
c.req.resp = newWriterRESP(conn)
|
2014-08-01 05:38:08 +04:00
|
|
|
c.req.remoteAddr = conn.RemoteAddr().String()
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
go c.run()
|
2014-07-31 10:38:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (c *respClient) run() {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
buf = buf[0:n]
|
|
|
|
|
|
|
|
log.Fatal("client run panic %s:%v", buf, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.conn.Close()
|
|
|
|
}()
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
for {
|
|
|
|
reqData, err := c.readRequest()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.handleRequest(reqData)
|
|
|
|
}
|
2014-07-31 10:38:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (c *respClient) readLine() ([]byte, error) {
|
|
|
|
return ReadLine(c.rb)
|
2014-07-31 10:38:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//A client sends to the Redis server a RESP Array consisting of just Bulk Strings.
|
2014-07-31 14:28:19 +04:00
|
|
|
func (c *respClient) readRequest() ([][]byte, error) {
|
|
|
|
l, err := c.readLine()
|
2014-07-31 10:38:20 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(l) == 0 || l[0] != '*' {
|
|
|
|
return nil, errReadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
var nparams int
|
|
|
|
if nparams, err = strconv.Atoi(ledis.String(l[1:])); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if nparams <= 0 {
|
|
|
|
return nil, errReadRequest
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
req := make([][]byte, 0, nparams)
|
2014-07-31 10:38:20 +04:00
|
|
|
var n int
|
|
|
|
for i := 0; i < nparams; i++ {
|
2014-07-31 14:28:19 +04:00
|
|
|
if l, err = c.readLine(); err != nil {
|
2014-07-31 10:38:20 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
|
|
|
return nil, errReadRequest
|
|
|
|
} else if l[0] == '$' {
|
|
|
|
//handle resp string
|
|
|
|
if n, err = strconv.Atoi(ledis.String(l[1:])); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if n == -1 {
|
2014-07-31 14:28:19 +04:00
|
|
|
req = append(req, nil)
|
2014-07-31 10:38:20 +04:00
|
|
|
} else {
|
|
|
|
buf := make([]byte, n)
|
2014-07-31 14:28:19 +04:00
|
|
|
if _, err = io.ReadFull(c.rb, buf); err != nil {
|
2014-07-31 10:38:20 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
if l, err = c.readLine(); err != nil {
|
2014-07-31 10:38:20 +04:00
|
|
|
return nil, err
|
|
|
|
} else if len(l) != 0 {
|
|
|
|
return nil, errors.New("bad bulk string format")
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
req = append(req, buf)
|
2014-07-31 10:38:20 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return nil, errReadRequest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *respClient) handleRequest(reqData [][]byte) {
|
|
|
|
req := c.req
|
|
|
|
|
|
|
|
if len(reqData) == 0 {
|
|
|
|
c.req.cmd = ""
|
|
|
|
c.req.args = reqData[0:0]
|
|
|
|
} else {
|
|
|
|
c.req.cmd = strings.ToLower(ledis.String(reqData[0]))
|
|
|
|
c.req.args = reqData[1:]
|
|
|
|
}
|
2014-08-04 07:06:28 +04:00
|
|
|
if c.req.cmd == "quit" {
|
|
|
|
c.req.resp.writeStatus(OK)
|
|
|
|
c.req.resp.flush()
|
|
|
|
c.conn.Close()
|
2014-08-04 07:37:21 +04:00
|
|
|
return
|
2014-08-04 07:06:28 +04:00
|
|
|
}
|
2014-07-31 14:28:19 +04:00
|
|
|
|
2014-08-01 05:38:08 +04:00
|
|
|
req.db = c.db
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
c.req.perform()
|
2014-08-01 05:38:08 +04:00
|
|
|
|
|
|
|
c.db = req.db // "SELECT"
|
|
|
|
|
|
|
|
return
|
2014-07-31 10:38:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
// response writer
|
2014-07-31 10:38:20 +04:00
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func newWriterRESP(conn net.Conn) *respWriter {
|
|
|
|
w := new(respWriter)
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff = bufio.NewWriterSize(conn, 256)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeError(err error) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.Write(ledis.Slice("-ERR"))
|
|
|
|
if err != nil {
|
|
|
|
w.buff.WriteByte(' ')
|
|
|
|
w.buff.Write(ledis.Slice(err.Error()))
|
|
|
|
}
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeStatus(status string) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('+')
|
|
|
|
w.buff.Write(ledis.Slice(status))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeInteger(n int64) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte(':')
|
|
|
|
w.buff.Write(ledis.StrPutInt64(n))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeBulk(b []byte) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('$')
|
|
|
|
if b == nil {
|
|
|
|
w.buff.Write(NullBulk)
|
|
|
|
} else {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(b))))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
w.buff.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeArray(lst []interface{}) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('*')
|
|
|
|
if lst == nil {
|
|
|
|
w.buff.Write(NullArray)
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
} else {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
switch v := lst[i].(type) {
|
|
|
|
case []interface{}:
|
|
|
|
w.writeArray(v)
|
|
|
|
case []byte:
|
|
|
|
w.writeBulk(v)
|
|
|
|
case nil:
|
|
|
|
w.writeBulk(nil)
|
|
|
|
case int64:
|
|
|
|
w.writeInteger(v)
|
|
|
|
default:
|
|
|
|
panic("invalid array type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeSliceArray(lst [][]byte) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('*')
|
|
|
|
if lst == nil {
|
|
|
|
w.buff.Write(NullArray)
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
} else {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
w.writeBulk(lst[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeFVPairArray(lst []ledis.FVPair) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('*')
|
|
|
|
if lst == nil {
|
|
|
|
w.buff.Write(NullArray)
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
} else {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
w.writeBulk(lst[i].Field)
|
|
|
|
w.writeBulk(lst[i].Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('*')
|
|
|
|
if lst == nil {
|
|
|
|
w.buff.Write(NullArray)
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
} else {
|
|
|
|
if withScores {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
} else {
|
|
|
|
w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(lst); i++ {
|
|
|
|
w.writeBulk(lst[i].Member)
|
|
|
|
|
|
|
|
if withScores {
|
|
|
|
w.writeBulk(ledis.StrPutInt64(lst[i].Score))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) writeBulkFrom(n int64, rb io.Reader) {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.WriteByte('$')
|
|
|
|
w.buff.Write(ledis.Slice(strconv.FormatInt(n, 10)))
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
|
|
|
|
io.Copy(w.buff, rb)
|
|
|
|
w.buff.Write(Delims)
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:28:19 +04:00
|
|
|
func (w *respWriter) flush() {
|
2014-07-31 10:38:20 +04:00
|
|
|
w.buff.Flush()
|
|
|
|
}
|