diff --git a/asyncio_mongo/connection.py b/asyncio_mongo/connection.py index 5999512..241d66b 100644 --- a/asyncio_mongo/connection.py +++ b/asyncio_mongo/connection.py @@ -1,8 +1,8 @@ +import logging +from asyncio_mongo.log import logger from asyncio_mongo.database import Database from .protocol import MongoProtocol -from asyncio.log import logger import asyncio -import logging __all__ = ['Connection'] @@ -24,7 +24,7 @@ class Connection: @classmethod @asyncio.coroutine - def create(cls, host='localhost', port=6379, loop=None, password=None, db=0, auto_reconnect=True): + def create(cls, host='localhost', port=27017, loop=None, auto_reconnect=True): connection = cls() connection.host = host @@ -33,7 +33,7 @@ class Connection: connection._retry_interval = .5 # Create protocol instance - protocol_factory = type('MongoProtocol', (cls.protocol,), { 'password': password, 'db': db }) + protocol_factory = type('MongoProtocol', (cls.protocol,), {}) if auto_reconnect: class protocol_factory(protocol_factory): diff --git a/asyncio_mongo/pool.py b/asyncio_mongo/pool.py index 115b079..5e6fc09 100644 --- a/asyncio_mongo/pool.py +++ b/asyncio_mongo/pool.py @@ -38,22 +38,22 @@ class Pool: @classmethod @asyncio.coroutine - def create(cls, host='localhost', port=6379, loop=None, password=None, db=0, pool_size=1, auto_reconnect=True): + def create(cls, host='localhost', port=27017, loop=None, poolsize=1, auto_reconnect=True): """ - Create a new connection instance. + Create a new pool instance. """ self = cls() self._host = host self._port = port - self._pool_size = pool_size + self._pool_size = poolsize # Create connections self._connections = [] - for i in range(pool_size): + for i in range(poolsize): connection_class = cls.get_connection_class() connection = yield from connection_class.create(host=host, port=port, loop=loop, - password=password, db=db, auto_reconnect=auto_reconnect) + auto_reconnect=auto_reconnect) self._connections.append(connection) return self @@ -66,19 +66,12 @@ class Pool: """ Number of parallel connections in the pool.""" return self._poolsize - @property - def connections_in_use(self): - """ - Return how many protocols are in use. - """ - return sum([ 1 for c in self._connections if c.protocol.in_use ]) - @property def connections_connected(self): """ The amount of open TCP connections. """ - return sum([ 1 for c in self._connections if c.protocol.is_connected ]) + return sum([1 for c in self._connections if c.protocol.is_connected]) def close(self): for conn in self._connections: @@ -93,7 +86,7 @@ class Pool: self._shuffle_connections() for c in self._connections: - if c.protocol.is_connected and not c.protocol.in_use: + if c.protocol.is_connected: return c def _shuffle_connections(self): @@ -116,5 +109,5 @@ class Pool: if connection: return getattr(connection, name) else: - raise NoAvailableConnectionsInPoolError('No available connections in the pool: size=%s, in_use=%s, connected=%s' % ( - self.pool_size, self.connections_in_use, self.connections_connected)) + raise NoAvailableConnectionsInPoolError('No available connections in the pool: size=%s, connected=%s' % ( + self.pool_size, self.connections_connected)) diff --git a/asyncio_mongo/protocol.py b/asyncio_mongo/protocol.py index c42fbf7..513d35e 100644 --- a/asyncio_mongo/protocol.py +++ b/asyncio_mongo/protocol.py @@ -50,7 +50,7 @@ class MongoProtocol(asyncio.Protocol): def connection_made(self, transport): self.transport = transport self._is_connected = True - logger.log(logging.INFO, 'Mongo connection made with %') + logger.log(logging.INFO, 'Mongo connection made') def connection_lost(self, exc): self._is_connected = False @@ -62,6 +62,11 @@ class MongoProtocol(asyncio.Protocol): logger.log(logging.INFO, 'Mongo connection lost') + @property + def is_connected(self): + """ True when the underlying transport is connected. """ + return self._is_connected + def data_received(self, data): while self.__waiting_header: self.__buffer += data diff --git a/tests/test_connection.py b/tests/test_connection.py index 7836624..175304b 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -36,7 +36,7 @@ class TestMongoConnectionMethods(MongoTest): @async def test_pool(self): # MongoConnectionPool returns deferred, which gets MongoAPI - pool = asyncio_mongo.Pool.create(mongo_host, mongo_port, pool_size=2) + pool = asyncio_mongo.Pool.create(mongo_host, mongo_port, poolsize=2) self.assertTrue(inspect.isgenerator(pool)) rapi = yield from pool print('rapi %s' % rapi.__class__)