ledisdb/client/go/ledis/client.go

100 lines
1.3 KiB
Go
Raw Normal View History

2014-06-21 18:12:37 +04:00
package ledis
import (
"container/list"
"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) {
co := c.get()
r, err := co.Do(cmd, args...)
c.put(co)
return r, err
}
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 {
return c.get()
}
func (c *Client) get() *Conn {
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
}
}
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()
}
}