diff --git a/src/risotto/dispatcher.py b/src/risotto/dispatcher.py index 532a156..5df457e 100644 --- a/src/risotto/dispatcher.py +++ b/src/risotto/dispatcher.py @@ -92,9 +92,7 @@ class CallDispatcher: if function_obj['database'] and hasattr(old_risotto_context, 'connection'): risotto_context.connection = old_risotto_context.connection if function_obj['database'] and not hasattr(risotto_context, 'connection'): - db_conf = get_config().get('database') - pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) - async with pool.acquire() as connection: + async with self.pool.acquire() as connection: await connection.set_type_codec( 'json', encoder=dumps, @@ -181,9 +179,7 @@ class PublishDispatcher: if function_obj['database'] and hasattr(old_risotto_context, 'connection'): risotto_context.connection = old_risotto_context.connection if function_obj['database'] and not hasattr(risotto_context, 'connection'): - db_conf = get_config().get('database') - pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) - async with pool.acquire() as connection: + async with self.pool.acquire() as connection: await connection.set_type_codec( 'json', encoder=dumps, @@ -283,9 +279,7 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher) config: Config, user_login: str, uri: str) -> None: - db_conf = get_config().get('database') - pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) - async with pool.acquire() as connection: + async with self.pool.acquire() as connection: async with connection.transaction(): # Verify if user exists and get ID sql = ''' diff --git a/src/risotto/register.py b/src/risotto/register.py index 9089d8d..28c82c4 100644 --- a/src/risotto/register.py +++ b/src/risotto/register.py @@ -33,6 +33,8 @@ class RegisterDispatcher: def __init__(self): # reference to instanciate module (to inject self in method): {"module_name": instance_of_module} self.injected_self = {} + # postgresql pool + self.pool = None # list of uris with informations: {"v1": {"module_name.xxxxx": yyyyyy}} self.messages = {} # load tiramisu objects @@ -261,8 +263,8 @@ class RegisterDispatcher: async def load(self): # valid function's arguments db_conf = get_config().get('database') - pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) - async with pool.acquire() as connection: + self.pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) + async with self.pool.acquire() as connection: async with connection.transaction(): for version, messages in self.messages.items(): for message, message_infos in messages.items():