2014-07-31 14:28:19 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type responseWriter interface {
|
|
|
|
writeError(error)
|
|
|
|
writeStatus(string)
|
|
|
|
writeInteger(int64)
|
|
|
|
writeBulk([]byte)
|
|
|
|
writeArray([]interface{})
|
|
|
|
writeSliceArray([][]byte)
|
|
|
|
writeFVPairArray([]ledis.FVPair)
|
|
|
|
writeScorePairArray([]ledis.ScorePair, bool)
|
|
|
|
writeBulkFrom(int64, io.Reader)
|
|
|
|
flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestContext struct {
|
|
|
|
app *App
|
|
|
|
ldb *ledis.Ledis
|
|
|
|
db *ledis.DB
|
|
|
|
|
|
|
|
remoteAddr string
|
|
|
|
cmd string
|
|
|
|
args [][]byte
|
|
|
|
|
|
|
|
resp responseWriter
|
|
|
|
|
|
|
|
syncBuf bytes.Buffer
|
|
|
|
compressBuf []byte
|
|
|
|
|
|
|
|
reqErr chan error
|
|
|
|
|
|
|
|
buf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequestContext(app *App) *requestContext {
|
|
|
|
req := new(requestContext)
|
|
|
|
|
|
|
|
req.app = app
|
|
|
|
req.ldb = app.ldb
|
|
|
|
req.db, _ = app.ldb.Select(0) //use default db
|
|
|
|
|
|
|
|
req.compressBuf = make([]byte, 256)
|
2014-08-01 07:42:16 +04:00
|
|
|
req.reqErr = make(chan error)
|
2014-07-31 14:28:19 +04:00
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
func (req *requestContext) perform() {
|
2014-07-31 14:28:19 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
if len(req.cmd) == 0 {
|
|
|
|
err = ErrEmptyCommand
|
|
|
|
} else if exeCmd, ok := regCmds[req.cmd]; !ok {
|
|
|
|
err = ErrNotFound
|
|
|
|
} else {
|
|
|
|
go func() {
|
2014-08-01 07:42:16 +04:00
|
|
|
req.reqErr <- exeCmd(req)
|
2014-07-31 14:28:19 +04:00
|
|
|
}()
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
err = <-req.reqErr
|
2014-07-31 14:28:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Since(start)
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
if req.app.access != nil {
|
|
|
|
fullCmd := req.catGenericCommand()
|
2014-07-31 14:28:19 +04:00
|
|
|
cost := duration.Nanoseconds() / 1000000
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
truncateLen := len(fullCmd)
|
|
|
|
if truncateLen > 256 {
|
|
|
|
truncateLen = 256
|
|
|
|
}
|
|
|
|
|
|
|
|
req.app.access.Log(req.remoteAddr, cost, fullCmd[:truncateLen], err)
|
2014-07-31 14:28:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
req.resp.writeError(err)
|
|
|
|
}
|
|
|
|
req.resp.flush()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (h *requestHandler) catFullCommand(req *requestContext) []byte {
|
|
|
|
//
|
|
|
|
// // if strings.HasSuffix(cmd, "expire") {
|
|
|
|
// // catExpireCommand(c, buffer)
|
|
|
|
// // } else {
|
|
|
|
// // catGenericCommand(c, buffer)
|
|
|
|
// // }
|
|
|
|
//
|
|
|
|
// return h.catGenericCommand(req)
|
|
|
|
// }
|
|
|
|
|
2014-08-01 07:42:16 +04:00
|
|
|
func (req *requestContext) catGenericCommand() []byte {
|
|
|
|
buffer := req.buf
|
2014-07-31 14:28:19 +04:00
|
|
|
buffer.Reset()
|
|
|
|
|
|
|
|
buffer.Write([]byte(req.cmd))
|
|
|
|
|
|
|
|
nargs := len(req.args)
|
|
|
|
for i, arg := range req.args {
|
|
|
|
buffer.Write(arg)
|
|
|
|
if i != nargs-1 {
|
|
|
|
buffer.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.Bytes()
|
|
|
|
}
|