use Failure for all errors from async methods

This commit is contained in:
meejah 2018-09-04 14:55:36 -06:00
parent 676a9efc23
commit efce7b1f6a
2 changed files with 84 additions and 75 deletions

View File

@ -7,6 +7,7 @@ from twisted.internet import reactor, defer
from twisted.application import service from twisted.application import service
from twisted.application.internet import TimerService from twisted.application.internet import TimerService
from twisted.python.filepath import FilePath from twisted.python.filepath import FilePath
from twisted.python.failure import Failure
from pycryptopp.publickey import rsa from pycryptopp.publickey import rsa
import allmydata import allmydata
@ -196,20 +197,20 @@ def create_client(basedir=u".", _client_factory=None):
instance of :class:`allmydata.node.Node` (or a subclass). By default instance of :class:`allmydata.node.Node` (or a subclass). By default
this is :class:`allmydata.client._Client` this is :class:`allmydata.client._Client`
:returns: :class:`allmydata.client._Client` instance (or whatever :returns: Deferred yielding an instance of :class:`allmydata.client._Client`
`_client_factory` returns)
""" """
node.create_node_dir(basedir, CLIENT_README) try:
config = read_config(basedir, u"client.port") node.create_node_dir(basedir, CLIENT_README)
# following call is async config = read_config(basedir, u"client.port")
return create_client_from_config( # following call is async
config, return create_client_from_config(
_client_factory=_client_factory, config,
) _client_factory=_client_factory,
)
except Exception:
return Failure()
# this method is async
# @defer.inlineCallbacks
def create_client_from_config(config, _client_factory=None): def create_client_from_config(config, _client_factory=None):
""" """
Creates a new client instance (a subclass of Node). Most code Creates a new client instance (a subclass of Node). Most code
@ -223,42 +224,45 @@ def create_client_from_config(config, _client_factory=None):
:param _client_factory: for testing; the class to instantiate :param _client_factory: for testing; the class to instantiate
instead of _Client instead of _Client
""" """
if _client_factory is None: try:
_client_factory = _Client if _client_factory is None:
_client_factory = _Client
i2p_provider = create_i2p_provider(reactor, config) i2p_provider = create_i2p_provider(reactor, config)
tor_provider = create_tor_provider(reactor, config) tor_provider = create_tor_provider(reactor, config)
handlers = node.create_connection_handlers(reactor, config, i2p_provider, tor_provider) handlers = node.create_connection_handlers(reactor, config, i2p_provider, tor_provider)
default_connection_handlers, foolscap_connection_handlers = handlers default_connection_handlers, foolscap_connection_handlers = handlers
tub_options = node.create_tub_options(config) tub_options = node.create_tub_options(config)
main_tub = node.create_main_tub( main_tub = node.create_main_tub(
config, tub_options, default_connection_handlers, config, tub_options, default_connection_handlers,
foolscap_connection_handlers, i2p_provider, tor_provider, foolscap_connection_handlers, i2p_provider, tor_provider,
) )
control_tub = node.create_control_tub() control_tub = node.create_control_tub()
introducer_clients = create_introducer_clients(config, main_tub) introducer_clients = create_introducer_clients(config, main_tub)
storage_broker = create_storage_farm_broker( storage_broker = create_storage_farm_broker(
config, default_connection_handlers, foolscap_connection_handlers, config, default_connection_handlers, foolscap_connection_handlers,
tub_options, introducer_clients tub_options, introducer_clients
) )
client = _client_factory( client = _client_factory(
config, config,
main_tub, main_tub,
control_tub, control_tub,
i2p_provider, i2p_provider,
tor_provider, tor_provider,
introducer_clients, introducer_clients,
storage_broker, storage_broker,
) )
i2p_provider.setServiceParent(client) i2p_provider.setServiceParent(client)
tor_provider.setServiceParent(client) tor_provider.setServiceParent(client)
for ic in introducer_clients: for ic in introducer_clients:
ic.setServiceParent(client) ic.setServiceParent(client)
storage_broker.setServiceParent(client) storage_broker.setServiceParent(client)
return defer.succeed(client) return defer.succeed(client)
except Exception:
return Failure()
def _sequencer(config): def _sequencer(config):

View File

@ -3,6 +3,7 @@ import time, os.path, textwrap
from zope.interface import implementer from zope.interface import implementer
from twisted.application import service from twisted.application import service
from twisted.internet import defer from twisted.internet import defer
from twisted.python.failure import Failure
from foolscap.api import Referenceable from foolscap.api import Referenceable
import allmydata import allmydata
from allmydata import node from allmydata import node
@ -37,45 +38,49 @@ def _valid_config_sections():
class FurlFileConflictError(Exception): class FurlFileConflictError(Exception):
pass pass
# this is/can-be async
# @defer.inlineCallbacks
def create_introducer(basedir=u"."): def create_introducer(basedir=u"."):
# ideally we would pass in reactor """
from twisted.internet import reactor :returns: a Deferred that yields a new _IntroducerNode instance
"""
try:
# see https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2946
from twisted.internet import reactor
if not os.path.exists(basedir): if not os.path.exists(basedir):
create_node_dir(basedir, INTRODUCER_README) create_node_dir(basedir, INTRODUCER_README)
config = read_config( config = read_config(
basedir, u"client.port", basedir, u"client.port",
generated_files=["introducer.furl"], generated_files=["introducer.furl"],
_valid_config_sections=_valid_config_sections, _valid_config_sections=_valid_config_sections,
) )
i2p_provider = create_i2p_provider(reactor, config) i2p_provider = create_i2p_provider(reactor, config)
tor_provider = create_tor_provider(reactor, config) tor_provider = create_tor_provider(reactor, config)
default_connection_handlers, foolscap_connection_handlers = create_connection_handlers(reactor, config, i2p_provider, tor_provider) default_connection_handlers, foolscap_connection_handlers = create_connection_handlers(reactor, config, i2p_provider, tor_provider)
tub_options = create_tub_options(config) tub_options = create_tub_options(config)
# we don't remember these because the Introducer doesn't make # we don't remember these because the Introducer doesn't make
# outbound connections. # outbound connections.
i2p_provider = None i2p_provider = None
tor_provider = None tor_provider = None
main_tub = create_main_tub( main_tub = create_main_tub(
config, tub_options, default_connection_handlers, config, tub_options, default_connection_handlers,
foolscap_connection_handlers, i2p_provider, tor_provider, foolscap_connection_handlers, i2p_provider, tor_provider,
) )
control_tub = create_control_tub() control_tub = create_control_tub()
node = _IntroducerNode( node = _IntroducerNode(
config, config,
main_tub, main_tub,
control_tub, control_tub,
i2p_provider, i2p_provider,
tor_provider, tor_provider,
) )
return defer.succeed(node) return defer.succeed(node)
except Exception:
return Failure()
class _IntroducerNode(node.Node): class _IntroducerNode(node.Node):