Return early on remote actor lookups instead of branching

supervise
Tyler Goodlet 2021-03-22 09:30:30 -04:00
parent 8ce37875a0
commit 4ab8545e87
1 changed files with 36 additions and 31 deletions

View File

@ -1,7 +1,7 @@
""" """
pikerd daemon lifecylcle & rpc pikerd daemon lifecylcle & rpc
""" """
from typing import Optional, Dict, Callable from typing import Optional
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import tractor import tractor
@ -23,35 +23,32 @@ _root_modules = [
@asynccontextmanager @asynccontextmanager
async def open_pikerd( async def open_pikerd(
loglevel: Optional[str] = None loglevel: Optional[str] = None,
**kwargs,
) -> Optional[tractor._portal.Portal]: ) -> Optional[tractor._portal.Portal]:
global _root_nursery global _root_nursery
async with tractor.open_root_actor( # XXX: this may open a root actor as well
async with tractor.open_nursery(
name=_root_dname, name=_root_dname,
loglevel=loglevel,
# TODO: eventually we should be able to avoid # TODO: eventually we should be able to avoid
# having the root have more then permissions to # having the root have more then permissions to
# spawn other specialized daemons I think? # spawn other specialized daemons I think?
# enable_modules=[__name__], # enable_modules=[__name__],
enable_modules=_root_modules enable_modules=_root_modules,
):
# init root nursery loglevel=loglevel,
try: ) as nursery:
async with tractor.open_nursery() as nursery: _root_nursery = nursery
_root_nursery = nursery yield nursery
yield nursery
finally:
# client code may block indefinitely so cancel when
# teardown is invoked
await nursery.cancel()
@asynccontextmanager @asynccontextmanager
async def maybe_open_pikerd( async def maybe_open_pikerd(
loglevel: Optional[str] = None loglevel: Optional[str] = None,
**kwargs,
) -> Optional[tractor._portal.Portal]: ) -> Optional[tractor._portal.Portal]:
"""If no ``pikerd`` daemon-root-actor can be found, """If no ``pikerd`` daemon-root-actor can be found,
assume that role and return a portal to myself assume that role and return a portal to myself
@ -62,18 +59,27 @@ async def maybe_open_pikerd(
if loglevel: if loglevel:
get_console_log(loglevel) get_console_log(loglevel)
async with tractor.find_actor(_root_dname) as portal: try:
async with tractor.find_actor(_root_dname) as portal:
if portal is not None: # pikerd exists
yield portal
return
if portal is not None: # pikerd exists except RuntimeError: # tractor runtime not started yet
yield portal pass
else: # assume role # assume pikerd role
async with open_pikerd(loglevel) as nursery: async with open_pikerd(
# in the case where we're starting up the loglevel,
# tractor-piker runtime stack in **this** process **kwargs,
# we want to hand off a nursery for starting (as a sub) ):
# whatever actor is requesting pikerd. assert _root_nursery
yield None
# in the case where we're starting up the
# tractor-piker runtime stack in **this** process
# we want to hand off a nursery for starting (as a sub)
# whatever actor is requesting pikerd.
yield None
# brokerd enabled modules # brokerd enabled modules
@ -101,12 +107,14 @@ async def spawn_brokerd(
# TODO: raise exception when _root_nursery == None? # TODO: raise exception when _root_nursery == None?
global _root_nursery global _root_nursery
portal = await _root_nursery.start_actor(
await _root_nursery.start_actor(
dname, dname,
enable_modules=_data_mods + [brokermod.__name__], enable_modules=_data_mods + [brokermod.__name__],
loglevel=loglevel, loglevel=loglevel,
**tractor_kwargs **tractor_kwargs
) )
return dname return dname
@ -116,15 +124,12 @@ async def spawn_emsd(
**extra_tractor_kwargs **extra_tractor_kwargs
) -> tractor._portal.Portal: ) -> tractor._portal.Portal:
from .clearing import _client
log.info('Spawning emsd') log.info('Spawning emsd')
# TODO: raise exception when _root_nursery == None? # TODO: raise exception when _root_nursery == None?
global _root_nursery global _root_nursery
assert _root_nursery
portal = await _root_nursery.start_actor( await _root_nursery.start_actor(
'emsd', 'emsd',
enable_modules=[ enable_modules=[
'piker.clearing._ems', 'piker.clearing._ems',