redis/redis.go

301 lines
5.3 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis
import (
2012-08-05 16:09:43 +04:00
"crypto/tls"
2012-07-26 19:16:17 +04:00
"fmt"
2012-07-25 17:00:50 +04:00
"io"
2012-08-05 16:09:43 +04:00
"net"
2012-07-25 17:00:50 +04:00
"sync"
"time"
2012-07-25 17:00:50 +04:00
2012-07-27 15:43:30 +04:00
"github.com/vmihailenco/bufreader"
2012-07-25 17:00:50 +04:00
)
2012-08-05 16:09:43 +04:00
type OpenConnFunc func() (io.ReadWriter, error)
type CloseConnFunc func(io.ReadWriter)
type InitConnFunc func(*Client) error
2012-07-25 17:00:50 +04:00
2012-08-05 16:09:43 +04:00
func TCPConnector(addr string) OpenConnFunc {
return func() (io.ReadWriter, error) {
return net.Dial("tcp", addr)
}
2012-07-26 22:43:21 +04:00
}
2012-08-05 16:09:43 +04:00
func TLSConnector(addr string, tlsConfig *tls.Config) OpenConnFunc {
return func() (io.ReadWriter, error) {
return tls.Dial("tcp", addr, tlsConfig)
}
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
func AuthSelectFunc(password string, db int64) InitConnFunc {
return func(client *Client) error {
if password != "" {
_, err := client.Auth(password).Reply()
if err != nil {
return err
}
}
2012-07-29 13:42:00 +04:00
2012-08-05 16:09:43 +04:00
_, err := client.Select(db).Reply()
if err != nil {
return err
}
return nil
2012-07-25 17:00:50 +04:00
}
}
2012-08-05 16:09:43 +04:00
func createReader() (*bufreader.Reader, error) {
return bufreader.NewSizedReader(8192), nil
}
2012-07-26 19:16:17 +04:00
2012-08-05 16:09:43 +04:00
type Client struct {
mtx sync.Mutex
ConnPool *ConnPool
InitConn InitConnFunc
reqs []Req
2012-07-26 19:16:17 +04:00
}
2012-08-05 16:09:43 +04:00
func NewClient(openConn OpenConnFunc, closeConn CloseConnFunc, initConn InitConnFunc) *Client {
return &Client{
ConnPool: NewConnPool(openConn, closeConn, 10),
InitConn: initConn,
2012-07-25 17:00:50 +04:00
}
}
2012-08-05 16:09:43 +04:00
func NewTCPClient(addr string, password string, db int64) *Client {
return NewClient(TCPConnector(addr), nil, AuthSelectFunc(password, db))
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
func NewTLSClient(addr string, tlsConfig *tls.Config, password string, db int64) *Client {
return NewClient(
TLSConnector(addr, tlsConfig),
nil,
AuthSelectFunc(password, db),
)
}
2012-07-26 22:43:21 +04:00
2012-08-05 16:09:43 +04:00
func (c *Client) WriteReq(buf []byte, conn *Conn) error {
_, err := conn.RW.Write(buf)
2012-07-25 17:00:50 +04:00
return err
}
2012-08-05 16:09:43 +04:00
func (c *Client) ReadReply(conn *Conn) error {
if false {
err := conn.RW.(*net.TCPConn).SetReadDeadline(time.Now().Add(time.Second))
if err != nil {
return err
}
}
2012-08-05 16:09:43 +04:00
_, err := conn.Rd.ReadFrom(conn.RW)
2012-07-25 17:00:50 +04:00
if err != nil {
2012-07-26 22:43:21 +04:00
return err
2012-07-25 17:00:50 +04:00
}
2012-07-26 22:43:21 +04:00
return nil
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
func (c *Client) WriteRead(buf []byte, conn *Conn) error {
2012-07-26 19:16:17 +04:00
c.mtx.Lock()
defer c.mtx.Unlock()
2012-08-05 16:09:43 +04:00
if err := c.WriteReq(buf, conn); err != nil {
2012-07-26 22:43:21 +04:00
return err
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
return c.ReadReply(conn)
}
func (c *Client) Process(req Req) {
if c.reqs == nil {
c.Run(req)
} else {
c.Queue(req)
}
2012-07-25 17:00:50 +04:00
}
2012-07-29 13:42:00 +04:00
func (c *Client) Queue(req Req) {
c.mtx.Lock()
c.reqs = append(c.reqs, req)
c.mtx.Unlock()
}
2012-07-26 19:16:17 +04:00
2012-07-29 13:42:00 +04:00
func (c *Client) Run(req Req) {
2012-08-05 16:09:43 +04:00
conn, _, err := c.ConnPool.Get()
2012-07-26 22:43:21 +04:00
if err != nil {
req.SetErr(err)
return
}
2012-08-05 16:09:43 +04:00
err = c.WriteRead(req.Req(), conn)
2012-07-26 22:43:21 +04:00
if err != nil {
2012-08-05 16:09:43 +04:00
c.ConnPool.Remove(conn)
2012-07-26 22:43:21 +04:00
req.SetErr(err)
return
}
2012-08-05 16:09:43 +04:00
val, err := req.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
c.ConnPool.Add(conn)
2012-07-26 19:16:17 +04:00
req.SetErr(err)
return
}
2012-08-05 16:09:43 +04:00
c.ConnPool.Add(conn)
2012-07-26 22:43:21 +04:00
req.SetVal(val)
2012-07-26 19:16:17 +04:00
}
2012-07-29 13:42:00 +04:00
func (c *Client) RunQueued() ([]Req, error) {
2012-08-05 16:09:43 +04:00
c.mtx.Lock()
2012-07-29 13:51:29 +04:00
if len(c.reqs) == 0 {
2012-08-05 16:09:43 +04:00
c.mtx.Unlock()
2012-07-29 13:51:29 +04:00
return c.reqs, nil
}
2012-07-29 13:42:00 +04:00
reqs := c.reqs
c.reqs = make([]Req, 0)
c.mtx.Unlock()
2012-07-26 19:16:17 +04:00
conn, _, err := c.ConnPool.Get()
if err != nil {
return nil, err
}
err = c.RunReqs(reqs, conn)
if err != nil {
c.ConnPool.Remove(conn)
return nil, err
}
// c.ConnPool.Add(conn)
return reqs, nil
2012-08-05 16:09:43 +04:00
}
func (c *Client) RunReqs(reqs []Req, conn *Conn) error {
2012-07-29 13:42:00 +04:00
var multiReq []byte
if len(reqs) == 1 {
multiReq = reqs[0].Req()
} else {
// TODO: split req to chunks
2012-07-29 13:42:00 +04:00
multiReq = make([]byte, 0, 1024)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
2012-07-26 19:16:17 +04:00
}
err := c.WriteRead(multiReq, conn)
2012-07-29 13:42:00 +04:00
if err != nil {
return err
2012-07-29 13:42:00 +04:00
}
2012-08-05 16:09:43 +04:00
for i := 0; i < len(reqs); i++ {
if !conn.Rd.HasUnread() {
_, err := conn.Rd.ReadFrom(conn.RW)
if err != err {
return err
}
}
2012-08-05 16:09:43 +04:00
req := reqs[i]
2012-08-05 16:09:43 +04:00
val, err := req.ParseReply(conn.Rd)
2012-07-29 13:42:00 +04:00
if err != nil {
req.SetErr(err)
} else {
req.SetVal(val)
}
}
return nil
2012-07-29 13:42:00 +04:00
}
//------------------------------------------------------------------------------
func (c *Client) Discard() {
2012-07-25 17:00:50 +04:00
c.mtx.Lock()
2012-07-26 19:16:17 +04:00
c.reqs = c.reqs[:0]
2012-07-25 17:00:50 +04:00
c.mtx.Unlock()
}
2012-07-26 19:16:17 +04:00
func (c *Client) Exec() ([]Req, error) {
2012-08-05 16:09:43 +04:00
c.mtx.Lock()
2012-07-29 13:51:29 +04:00
if len(c.reqs) == 0 {
2012-08-05 16:09:43 +04:00
c.mtx.Unlock()
2012-07-29 13:51:29 +04:00
return c.reqs, nil
}
2012-07-26 19:16:17 +04:00
reqs := c.reqs
c.reqs = make([]Req, 0)
c.mtx.Unlock()
conn, _, err := c.ConnPool.Get()
if err != nil {
return nil, err
}
err = c.ExecReqs(reqs, conn)
if err != nil {
c.ConnPool.Remove(conn)
return nil, err
}
c.ConnPool.Add(conn)
return reqs, nil
}
func (c *Client) ExecReqs(reqs []Req, conn *Conn) error {
// TODO: split req to chunks
2012-07-26 19:16:17 +04:00
multiReq := make([]byte, 0, 1024)
multiReq = append(multiReq, PackReq([]string{"MULTI"})...)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
multiReq = append(multiReq, PackReq([]string{"EXEC"})...)
err := c.WriteRead(multiReq, conn)
2012-07-25 17:00:50 +04:00
if err != nil {
return err
2012-07-25 17:00:50 +04:00
}
2012-07-26 19:16:17 +04:00
statusReq := NewStatusReq()
2012-07-29 13:42:00 +04:00
// Parse MULTI command reply.
2012-08-05 16:09:43 +04:00
_, err = statusReq.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
2012-07-29 13:42:00 +04:00
// Parse queued replies.
2012-07-26 19:16:17 +04:00
for _ = range reqs {
if !conn.Rd.HasUnread() {
_, err := conn.Rd.ReadFrom(conn.RW)
if err != err {
return err
}
}
2012-08-05 16:09:43 +04:00
_, err = statusReq.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
}
2012-07-29 13:42:00 +04:00
// Parse number of replies.
2012-08-05 16:09:43 +04:00
line, err := conn.Rd.ReadLine('\n')
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
if line[0] != '*' {
return fmt.Errorf("Expected '*', but got line %q of %q.", line, conn.Rd.Bytes())
2012-07-26 19:16:17 +04:00
}
2012-07-29 13:42:00 +04:00
// Parse replies.
2012-08-05 16:09:43 +04:00
for i := 0; i < len(reqs); i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
2012-07-26 22:43:21 +04:00
if err != nil {
req.SetErr(err)
2012-07-29 13:42:00 +04:00
} else {
req.SetVal(val)
2012-07-26 22:43:21 +04:00
}
2012-07-26 19:16:17 +04:00
}
return nil
2012-07-25 17:00:50 +04:00
}