From 7756ae11cf86ec8e4213eb151ffdc504b16291fb Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Mon, 31 Oct 2022 16:05:04 +0300 Subject: [PATCH] Removing Auto() sugar since it's not safe --- rdb.go | 52 ++++++---------------------------------------------- rdb_test.go | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/rdb.go b/rdb.go index 3a36d55..f6edad7 100644 --- a/rdb.go +++ b/rdb.go @@ -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") -} diff --git a/rdb_test.go b/rdb_test.go index 69275d2..6f0f21a 100644 --- a/rdb_test.go +++ b/rdb_test.go @@ -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) +}