ledisdb/client/go/ledis/client.go

124 lines
1.7 KiB
Go
Raw Normal View History

2014-06-21 18:12:37 +04:00
package ledis
import (
"container/list"
"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
cfg *Config
2014-06-21 18:12:37 +04:00
conns *list.List
}
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) {
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
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()
}
}
func (c *Client) Get() (*Conn, error) {
2014-06-21 18:12:37 +04:00
return c.get()
}
func (c *Client) get() (*Conn, error) {
2014-06-21 18:12:37 +04:00
c.Lock()
if c.conns.Len() == 0 {
c.Unlock()
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()
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()
}
}