2015-05-02 16:11:18 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2015-09-03 17:55:31 +03:00
|
|
|
"bufio"
|
2015-05-02 16:11:18 +03:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-09-03 17:55:31 +03:00
|
|
|
const defaultBufSize = 4096
|
|
|
|
|
2015-05-10 15:33:04 +03:00
|
|
|
var (
|
|
|
|
zeroTime = time.Time{}
|
|
|
|
)
|
|
|
|
|
2015-05-02 16:11:18 +03:00
|
|
|
type conn struct {
|
|
|
|
netcn net.Conn
|
|
|
|
rd *bufio.Reader
|
|
|
|
buf []byte
|
|
|
|
|
|
|
|
usedAt time.Time
|
2015-05-10 15:33:04 +03:00
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
2015-05-02 16:11:18 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func newConnDialer(opt *Options) func() (*conn, error) {
|
|
|
|
dialer := opt.getDialer()
|
2015-05-02 16:11:18 +03:00
|
|
|
return func() (*conn, error) {
|
2015-05-23 17:55:08 +03:00
|
|
|
netcn, err := dialer()
|
2015-05-02 16:11:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cn := &conn{
|
|
|
|
netcn: netcn,
|
2015-09-03 17:55:31 +03:00
|
|
|
buf: make([]byte, defaultBufSize),
|
2015-05-02 16:11:18 +03:00
|
|
|
}
|
|
|
|
cn.rd = bufio.NewReader(cn)
|
|
|
|
return cn, cn.init(opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func (cn *conn) init(opt *Options) error {
|
2015-05-02 16:11:18 +03:00
|
|
|
if opt.Password == "" && opt.DB == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:45:37 +03:00
|
|
|
// Use connection to connect to Redis.
|
|
|
|
pool := newSingleConnPoolConn(cn)
|
2015-05-02 16:11:18 +03:00
|
|
|
|
|
|
|
// Client is not closed because we want to reuse underlying connection.
|
|
|
|
client := newClient(opt, pool)
|
|
|
|
|
|
|
|
if opt.Password != "" {
|
|
|
|
if err := client.Auth(opt.Password).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.DB > 0 {
|
|
|
|
if err := client.Select(opt.DB).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) writeCmds(cmds ...Cmder) error {
|
|
|
|
buf := cn.buf[:0]
|
|
|
|
for _, cmd := range cmds {
|
2015-05-28 15:51:19 +03:00
|
|
|
var err error
|
|
|
|
buf, err = appendArgs(buf, cmd.args())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-02 16:11:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := cn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) Read(b []byte) (int, error) {
|
2015-05-10 15:33:04 +03:00
|
|
|
if cn.ReadTimeout != 0 {
|
|
|
|
cn.netcn.SetReadDeadline(time.Now().Add(cn.ReadTimeout))
|
2015-05-02 16:11:18 +03:00
|
|
|
} else {
|
|
|
|
cn.netcn.SetReadDeadline(zeroTime)
|
|
|
|
}
|
|
|
|
return cn.netcn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) Write(b []byte) (int, error) {
|
2015-05-10 15:33:04 +03:00
|
|
|
if cn.WriteTimeout != 0 {
|
|
|
|
cn.netcn.SetWriteDeadline(time.Now().Add(cn.WriteTimeout))
|
2015-05-02 16:11:18 +03:00
|
|
|
} else {
|
|
|
|
cn.netcn.SetWriteDeadline(zeroTime)
|
|
|
|
}
|
|
|
|
return cn.netcn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) RemoteAddr() net.Addr {
|
|
|
|
return cn.netcn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) Close() error {
|
|
|
|
return cn.netcn.Close()
|
|
|
|
}
|
2015-09-03 17:55:31 +03:00
|
|
|
|
|
|
|
func isSameSlice(s1, s2 []byte) bool {
|
|
|
|
return len(s1) > 0 && len(s2) > 0 && &s1[0] == &s2[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) copyBuf(b []byte) []byte {
|
|
|
|
if isSameSlice(b, cn.buf) {
|
|
|
|
new := make([]byte, len(b))
|
|
|
|
copy(new, b)
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|