Skip to content

Commit

Permalink
move db selection from pool to client object
Browse files Browse the repository at this point in the history
  • Loading branch information
thefab committed Sep 26, 2016
1 parent abc49a0 commit dbebdc7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
7 changes: 7 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def test_init(self):
yield c.connect()
c.disconnect()

@tornado.testing.gen_test
def test_init_with_db(self):
c = Client(db=2)
yield c.connect()
self.assertEqual(c.db, 2)
c.disconnect()

@tornado.testing.gen_test
def test_ping(self):
c = Client()
Expand Down
6 changes: 1 addition & 5 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ def test_get_client_select_db_after_connect(self):
db = 13
c = ClientPool(db=db)
client = yield c.get_connected_client()

self.assertIsInstance(client, Client)
self.assertEqual(db, c.db)

self.assertEqual(db, client.db)
c.release_client(client)
c.destroy()

Expand All @@ -119,9 +117,7 @@ def test_get_client_invalid_select_db_after_connect(self):
db = 'non-existent-db'
c = ClientPool(db=db)
client = yield c.get_connected_client()

self.assertIsInstance(client, ClientError)

c.destroy()

@tornado.testing.gen_test
Expand Down
30 changes: 19 additions & 11 deletions tornadis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ class Client(object):
autoconnect (boolean): True if the client is in autoconnect mode
(and in autoreconnection mode) (default True).
password (string): the password to authenticate with.
db (int) database number.
connection_kwargs (dict): :class:`Connection` object
kwargs (note that read_callback and close_callback args are
set automatically).
"""

def __init__(self, autoconnect=True, password=None, **connection_kwargs):
def __init__(self, autoconnect=True, password=None, db=0,
**connection_kwargs):
"""Constructor.
Args:
autoconnect (boolean): True if the client is in autoconnect mode
(and in autoreconnection mode) (default True).
password (string): the password to authenticate with.
db (int) database number.
**connection_kwargs: :class:`Connection` object kwargs.
"""
if 'read_callback' in connection_kwargs or \
Expand All @@ -53,6 +56,7 @@ def __init__(self, autoconnect=True, password=None, **connection_kwargs):
self.connection_kwargs = connection_kwargs
self.autoconnect = autoconnect
self.password = password
self.db = db
self.__connection = None
self.subscribed = False
self.__connection = None
Expand Down Expand Up @@ -96,18 +100,22 @@ def connect(self):
kwargs = self.connection_kwargs
self.__connection = Connection(cb1, cb2, **kwargs)
connection_status = yield self.__connection.connect()
if connection_status is not True or not self.password:
if connection_status is not True:
# nothing left to do here, return
raise tornado.gen.Return(connection_status)
authentication_status = yield self._call('AUTH', self.password)
if authentication_status == b'OK':
# correct password, it worked
raise tornado.gen.Return(True)
else:
# incorrect password, return back the result
LOG.warning("impossible to connect: bad password")
self.__connection.disconnect()
raise tornado.gen.Return(False)
if self.password is not None:
authentication_status = yield self._call('AUTH', self.password)
if authentication_status != b'OK':
# incorrect password, return back the result
LOG.warning("impossible to connect: bad password")
self.__connection.disconnect()
raise tornado.gen.Return(False)
if self.db != 0:
db_status = yield self._call('SELECT', self.db)
if db_status != b'OK':
LOG.warning("can't select db %s", self.db)
raise tornado.gen.Return(False)
raise tornado.gen.Return(True)

def disconnect(self):
"""Disconnects the client object from redis.
Expand Down
13 changes: 1 addition & 12 deletions tornadis/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class ClientPool(object):
"""High level object to deal with a pool of redis clients."""

def __init__(self, max_size=-1, client_timeout=-1, autoclose=False, db=0,
def __init__(self, max_size=-1, client_timeout=-1, autoclose=False,
**client_kwargs):
"""Constructor.
Expand All @@ -32,14 +32,11 @@ def __init__(self, max_size=-1, client_timeout=-1, autoclose=False, db=0,
autoclose (boolean): automatically disconnect released connections
with lifetime > client_timeout (test made every
client_timeout/10 seconds).
db (int) database number
client_kwargs (dict): Client constructor arguments.
"""
self.max_size = max_size
self.client_timeout = client_timeout
self.db = db
self.client_kwargs = client_kwargs

self.__ioloop = client_kwargs.get('ioloop',
tornado.ioloop.IOLoop.instance())
self.autoclose = autoclose
Expand Down Expand Up @@ -91,14 +88,6 @@ def get_connected_client(self):
LOG.warning("can't connect to %s", client.title)
raise tornado.gen.Return(
ClientError("can't connect to %s" % client.title))

if self.db != 0:
res = yield client.call('SELECT', self.db)

if isinstance(res, ClientError):
LOG.warning("can't select db %s", self.db)
raise tornado.gen.Return(res)

raise tornado.gen.Return(client)

def get_client_nowait(self):
Expand Down

0 comments on commit dbebdc7

Please sign in to comment.