2015-03-11 06:54:02 +03:00
|
|
|
package goredis
|
2014-06-21 18:12:37 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-05-03 06:19:11 +03:00
|
|
|
"fmt"
|
2014-06-21 18:12:37 +04:00
|
|
|
"io"
|
|
|
|
"net"
|
2015-03-16 06:56:04 +03:00
|
|
|
"sync/atomic"
|
2014-12-01 12:50:48 +03:00
|
|
|
"time"
|
2014-06-21 18:12:37 +04:00
|
|
|
)
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
type sizeWriter int64
|
|
|
|
|
|
|
|
func (s *sizeWriter) Write(p []byte) (int, error) {
|
|
|
|
*s += sizeWriter(len(p))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
type Conn struct {
|
2015-05-03 06:19:11 +03:00
|
|
|
c net.Conn
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-03-23 14:50:37 +03:00
|
|
|
respReader *RespReader
|
|
|
|
respWriter *RespWriter
|
2015-03-11 06:54:02 +03:00
|
|
|
|
|
|
|
totalReadSize sizeWriter
|
|
|
|
totalWriteSize sizeWriter
|
2015-03-16 06:35:59 +03:00
|
|
|
|
2015-03-16 06:56:04 +03:00
|
|
|
closed int32
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
func Connect(addr string) (*Conn, error) {
|
2015-03-11 06:54:02 +03:00
|
|
|
return ConnectWithSize(addr, 1024, 1024)
|
2015-03-02 12:12:55 +03:00
|
|
|
}
|
2014-10-23 09:34:03 +04:00
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
func ConnectWithSize(addr string, readSize int, writeSize int) (*Conn, error) {
|
2015-05-03 06:19:11 +03:00
|
|
|
conn, err := net.Dial(getProto(addr), addr)
|
2015-03-02 12:12:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-01 18:28:28 +03:00
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
return NewConnWithSize(conn, readSize, writeSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConn(conn net.Conn) (*Conn, error) {
|
|
|
|
return NewConnWithSize(conn, 1024, 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConnWithSize(conn net.Conn, readSize int, writeSize int) (*Conn, error) {
|
|
|
|
c := new(Conn)
|
|
|
|
|
|
|
|
c.c = conn
|
2014-10-23 09:49:25 +04:00
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
br := bufio.NewReaderSize(io.TeeReader(c.c, &c.totalReadSize), readSize)
|
|
|
|
bw := bufio.NewWriterSize(io.MultiWriter(c.c, &c.totalWriteSize), writeSize)
|
|
|
|
|
|
|
|
c.respReader = NewRespReader(br)
|
|
|
|
c.respWriter = NewRespWriter(bw)
|
2015-03-23 14:50:37 +03:00
|
|
|
|
2015-03-16 06:56:04 +03:00
|
|
|
atomic.StoreInt32(&c.closed, 0)
|
2015-03-16 06:35:59 +03:00
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
return c, nil
|
2015-05-03 06:19:11 +03:00
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
func (c *Conn) Close() {
|
2015-03-16 06:56:04 +03:00
|
|
|
if atomic.LoadInt32(&c.closed) == 1 {
|
2015-03-16 06:35:59 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
c.c.Close()
|
2015-03-16 06:56:04 +03:00
|
|
|
|
|
|
|
atomic.StoreInt32(&c.closed, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) isClosed() bool {
|
|
|
|
return atomic.LoadInt32(&c.closed) == 1
|
2015-03-11 06:54:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) GetTotalReadSize() int64 {
|
|
|
|
return int64(c.totalReadSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) GetTotalWriteSize() int64 {
|
|
|
|
return int64(c.totalWriteSize)
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.c.SetReadDeadline(t)
|
2014-12-03 11:27:52 +03:00
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return c.c.SetWriteDeadline(t)
|
2014-12-03 11:27:52 +03:00
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
|
|
|
return c.c.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
|
|
|
return c.c.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send RESP command and receive the reply
|
2014-06-21 18:12:37 +04:00
|
|
|
func (c *Conn) Do(cmd string, args ...interface{}) (interface{}, error) {
|
2014-10-23 09:34:03 +04:00
|
|
|
if err := c.Send(cmd, args...); err != nil {
|
2014-06-21 18:12:37 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
return c.Receive()
|
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
// Send RESP command
|
2014-10-23 09:34:03 +04:00
|
|
|
func (c *Conn) Send(cmd string, args ...interface{}) error {
|
2015-03-23 14:50:37 +03:00
|
|
|
if err := c.respWriter.WriteCommand(cmd, args...); err != nil {
|
2015-03-11 06:54:02 +03:00
|
|
|
c.Close()
|
2014-10-23 09:34:03 +04:00
|
|
|
return err
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
// Receive RESP reply
|
2014-10-23 09:34:03 +04:00
|
|
|
func (c *Conn) Receive() (interface{}, error) {
|
2015-03-23 14:50:37 +03:00
|
|
|
if reply, err := c.respReader.Parse(); err != nil {
|
2015-03-11 06:54:02 +03:00
|
|
|
c.Close()
|
2014-06-21 18:12:37 +04:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
if e, ok := reply.(Error); ok {
|
|
|
|
return reply, e
|
|
|
|
} else {
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
// Receive RESP bulk string reply into writer w
|
2014-10-23 09:34:03 +04:00
|
|
|
func (c *Conn) ReceiveBulkTo(w io.Writer) error {
|
2015-03-23 14:50:37 +03:00
|
|
|
err := c.respReader.ParseBulkTo(w)
|
2014-10-23 09:34:03 +04:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(Error); !ok {
|
2015-03-11 06:54:02 +03:00
|
|
|
c.Close()
|
2014-10-23 09:34:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-03 06:19:11 +03:00
|
|
|
// Receive RESP command request, must array of bulk stirng
|
|
|
|
func (c *Conn) ReceiveRequest() ([][]byte, error) {
|
|
|
|
return c.respReader.ParseRequest()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send RESP value, must be string, int64, []byte, error, nil or []interface{}
|
|
|
|
func (c *Conn) SendValue(v interface{}) error {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
return c.respWriter.FlushString(v)
|
|
|
|
case int64:
|
|
|
|
return c.respWriter.FlushInteger(v)
|
|
|
|
case []byte:
|
|
|
|
return c.respWriter.FlushBulk(v)
|
|
|
|
case []interface{}:
|
|
|
|
return c.respWriter.FlushArray(v)
|
|
|
|
case error:
|
|
|
|
return c.respWriter.FlushError(v)
|
|
|
|
case nil:
|
|
|
|
return c.respWriter.FlushBulk(nil)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid type %T for send RESP value", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:54:02 +03:00
|
|
|
func (c *Client) newConn(addr string, pass string) (*Conn, error) {
|
|
|
|
co, err := ConnectWithSize(addr, c.readBufferSize, c.writeBufferSize)
|
2015-03-02 12:12:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-11 06:54:02 +03:00
|
|
|
|
|
|
|
if len(pass) > 0 {
|
|
|
|
_, err = co.Do("AUTH", pass)
|
|
|
|
if err != nil {
|
|
|
|
co.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
return co, nil
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|