Impove v2 API.

This commit is contained in:
Vladimir Mihailenco 2013-09-11 18:06:36 +03:00
parent 28a881e724
commit ce34e39219
2 changed files with 50 additions and 69 deletions

View File

@ -35,19 +35,19 @@ func newConn(netcn net.Conn, readTimeout, writeTimeout time.Duration) *conn {
readTimeout: readTimeout, readTimeout: readTimeout,
writeTimeout: writeTimeout, writeTimeout: writeTimeout,
} }
cn.Rd = bufio.NewReaderSize(netcn, 1024) cn.Rd = bufio.NewReaderSize(cn, 1024)
return cn return cn
} }
func (cn *conn) Read(b []byte) (int, error) { func (cn *conn) Read(b []byte) (int, error) {
if cn.readTimeout > 0 { if cn.readTimeout != 0 {
cn.Cn.SetReadDeadline(time.Now().Add(cn.readTimeout)) cn.Cn.SetReadDeadline(time.Now().Add(cn.readTimeout))
} }
return cn.Cn.Read(b) return cn.Cn.Read(b)
} }
func (cn *conn) Write(b []byte) (int, error) { func (cn *conn) Write(b []byte) (int, error) {
if cn.writeTimeout > 0 { if cn.writeTimeout != 0 {
cn.Cn.SetWriteDeadline(time.Now().Add(cn.writeTimeout)) cn.Cn.SetWriteDeadline(time.Now().Add(cn.writeTimeout))
} }
return cn.Cn.Write(b) return cn.Cn.Write(b)
@ -56,8 +56,7 @@ func (cn *conn) Write(b []byte) (int, error) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type connPool struct { type connPool struct {
dial func() (net.Conn, error) dial func() (net.Conn, error)
close func(net.Conn) error
cond *sync.Cond cond *sync.Cond
conns *list.List conns *list.List
@ -70,13 +69,11 @@ type connPool struct {
func newConnPool( func newConnPool(
dial func() (net.Conn, error), dial func() (net.Conn, error),
close func(net.Conn) error,
maxSize int, maxSize int,
readTimeout, writeTimeout, idleTimeout time.Duration, readTimeout, writeTimeout, idleTimeout time.Duration,
) *connPool { ) *connPool {
return &connPool{ return &connPool{
dial: dial, dial: dial,
close: close,
cond: sync.NewCond(&sync.Mutex{}), cond: sync.NewCond(&sync.Mutex{}),
conns: list.New(), conns: list.New(),
@ -143,10 +140,14 @@ func (p *connPool) Remove(cn *conn) error {
} }
func (p *connPool) Len() int { func (p *connPool) Len() int {
defer p.cond.L.Unlock()
p.cond.L.Lock()
return p.conns.Len() return p.conns.Len()
} }
func (p *connPool) Size() int { func (p *connPool) Size() int {
defer p.cond.L.Unlock()
p.cond.L.Lock()
return p.size return p.size
} }
@ -166,11 +167,7 @@ func (p *connPool) Close() error {
} }
func (p *connPool) closeConn(cn *conn) error { func (p *connPool) closeConn(cn *conn) error {
if p.close != nil { return cn.Cn.Close()
return p.close(cn.Cn)
} else {
return cn.Cn.Close()
}
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -137,46 +137,31 @@ func (c *baseClient) Close() error {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
type ClientFactory struct { type Options struct {
Dial func() (net.Conn, error) Addr string
Close func(net.Conn) error
Password string Password string
DB int64 DB int64
PoolSize int PoolSize int
ReadTimeout, WriteTimeout, IdleTimeout time.Duration DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
} }
func (f *ClientFactory) New() *Client { func (opt *Options) getPoolSize() int {
return &Client{ if opt.PoolSize == 0 {
baseClient: &baseClient{
password: f.Password,
db: f.DB,
connPool: newConnPool(
f.Dial, f.getClose(), f.getPoolSize(),
f.ReadTimeout, f.WriteTimeout, f.IdleTimeout,
),
},
}
}
func (f *ClientFactory) getClose() func(net.Conn) error {
if f.Close == nil {
return func(conn net.Conn) error {
return conn.Close()
}
}
return f.Close
}
func (f *ClientFactory) getPoolSize() int {
if f.PoolSize == 0 {
return 10 return 10
} }
return f.PoolSize return opt.PoolSize
}
func (opt *Options) getDialTimeout() time.Duration {
if opt.DialTimeout == 0 {
return 5 * time.Second
}
return opt.DialTimeout
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -185,42 +170,41 @@ type Client struct {
*baseClient *baseClient
} }
func NewTCPClient(addr string, password string, db int64) *Client { func newClient(opt *Options, dial func() (net.Conn, error)) *Client {
dial := func() (net.Conn, error) { return &Client{
return net.DialTimeout("tcp", addr, 3*time.Second) baseClient: &baseClient{
} password: opt.Password,
return (&ClientFactory{ db: opt.DB,
Dial: dial,
Password: password, connPool: newConnPool(
DB: db, dial, opt.getPoolSize(),
}).New() opt.ReadTimeout, opt.WriteTimeout, opt.IdleTimeout,
),
},
}
} }
func NewTLSClient(addr string, tlsConfig *tls.Config, password string, db int64) *Client { func DialTCP(opt *Options) *Client {
dial := func() (net.Conn, error) { dial := func() (net.Conn, error) {
conn, err := net.DialTimeout("tcp", addr, 3*time.Second) return net.DialTimeout("tcp", opt.Addr, opt.getDialTimeout())
}
return newClient(opt, dial)
}
func DialTLS(opt *Options, tlsConfig *tls.Config) *Client {
dial := func() (net.Conn, error) {
conn, err := net.DialTimeout("tcp", opt.Addr, opt.getDialTimeout())
if err != nil { if err != nil {
return nil, err return nil, err
} }
return tls.Client(conn, tlsConfig), nil return tls.Client(conn, tlsConfig), nil
} }
return (&ClientFactory{ return newClient(opt, dial)
Dial: dial,
Password: password,
DB: db,
}).New()
} }
func NewUnixClient(addr string, password string, db int64) *Client { func DialUnix(opt *Options) *Client {
dial := func() (net.Conn, error) { dial := func() (net.Conn, error) {
return net.DialTimeout("unix", addr, 3*time.Second) return net.DialTimeout("unix", opt.Addr, opt.getDialTimeout())
} }
return (&ClientFactory{ return newClient(opt, dial)
Dial: dial,
Password: password,
DB: db,
}).New()
} }