Don't hold lock on initial client construction

cached_feeds
Tyler Goodlet 2021-03-31 14:04:59 -04:00
parent 3ebb7ab6b1
commit b2eacb85d4
1 changed files with 10 additions and 8 deletions

View File

@ -22,7 +22,6 @@ from typing import Dict
from contextlib import asynccontextmanager, AsyncExitStack from contextlib import asynccontextmanager, AsyncExitStack
import trio import trio
import tractor
from . import get_brokermod from . import get_brokermod
from ..log import get_logger from ..log import get_logger
@ -30,10 +29,12 @@ from ..log import get_logger
log = get_logger(__name__) log = get_logger(__name__)
_cache: Dict[str, 'Client'] = {}
_cache: Dict[str, 'Client'] = {} # noqa
@asynccontextmanager @asynccontextmanager
async def get_cached_client( async def open_cached_client(
brokername: str, brokername: str,
*args, *args,
**kwargs, **kwargs,
@ -77,7 +78,8 @@ async def get_cached_client(
yield client yield client
finally: finally:
if client is not None:
# if no more consumers, teardown the client
client._consumers -= 1 client._consumers -= 1
if client._consumers <= 0: if client._consumers <= 0:
# teardown the client
await client._exit_stack.aclose() await client._exit_stack.aclose()