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,
@ -74,10 +75,11 @@ async def get_cached_client(
client._exit_stack = exit_stack client._exit_stack = exit_stack
clients[brokername] = client clients[brokername] = client
yield client yield client
finally: finally:
client._consumers -= 1 if client is not None:
if client._consumers <= 0: # if no more consumers, teardown the client
# teardown the client client._consumers -= 1
await client._exit_stack.aclose() if client._consumers <= 0:
await client._exit_stack.aclose()