Removing Auto() sugar since it's not safe

This commit is contained in:
Pavel Shevaev 2022-10-31 16:05:04 +03:00
parent a9f5855503
commit 7756ae11cf
2 changed files with 22 additions and 47 deletions

52
rdb.go
View File

@ -59,11 +59,14 @@ func (p *Pool) Get() redis.Conn {
return conn
}
func (p *Pool) Auto() redis.Conn {
return &autoConn{p, nil}
//NOTE: convenience method for 'one-shot' commands
func (p *Pool) Do(cmd string, args ...interface{}) (interface{}, error) {
rc := p.Get()
defer rc.Close()
return rc.Do(cmd, args...)
}
// NOTE: redis connection logs all operations
//NOTE: redis connection logs all operations
type rdb struct {
orig redis.Conn
name string
@ -411,46 +414,3 @@ func (t *tracked) Send(cmd string, args ...interface{}) error { return t.subj.Se
func (t *tracked) Err() error { return t.subj.Err() }
func (t *tracked) Flush() error { return t.subj.Flush() }
func (t *tracked) Receive() (interface{}, error) { return t.subj.Receive() }
type autoConn struct {
p *Pool
pipe redis.Conn
}
func (c *autoConn) Close() error {
return nil
}
func (c *autoConn) Do(cmd string, args ...interface{}) (interface{}, error) {
if c.pipe != nil {
return nil, errors.New("There is an active pipeline")
}
rc := c.p.Get()
defer rc.Close()
return rc.Do(cmd, args...)
}
func (c *autoConn) Send(cmd string, args ...interface{}) error {
if c.pipe == nil {
c.pipe = c.p.Get()
}
return c.pipe.Send(cmd, args...)
}
func (c *autoConn) Err() error {
if c.pipe != nil {
return c.pipe.Err()
}
return nil
}
func (c *autoConn) Flush() error {
if c.pipe != nil {
return c.pipe.Flush()
}
return errors.New("There is no active pipeline")
}
func (c *autoConn) Receive() (interface{}, error) {
if c.pipe != nil {
return c.pipe.Receive()
}
return nil, errors.New("There is no active pipeline")
}

View File

@ -36,12 +36,14 @@ func TestGetConn(t *testing.T) {
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("PING")
assert.Nil(t, err)
}
func TestCloseConn(t *testing.T) {
pool := getPool()
assert.EqualValues(t, 0, pool.RP.IdleCount())
defer pool.Close()
conn := pool.Get()
@ -51,9 +53,10 @@ func TestCloseConn(t *testing.T) {
assert.Nil(t, err)
conn.Close()
assert.EqualValues(t, 1, pool.RP.IdleCount())
_, err = conn.Do("PING")
assert.NotNil(t, err)
assert.EqualValues(t, 1, pool.RP.IdleCount())
}
func TestDoubleCloseConnIsOk(t *testing.T) {
@ -61,6 +64,8 @@ func TestDoubleCloseConnIsOk(t *testing.T) {
defer pool.Close()
conn := pool.Get()
assert.EqualValues(t, 0, pool.RP.IdleCount())
_, err := conn.Do("PING")
assert.Nil(t, err)
@ -70,3 +75,13 @@ func TestDoubleCloseConnIsOk(t *testing.T) {
conn.Close()
assert.EqualValues(t, 1, pool.RP.IdleCount())
}
func TestDoOnPool(t *testing.T) {
pool := getPool()
defer pool.Close()
assert.EqualValues(t, 0, pool.RP.IdleCount())
_, err := pool.Do("PING")
assert.EqualValues(t, 1, pool.RP.IdleCount())
assert.Nil(t, err)
}