redis/redis.go

177 lines
3.0 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis
import (
2012-07-26 19:16:17 +04:00
"fmt"
2012-07-25 17:00:50 +04:00
"io"
"sync"
"github.com/togoio/redisgoproxy/bufreader"
)
type connectFunc func() (io.ReadWriter, error)
type disconnectFunc func(io.ReadWriter)
type Client struct {
mtx sync.Mutex
connect connectFunc
disconnect disconnectFunc
currConn io.ReadWriter
rd *bufreader.Reader
2012-07-26 19:16:17 +04:00
reqs []Req
2012-07-25 17:00:50 +04:00
}
func NewClient(connect connectFunc, disconnect disconnectFunc) *Client {
return &Client{
rd: bufreader.NewSizedReader(8192),
connect: connect,
disconnect: disconnect,
}
}
2012-07-26 19:16:17 +04:00
func NewMultiClient(connect connectFunc, disconnect disconnectFunc) *Client {
return &Client{
rd: bufreader.NewSizedReader(8192),
connect: connect,
disconnect: disconnect,
reqs: make([]Req, 0),
}
}
2012-07-25 17:00:50 +04:00
func (c *Client) Close() error {
if c.disconnect != nil {
c.disconnect(c.currConn)
}
c.currConn = nil
return nil
}
func (c *Client) conn() (io.ReadWriter, error) {
if c.currConn == nil {
currConn, err := c.connect()
if err != nil {
return nil, err
}
c.currConn = currConn
}
return c.currConn, nil
}
func (c *Client) WriteReq(buf []byte) error {
conn, err := c.conn()
if err != nil {
return err
}
_, err = conn.Write(buf)
if err != nil {
c.Close()
}
return err
}
func (c *Client) ReadReply() (*bufreader.Reader, error) {
conn, err := c.conn()
if err != nil {
return nil, err
}
_, err = c.rd.ReadFrom(conn)
if err != nil {
c.Close()
return nil, err
}
return c.rd, nil
}
func (c *Client) WriteRead(buf []byte) (*bufreader.Reader, error) {
2012-07-26 19:16:17 +04:00
c.mtx.Lock()
defer c.mtx.Unlock()
2012-07-25 17:00:50 +04:00
if err := c.WriteReq(buf); err != nil {
return nil, err
}
return c.ReadReply()
}
func (c *Client) Run(req Req) {
2012-07-26 19:16:17 +04:00
if c.reqs != nil {
c.mtx.Lock()
c.reqs = append(c.reqs, req)
c.mtx.Unlock()
return
}
rd, err := c.WriteRead(req.Req())
if err != nil {
req.SetErr(err)
return
}
req.ParseReply(rd)
}
//------------------------------------------------------------------------------
func (c *Client) Discard() {
if c.reqs == nil {
panic("MultiClient required")
}
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) {
if c.reqs == nil {
panic("MultiClient required")
}
c.mtx.Lock()
reqs := c.reqs
c.reqs = make([]Req, 0)
c.mtx.Unlock()
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"})...)
rd, err := c.WriteRead(multiReq)
2012-07-25 17:00:50 +04:00
if err != nil {
2012-07-26 19:16:17 +04:00
return nil, err
2012-07-25 17:00:50 +04:00
}
2012-07-26 19:16:17 +04:00
statusReq := NewStatusReq()
// multi
statusReq.ParseReply(rd)
_, err = statusReq.Reply()
if err != nil {
return nil, err
}
for _ = range reqs {
// queue
statusReq.ParseReply(rd)
_, err = statusReq.Reply()
if err != nil {
return nil, err
}
}
line, err := rd.ReadLine('\n')
if err != nil {
return nil, err
}
if line[0] != '*' {
return nil, fmt.Errorf("Expected '*', but got line %q of %q.", line, rd.Bytes())
}
for _, req := range reqs {
req.ParseReply(rd)
}
return reqs, nil
2012-07-25 17:00:50 +04:00
}