2014-06-21 18:12:37 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2015-03-02 12:12:55 +03:00
|
|
|
"net"
|
2014-06-21 18:12:37 +04:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2014-10-25 11:27:05 +04:00
|
|
|
Addr string
|
|
|
|
MaxIdleConns int
|
|
|
|
ReadBufferSize int
|
|
|
|
WriteBufferSize int
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
cfg *Config
|
2014-06-21 18:12:37 +04:00
|
|
|
|
|
|
|
conns *list.List
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
func getProto(addr string) string {
|
|
|
|
if strings.Contains(addr, "/") {
|
|
|
|
return "unix"
|
|
|
|
} else {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 18:12:37 +04:00
|
|
|
func NewClient(cfg *Config) *Client {
|
|
|
|
c := new(Client)
|
|
|
|
|
|
|
|
c.cfg = cfg
|
2014-10-25 11:27:05 +04:00
|
|
|
if c.cfg.ReadBufferSize == 0 {
|
|
|
|
c.cfg.ReadBufferSize = 4096
|
|
|
|
}
|
|
|
|
if c.cfg.WriteBufferSize == 0 {
|
|
|
|
c.cfg.WriteBufferSize = 4096
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
|
|
|
c.conns = list.New()
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error) {
|
2015-03-02 12:12:55 +03:00
|
|
|
var co *Conn
|
|
|
|
var err error
|
|
|
|
var r interface{}
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
co, err = c.get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err = co.Do(cmd, args...)
|
|
|
|
if err != nil {
|
|
|
|
co.finalize()
|
|
|
|
|
|
|
|
if e, ok := err.(*net.OpError); ok && strings.Contains(e.Error(), "use of closed network connection") {
|
|
|
|
//send to a closed connection, try again
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
c.put(co)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
2014-06-21 18:12:37 +04:00
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
return nil, err
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
for c.conns.Len() > 0 {
|
|
|
|
e := c.conns.Front()
|
|
|
|
co := e.Value.(*Conn)
|
|
|
|
c.conns.Remove(e)
|
|
|
|
|
|
|
|
co.finalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
func (c *Client) Get() (*Conn, error) {
|
2014-06-21 18:12:37 +04:00
|
|
|
return c.get()
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
func (c *Client) get() (*Conn, error) {
|
2014-06-21 18:12:37 +04:00
|
|
|
c.Lock()
|
|
|
|
if c.conns.Len() == 0 {
|
|
|
|
c.Unlock()
|
|
|
|
|
2014-10-23 09:34:03 +04:00
|
|
|
return c.newConn(c.cfg.Addr)
|
2014-06-21 18:12:37 +04:00
|
|
|
} else {
|
|
|
|
e := c.conns.Front()
|
|
|
|
co := e.Value.(*Conn)
|
|
|
|
c.conns.Remove(e)
|
|
|
|
|
|
|
|
c.Unlock()
|
|
|
|
|
2015-03-02 12:12:55 +03:00
|
|
|
return co, nil
|
2014-06-21 18:12:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) put(conn *Conn) {
|
|
|
|
c.Lock()
|
|
|
|
if c.conns.Len() >= c.cfg.MaxIdleConns {
|
|
|
|
c.Unlock()
|
|
|
|
conn.finalize()
|
|
|
|
} else {
|
|
|
|
c.conns.PushFront(conn)
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
}
|