Initial commit

This commit is contained in:
2026-02-01 09:31:38 +01:00
commit e02db93960
4396 changed files with 1511612 additions and 0 deletions

View File

@@ -0,0 +1,866 @@
"""Implements `AMQPConnectionWorkflow` - the default workflow of performing
multiple TCP/[SSL]/AMQP connection attempts with timeouts and retries until one
succeeds or all attempts fail.
Defines the interface `AbstractAMQPConnectionWorkflow` that facilitates
implementing custom connection workflows.
"""
import functools
import logging
import socket
import pika.compat
import pika.exceptions
import pika.tcp_socket_opts
from pika import __version__
_LOG = logging.getLogger(__name__)
class AMQPConnectorException(Exception):
"""Base exception for this module"""
class AMQPConnectorStackTimeout(AMQPConnectorException):
"""Overall TCP/[SSL]/AMQP stack connection attempt timed out."""
class AMQPConnectorAborted(AMQPConnectorException):
"""Asynchronous request was aborted"""
class AMQPConnectorWrongState(AMQPConnectorException):
"""AMQPConnector operation requested in wrong state, such as aborting after
completion was reported.
"""
class AMQPConnectorPhaseErrorBase(AMQPConnectorException):
"""Wrapper for exception that occurred during a particular bring-up phase.
"""
def __init__(self, exception, *args):
"""
:param BaseException exception: error that occurred while waiting for a
subclass-specific protocol bring-up phase to complete.
:param args: args for parent class
"""
super(AMQPConnectorPhaseErrorBase, self).__init__(*args)
self.exception = exception
def __repr__(self):
return '{}: {!r}'.format(self.__class__.__name__, self.exception)
class AMQPConnectorSocketConnectError(AMQPConnectorPhaseErrorBase):
"""Error connecting TCP socket to remote peer"""
class AMQPConnectorTransportSetupError(AMQPConnectorPhaseErrorBase):
"""Error setting up transport after TCP connected but before AMQP handshake.
"""
class AMQPConnectorAMQPHandshakeError(AMQPConnectorPhaseErrorBase):
"""Error during AMQP handshake"""
class AMQPConnectionWorkflowAborted(AMQPConnectorException):
"""AMQP Connection workflow was aborted."""
class AMQPConnectionWorkflowWrongState(AMQPConnectorException):
"""AMQP Connection Workflow operation requested in wrong state, such as
aborting after completion was reported.
"""
class AMQPConnectionWorkflowFailed(AMQPConnectorException):
"""Indicates that AMQP connection workflow failed.
"""
def __init__(self, exceptions, *args):
"""
:param sequence exceptions: Exceptions that occurred during the
workflow.
:param args: args to pass to base class
"""
super(AMQPConnectionWorkflowFailed, self).__init__(*args)
self.exceptions = tuple(exceptions)
def __repr__(self):
return ('{}: {} exceptions in all; last exception - {!r}; first '
'exception - {!r}').format(
self.__class__.__name__, len(self.exceptions),
self.exceptions[-1],
self.exceptions[0] if len(self.exceptions) > 1 else None)
class AMQPConnector(object):
"""Performs a single TCP/[SSL]/AMQP connection workflow.
"""
_STATE_INIT = 0 # start() hasn't been called yet
_STATE_TCP = 1 # TCP/IP connection establishment
_STATE_TRANSPORT = 2 # [SSL] and transport linkup
_STATE_AMQP = 3 # AMQP connection handshake
_STATE_TIMEOUT = 4 # overall TCP/[SSL]/AMQP timeout
_STATE_ABORTING = 5 # abort() called - aborting workflow
_STATE_DONE = 6 # result reported to client
def __init__(self, conn_factory, nbio):
"""
:param callable conn_factory: A function that takes
`pika.connection.Parameters` as its only arg and returns a brand new
`pika.connection.Connection`-based adapter instance each time it is
called. The factory must instantiate the connection with
`internal_connection_workflow=False`.
:param pika.adapters.utils.nbio_interface.AbstractIOServices nbio:
"""
self._conn_factory = conn_factory
self._nbio = nbio
self._addr_record = None # type: tuple
self._conn_params = None # type: pika.connection.Parameters
self._on_done = None # will be provided via start()
# TCP connection timeout
# pylint: disable=C0301
self._tcp_timeout_ref = None # type: pika.adapters.utils.nbio_interface.AbstractTimerReference
# Overall TCP/[SSL]/AMQP timeout
self._stack_timeout_ref = None # type: pika.adapters.utils.nbio_interface.AbstractTimerReference
# Current task
self._task_ref = None # type: pika.adapters.utils.nbio_interface.AbstractIOReference
self._sock = None # type: socket.socket
self._amqp_conn = None # type: pika.connection.Connection
self._state = self._STATE_INIT
def start(self, addr_record, conn_params, on_done):
"""Asynchronously perform a single TCP/[SSL]/AMQP connection attempt.
:param tuple addr_record: a single resolved address record compatible
with `socket.getaddrinfo()` format.
:param pika.connection.Parameters conn_params:
:param callable on_done: Function to call upon completion of the
workflow: `on_done(pika.connection.Connection | BaseException)`. If
exception, it's going to be one of the following:
`AMQPConnectorSocketConnectError`
`AMQPConnectorTransportSetupError`
`AMQPConnectorAMQPHandshakeError`
`AMQPConnectorAborted`
"""
if self._state != self._STATE_INIT:
raise AMQPConnectorWrongState(
'Already in progress or finished; state={}'.format(self._state))
self._addr_record = addr_record
self._conn_params = conn_params
self._on_done = on_done
# Create socket and initiate TCP/IP connection
self._state = self._STATE_TCP
self._sock = socket.socket(*self._addr_record[:3])
self._sock.setsockopt(pika.compat.SOL_TCP, socket.TCP_NODELAY, 1)
pika.tcp_socket_opts.set_sock_opts(self._conn_params.tcp_options,
self._sock)
self._sock.setblocking(False)
addr = self._addr_record[4]
_LOG.info('Pika version %s connecting to %r', __version__, addr)
self._task_ref = self._nbio.connect_socket(
self._sock, addr, on_done=self._on_tcp_connection_done)
# Start socket connection timeout timer
self._tcp_timeout_ref = None
if self._conn_params.socket_timeout is not None:
self._tcp_timeout_ref = self._nbio.call_later(
self._conn_params.socket_timeout,
self._on_tcp_connection_timeout)
# Start overall TCP/[SSL]/AMQP stack connection timeout timer
self._stack_timeout_ref = None
if self._conn_params.stack_timeout is not None:
self._stack_timeout_ref = self._nbio.call_later(
self._conn_params.stack_timeout, self._on_overall_timeout)
def abort(self):
"""Abort the workflow asynchronously. The completion callback will be
called with an instance of AMQPConnectorAborted.
NOTE: we can't cancel/close synchronously because aborting pika
Connection and its transport requires an asynchronous operation.
:raises AMQPConnectorWrongState: If called after completion has been
reported or the workflow not started yet.
"""
if self._state == self._STATE_INIT:
raise AMQPConnectorWrongState('Cannot abort before starting.')
if self._state == self._STATE_DONE:
raise AMQPConnectorWrongState('Cannot abort after completion was reported')
self._state = self._STATE_ABORTING
self._deactivate()
_LOG.info(
'AMQPConnector: beginning client-initiated asynchronous '
'abort; %r/%s', self._conn_params.host, self._addr_record)
if self._amqp_conn is None:
_LOG.debug('AMQPConnector.abort(): no connection, so just '
'scheduling completion report via I/O loop.')
self._nbio.add_callback_threadsafe(
functools.partial(self._report_completion_and_cleanup,
AMQPConnectorAborted()))
else:
if not self._amqp_conn.is_closing:
# Initiate close of AMQP connection and wait for asynchronous
# callback from the Connection instance before reporting
# completion to client
_LOG.debug('AMQPConnector.abort(): closing Connection.')
self._amqp_conn.close(
320, 'Client-initiated abort of AMQP Connection Workflow.')
else:
# It's already closing, must be due to our timeout processing,
# so we'll just piggy back on the callback it registered
_LOG.debug('AMQPConnector.abort(): closing of Connection was '
'already initiated.')
assert self._state == self._STATE_TIMEOUT, \
('Connection is closing, but not in TIMEOUT state; state={}'
.format(self._state))
def _close(self):
"""Cancel asynchronous tasks and clean up to assist garbage collection.
Transition to STATE_DONE.
"""
self._deactivate()
if self._sock is not None:
self._sock.close()
self._sock = None
self._conn_factory = None
self._nbio = None
self._addr_record = None
self._on_done = None
self._state = self._STATE_DONE
def _deactivate(self):
"""Cancel asynchronous tasks.
"""
# NOTE: self._amqp_conn requires special handling as it doesn't support
# synchronous closing. We special-case it elsewhere in the code where
# needed.
assert self._amqp_conn is None, \
'_deactivate called with self._amqp_conn not None; state={}'.format(
self._state)
if self._tcp_timeout_ref is not None:
self._tcp_timeout_ref.cancel()
self._tcp_timeout_ref = None
if self._stack_timeout_ref is not None:
self._stack_timeout_ref.cancel()
self._stack_timeout_ref = None
if self._task_ref is not None:
self._task_ref.cancel()
self._task_ref = None
def _report_completion_and_cleanup(self, result):
"""Clean up and invoke client's `on_done` callback.
:param pika.connection.Connection | BaseException result: value to pass
to user's `on_done` callback.
"""
if isinstance(result, BaseException):
_LOG.error('AMQPConnector - reporting failure: %r', result)
else:
_LOG.info('AMQPConnector - reporting success: %r', result)
on_done = self._on_done
self._close()
on_done(result)
def _on_tcp_connection_timeout(self):
"""Handle TCP connection timeout
Reports AMQPConnectorSocketConnectError with socket.timeout inside.
"""
self._tcp_timeout_ref = None
error = AMQPConnectorSocketConnectError(
socket.timeout('TCP connection attempt timed out: {!r}/{}'.format(
self._conn_params.host, self._addr_record)))
self._report_completion_and_cleanup(error)
def _on_overall_timeout(self):
"""Handle overall TCP/[SSL]/AMQP connection attempt timeout by reporting
`Timeout` error to the client.
Reports AMQPConnectorSocketConnectError if timeout occurred during
socket TCP connection attempt.
Reports AMQPConnectorTransportSetupError if timeout occurred during
tramsport [SSL] setup attempt.
Reports AMQPConnectorAMQPHandshakeError if timeout occurred during
AMQP handshake.
"""
self._stack_timeout_ref = None
prev_state = self._state
self._state = self._STATE_TIMEOUT
if prev_state == self._STATE_AMQP:
msg = ('Timeout while setting up AMQP to {!r}/{}; ssl={}'.format(
self._conn_params.host, self._addr_record,
bool(self._conn_params.ssl_options)))
_LOG.error(msg)
# Initiate close of AMQP connection and wait for asynchronous
# callback from the Connection instance before reporting completion
# to client
assert not self._amqp_conn.is_open, \
'Unexpected open state of {!r}'.format(self._amqp_conn)
if not self._amqp_conn.is_closing:
self._amqp_conn.close(320, msg)
return
if prev_state == self._STATE_TCP:
error = AMQPConnectorSocketConnectError(
AMQPConnectorStackTimeout(
'Timeout while connecting socket to {!r}/{}'.format(
self._conn_params.host, self._addr_record)))
else:
assert prev_state == self._STATE_TRANSPORT
error = AMQPConnectorTransportSetupError(
AMQPConnectorStackTimeout(
'Timeout while setting up transport to {!r}/{}; ssl={}'.
format(self._conn_params.host, self._addr_record,
bool(self._conn_params.ssl_options))))
self._report_completion_and_cleanup(error)
def _on_tcp_connection_done(self, exc):
"""Handle completion of asynchronous socket connection attempt.
Reports AMQPConnectorSocketConnectError if TCP socket connection
failed.
:param None|BaseException exc: None on success; exception object on
failure
"""
self._task_ref = None
if self._tcp_timeout_ref is not None:
self._tcp_timeout_ref.cancel()
self._tcp_timeout_ref = None
if exc is not None:
_LOG.error('TCP Connection attempt failed: %r; dest=%r', exc,
self._addr_record)
self._report_completion_and_cleanup(
AMQPConnectorSocketConnectError(exc))
return
# We succeeded in making a TCP/IP connection to the server
_LOG.debug('TCP connection to broker established: %r.', self._sock)
# Now set up the transport
self._state = self._STATE_TRANSPORT
ssl_context = server_hostname = None
if self._conn_params.ssl_options is not None:
ssl_context = self._conn_params.ssl_options.context
server_hostname = self._conn_params.ssl_options.server_hostname
if server_hostname is None:
server_hostname = self._conn_params.host
self._task_ref = self._nbio.create_streaming_connection(
protocol_factory=functools.partial(self._conn_factory,
self._conn_params),
sock=self._sock,
ssl_context=ssl_context,
server_hostname=server_hostname,
on_done=self._on_transport_establishment_done)
self._sock = None # create_streaming_connection() takes ownership
def _on_transport_establishment_done(self, result):
"""Handle asynchronous completion of
`AbstractIOServices.create_streaming_connection()`
Reports AMQPConnectorTransportSetupError if transport ([SSL]) setup
failed.
:param sequence|BaseException result: On success, a two-tuple
(transport, protocol); on failure, exception instance.
"""
self._task_ref = None
if isinstance(result, BaseException):
_LOG.error(
'Attempt to create the streaming transport failed: %r; '
'%r/%s; ssl=%s', result, self._conn_params.host,
self._addr_record, bool(self._conn_params.ssl_options))
self._report_completion_and_cleanup(
AMQPConnectorTransportSetupError(result))
return
# We succeeded in setting up the streaming transport!
# result is a two-tuple (transport, protocol)
_LOG.info('Streaming transport linked up: %r.', result)
_transport, self._amqp_conn = result
# AMQP handshake is in progress - initiated during transport link-up
self._state = self._STATE_AMQP
# We explicitly remove default handler because it raises an exception.
self._amqp_conn.add_on_open_error_callback(
self._on_amqp_handshake_done, remove_default=True)
self._amqp_conn.add_on_open_callback(self._on_amqp_handshake_done)
def _on_amqp_handshake_done(self, connection, error=None):
"""Handle completion of AMQP connection handshake attempt.
NOTE: we handle two types of callbacks - success with just connection
arg as well as the open-error callback with connection and error
Reports AMQPConnectorAMQPHandshakeError if AMQP handshake failed.
:param pika.connection.Connection connection:
:param BaseException | None error: None on success, otherwise
failure
"""
_LOG.debug(
'AMQPConnector: AMQP handshake attempt completed; state=%s; '
'error=%r; %r/%s', self._state, error, self._conn_params.host,
self._addr_record)
# Don't need it any more; and _deactivate() checks that it's None
self._amqp_conn = None
if self._state == self._STATE_ABORTING:
# Client-initiated abort takes precedence over timeout
result = AMQPConnectorAborted()
elif self._state == self._STATE_TIMEOUT:
result = AMQPConnectorAMQPHandshakeError(
AMQPConnectorStackTimeout(
'Timeout during AMQP handshake{!r}/{}; ssl={}'.format(
self._conn_params.host, self._addr_record,
bool(self._conn_params.ssl_options))))
elif self._state == self._STATE_AMQP:
if error is None:
_LOG.debug(
'AMQPConnector: AMQP connection established for %r/%s: %r',
self._conn_params.host, self._addr_record, connection)
result = connection
else:
_LOG.debug(
'AMQPConnector: AMQP connection handshake failed for '
'%r/%s: %r', self._conn_params.host, self._addr_record,
error)
result = AMQPConnectorAMQPHandshakeError(error)
else:
# We timed out or aborted and initiated closing of the connection,
# but this callback snuck in
_LOG.debug(
'AMQPConnector: Ignoring AMQP handshake completion '
'notification due to wrong state=%s; error=%r; conn=%r',
self._state, error, connection)
return
self._report_completion_and_cleanup(result)
class AbstractAMQPConnectionWorkflow(pika.compat.AbstractBase):
"""Interface for implementing a custom TCP/[SSL]/AMQP connection workflow.
"""
def start(self, connection_configs, connector_factory, native_loop,
on_done):
"""Asynchronously perform the workflow until success or all retries
are exhausted. Called by the adapter.
:param sequence connection_configs: A sequence of one or more
`pika.connection.Parameters`-based objects. Will attempt to connect
using each config in the given order.
:param callable connector_factory: call it without args to obtain a new
instance of `AMQPConnector` for each connection attempt.
See `AMQPConnector` for details.
:param native_loop: Native I/O loop passed by app to the adapter or
obtained by the adapter by default.
:param callable on_done: Function to call upon completion of the
workflow:
`on_done(pika.connection.Connection |
AMQPConnectionWorkflowFailed |
AMQPConnectionWorkflowAborted)`.
`Connection`-based adapter on success,
`AMQPConnectionWorkflowFailed` on failure,
`AMQPConnectionWorkflowAborted` if workflow was aborted.
:raises AMQPConnectionWorkflowWrongState: If called in wrong state, such
as after starting the workflow.
"""
raise NotImplementedError
def abort(self):
"""Abort the workflow asynchronously. The completion callback will be
called with an instance of AMQPConnectionWorkflowAborted.
NOTE: we can't cancel/close synchronously because aborting pika
Connection and its transport requires an asynchronous operation.
:raises AMQPConnectionWorkflowWrongState: If called in wrong state, such
as before starting or after completion has been reported.
"""
raise NotImplementedError
class AMQPConnectionWorkflow(AbstractAMQPConnectionWorkflow):
"""Implements Pika's default workflow for performing multiple TCP/[SSL]/AMQP
connection attempts with timeouts and retries until one succeeds or all
attempts fail.
The workflow:
while not success and retries remain:
1. For each given config (pika.connection.Parameters object):
A. Perform DNS resolution of the config's host.
B. Attempt to establish TCP/[SSL]/AMQP for each resolved address
until one succeeds, in which case we're done.
2. If all configs failed but retries remain, resume from beginning
after the given retry pause. NOTE: failure of DNS resolution
is equivalent to one cycle and will be retried after the pause
if retries remain.
"""
_SOCK_TYPE = socket.SOCK_STREAM
_IPPROTO = socket.IPPROTO_TCP
_STATE_INIT = 0
_STATE_ACTIVE = 1
_STATE_ABORTING = 2
_STATE_DONE = 3
def __init__(self, _until_first_amqp_attempt=False):
"""
:param int | float retry_pause: Non-negative number of seconds to wait
before retrying the config sequence. Meaningful only if retries is
greater than 0. Defaults to 2 seconds.
:param bool _until_first_amqp_attempt: INTERNAL USE ONLY; ends workflow
after first AMQP handshake attempt, regardless of outcome (success
or failure). The automatic connection logic in
`pika.connection.Connection` enables this because it's not
designed/tested to reset all state properly to handle more than one
AMQP handshake attempt.
TODO: Do we need getaddrinfo timeout?
TODO: Would it be useful to implement exponential back-off?
"""
self._attempts_remaining = None # supplied by start()
self._retry_pause = None # supplied by start()
self._until_first_amqp_attempt = _until_first_amqp_attempt
# Provided by set_io_services()
# pylint: disable=C0301
self._nbio = None # type: pika.adapters.utils.nbio_interface.AbstractIOServices
# Current index within `_connection_configs`; initialized when
# starting a new connection sequence.
self._current_config_index = None
self._connection_configs = None # supplied by start()
self._connector_factory = None # supplied by start()
self._on_done = None # supplied by start()
self._connector = None # type: AMQPConnector
self._task_ref = None # current cancelable asynchronous task or timer
self._addrinfo_iter = None
# Exceptions from all failed connection attempts in this workflow
self._connection_errors = []
self._state = self._STATE_INIT
def set_io_services(self, nbio):
"""Called by the conneciton adapter only on pika's
`AMQPConnectionWorkflow` instance to provide it the adapter-specific
`AbstractIOServices` object before calling the `start()` method.
NOTE: Custom workflow implementations should use the native I/O loop
directly because `AbstractIOServices` is private to Pika
implementation and its interface may change without notice.
:param pika.adapters.utils.nbio_interface.AbstractIOServices nbio:
"""
self._nbio = nbio
def start(
self,
connection_configs,
connector_factory,
native_loop, # pylint: disable=W0613
on_done):
"""Override `AbstractAMQPConnectionWorkflow.start()`.
NOTE: This implementation uses `connection_attempts` and `retry_delay`
values from the last element of the given `connection_configs` sequence
as the overall number of connection attempts of the entire
`connection_configs` sequence and pause between each sequence.
"""
if self._state != self._STATE_INIT:
raise AMQPConnectorWrongState(
'Already in progress or finished; state={}'.format(self._state))
try:
iter(connection_configs)
except Exception as error:
raise TypeError(
'connection_configs does not support iteration: {!r}'.format(
error))
if not connection_configs:
raise ValueError(
'connection_configs is empty: {!r}.'.format(connection_configs))
self._connection_configs = connection_configs
self._connector_factory = connector_factory
self._on_done = on_done
self._attempts_remaining = connection_configs[-1].connection_attempts
self._retry_pause = connection_configs[-1].retry_delay
self._state = self._STATE_ACTIVE
_LOG.debug('Starting AMQP Connection workflow asynchronously.')
# Begin from our own I/O loop context to avoid calling back into client
# from client's call here
self._task_ref = self._nbio.call_later(
0, functools.partial(self._start_new_cycle_async, first=True))
def abort(self):
"""Override `AbstractAMQPConnectionWorkflow.abort()`.
"""
if self._state == self._STATE_INIT:
raise AMQPConnectorWrongState('Cannot abort before starting.')
elif self._state == self._STATE_DONE:
raise AMQPConnectorWrongState(
'Cannot abort after completion was reported')
self._state = self._STATE_ABORTING
self._deactivate()
_LOG.info('AMQPConnectionWorkflow: beginning client-initiated '
'asynchronous abort.')
if self._connector is None:
_LOG.debug('AMQPConnectionWorkflow.abort(): no connector, so just '
'scheduling completion report via I/O loop.')
self._nbio.add_callback_threadsafe(
functools.partial(self._report_completion_and_cleanup,
AMQPConnectionWorkflowAborted()))
else:
_LOG.debug('AMQPConnectionWorkflow.abort(): requesting '
'connector.abort().')
self._connector.abort()
def _close(self):
"""Cancel asynchronous tasks and clean up to assist garbage collection.
Transition to _STATE_DONE.
"""
self._deactivate()
self._connection_configs = None
self._nbio = None
self._connector_factory = None
self._on_done = None
self._connector = None
self._addrinfo_iter = None
self._connection_errors = None
self._state = self._STATE_DONE
def _deactivate(self):
"""Cancel asynchronous tasks.
"""
if self._task_ref is not None:
self._task_ref.cancel()
self._task_ref = None
def _report_completion_and_cleanup(self, result):
"""Clean up and invoke client's `on_done` callback.
:param pika.connection.Connection | AMQPConnectionWorkflowFailed result:
value to pass to user's `on_done` callback.
"""
if isinstance(result, BaseException):
_LOG.error('AMQPConnectionWorkflow - reporting failure: %r', result)
else:
_LOG.info('AMQPConnectionWorkflow - reporting success: %r', result)
on_done = self._on_done
self._close()
on_done(result)
def _start_new_cycle_async(self, first):
"""Start a new workflow cycle (if any more attempts are left) beginning
with the first Parameters object in self._connection_configs. If out of
attempts, report `AMQPConnectionWorkflowFailed`.
:param bool first: if True, don't delay; otherwise delay next attempt by
`self._retry_pause` seconds.
"""
self._task_ref = None
assert self._attempts_remaining >= 0, self._attempts_remaining
if self._attempts_remaining <= 0:
error = AMQPConnectionWorkflowFailed(self._connection_errors)
_LOG.error('AMQP connection workflow failed: %r.', error)
self._report_completion_and_cleanup(error)
return
self._attempts_remaining -= 1
_LOG.debug(
'Beginning a new AMQP connection workflow cycle; attempts '
'remaining after this: %s', self._attempts_remaining)
self._current_config_index = None
self._task_ref = self._nbio.call_later(
0 if first else self._retry_pause, self._try_next_config_async)
def _try_next_config_async(self):
"""Attempt to connect using the next Parameters config. If there are no
more configs, start a new cycle.
"""
self._task_ref = None
if self._current_config_index is None:
self._current_config_index = 0
else:
self._current_config_index += 1
if self._current_config_index >= len(self._connection_configs):
_LOG.debug('_try_next_config_async: starting a new cycle.')
self._start_new_cycle_async(first=False)
return
params = self._connection_configs[self._current_config_index]
_LOG.debug('_try_next_config_async: %r:%s', params.host, params.port)
# Begin with host address resolution
assert self._task_ref is None
self._task_ref = self._nbio.getaddrinfo(
host=params.host,
port=params.port,
socktype=self._SOCK_TYPE,
proto=self._IPPROTO,
on_done=self._on_getaddrinfo_async_done)
def _on_getaddrinfo_async_done(self, addrinfos_or_exc):
"""Handles completion callback from asynchronous `getaddrinfo()`.
:param list | BaseException addrinfos_or_exc: resolved address records
returned by `getaddrinfo()` or an exception object from failure.
"""
self._task_ref = None
if isinstance(addrinfos_or_exc, BaseException):
_LOG.error('getaddrinfo failed: %r.', addrinfos_or_exc)
self._connection_errors.append(addrinfos_or_exc)
self._start_new_cycle_async(first=False)
return
_LOG.debug('getaddrinfo returned %s records', len(addrinfos_or_exc))
self._addrinfo_iter = iter(addrinfos_or_exc)
self._try_next_resolved_address()
def _try_next_resolved_address(self):
"""Try connecting using next resolved address. If there aren't any left,
continue with next Parameters config.
"""
try:
addr_record = next(self._addrinfo_iter)
except StopIteration:
_LOG.debug(
'_try_next_resolved_address: continuing with next config.')
self._try_next_config_async()
return
_LOG.debug('Attempting to connect using address record %r', addr_record)
self._connector = self._connector_factory() # type: AMQPConnector
self._connector.start(
addr_record=addr_record,
conn_params=self._connection_configs[self._current_config_index],
on_done=self._on_connector_done)
def _on_connector_done(self, conn_or_exc):
"""Handle completion of connection attempt by `AMQPConnector`.
:param pika.connection.Connection | BaseException conn_or_exc: See
`AMQPConnector.start()` for exception details.
"""
self._connector = None
_LOG.debug('Connection attempt completed with %r', conn_or_exc)
if isinstance(conn_or_exc, BaseException):
self._connection_errors.append(conn_or_exc)
if isinstance(conn_or_exc, AMQPConnectorAborted):
assert self._state == self._STATE_ABORTING, \
'Expected _STATE_ABORTING, but got {!r}'.format(self._state)
self._report_completion_and_cleanup(
AMQPConnectionWorkflowAborted())
elif (self._until_first_amqp_attempt and
isinstance(conn_or_exc, AMQPConnectorAMQPHandshakeError)):
_LOG.debug('Ending AMQP connection workflow after first failed '
'AMQP handshake due to _until_first_amqp_attempt.')
if isinstance(conn_or_exc.exception,
pika.exceptions.ConnectionOpenAborted):
error = AMQPConnectionWorkflowAborted
else:
error = AMQPConnectionWorkflowFailed(
self._connection_errors)
self._report_completion_and_cleanup(error)
else:
self._try_next_resolved_address()
else:
# Success!
self._report_completion_and_cleanup(conn_or_exc)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,441 @@
"""Non-blocking I/O interface for pika connection adapters.
I/O interface expected by `pika.adapters.base_connection.BaseConnection`
NOTE: This API is modeled after asyncio in python3 for a couple of reasons
1. It's a sensible API
2. To make it easy to implement at least on top of the built-in asyncio
Furthermore, the API caters to the needs of pika core and lack of generalization
is intentional for the sake of reducing complexity of the implementation and
testing and lessening the maintenance burden.
"""
import abc
import pika.compat
class AbstractIOServices(pika.compat.AbstractBase):
"""Interface to I/O services required by `pika.adapters.BaseConnection` and
related utilities.
NOTE: This is not a public API. Pika users should rely on the native I/O
loop APIs (e.g., asyncio event loop, tornado ioloop, twisted reactor, etc.)
that corresponds to the chosen Connection adapter.
"""
@abc.abstractmethod
def get_native_ioloop(self):
"""Returns the native I/O loop instance, such as Twisted reactor,
asyncio's or tornado's event loop
"""
raise NotImplementedError
@abc.abstractmethod
def close(self):
"""Release IOLoop's resources.
the `close()` method is intended to be called by Pika's own test
code only after `start()` returns. After calling `close()`, no other
interaction with the closed instance of `IOLoop` should be performed.
NOTE: This method is provided for Pika's own test scripts that need to
be able to run I/O loops generically to test multiple Connection Adapter
implementations. Pika users should use the native I/O loop's API
instead.
"""
raise NotImplementedError
@abc.abstractmethod
def run(self):
"""Run the I/O loop. It will loop until requested to exit. See `stop()`.
NOTE: the outcome or restarting an instance that had been stopped is
UNDEFINED!
NOTE: This method is provided for Pika's own test scripts that need to
be able to run I/O loops generically to test multiple Connection Adapter
implementations (not all of the supported I/O Loop frameworks have
methods named start/stop). Pika users should use the native I/O loop's
API instead.
"""
raise NotImplementedError
@abc.abstractmethod
def stop(self):
"""Request exit from the ioloop. The loop is NOT guaranteed to
stop before this method returns.
NOTE: The outcome of calling `stop()` on a non-running instance is
UNDEFINED!
NOTE: This method is provided for Pika's own test scripts that need to
be able to run I/O loops generically to test multiple Connection Adapter
implementations (not all of the supported I/O Loop frameworks have
methods named start/stop). Pika users should use the native I/O loop's
API instead.
To invoke `stop()` safely from a thread other than this IOLoop's thread,
call it via `add_callback_threadsafe`; e.g.,
`ioloop.add_callback_threadsafe(ioloop.stop)`
"""
raise NotImplementedError
@abc.abstractmethod
def add_callback_threadsafe(self, callback):
"""Requests a call to the given function as soon as possible. It will be
called from this IOLoop's thread.
NOTE: This is the only thread-safe method offered by the IOLoop adapter.
All other manipulations of the IOLoop adapter and objects governed
by it must be performed from the IOLoop's thread.
NOTE: if you know that the requester is running on the same thread as
the connection it is more efficient to use the
`ioloop.call_later()` method with a delay of 0.
:param callable callback: The callback method; must be callable.
"""
raise NotImplementedError
@abc.abstractmethod
def call_later(self, delay, callback):
"""Add the callback to the IOLoop timer to be called after delay seconds
from the time of call on best-effort basis. Returns a handle to the
timeout.
If two are scheduled for the same time, it's undefined which one will
be called first.
:param float delay: The number of seconds to wait to call callback
:param callable callback: The callback method
:returns: A handle that can be used to cancel the request.
:rtype: AbstractTimerReference
"""
raise NotImplementedError
@abc.abstractmethod
def getaddrinfo(self,
host,
port,
on_done,
family=0,
socktype=0,
proto=0,
flags=0):
"""Perform the equivalent of `socket.getaddrinfo()` asynchronously.
See `socket.getaddrinfo()` for the standard args.
:param callable on_done: user callback that takes the return value of
`socket.getaddrinfo()` upon successful completion or exception upon
failure (check for `BaseException`) as its only arg. It will not be
called if the operation was cancelled.
:rtype: AbstractIOReference
"""
raise NotImplementedError
@abc.abstractmethod
def connect_socket(self, sock, resolved_addr, on_done):
"""Perform the equivalent of `socket.connect()` on a previously-resolved
address asynchronously.
IMPLEMENTATION NOTE: Pika's connection logic resolves the addresses
prior to making socket connections, so we don't need to burden the
implementations of this method with the extra logic of asynchronous
DNS resolution. Implementations can use `socket.inet_pton()` to
verify the address.
:param socket.socket sock: non-blocking socket that needs to be
connected via `socket.socket.connect()`
:param tuple resolved_addr: resolved destination address/port two-tuple
as per `socket.socket.connect()`, except that the first element must
be an actual IP address that's consistent with the given socket's
address family.
:param callable on_done: user callback that takes None upon successful
completion or exception (check for `BaseException`) upon error as
its only arg. It will not be called if the operation was cancelled.
:rtype: AbstractIOReference
:raises ValueError: if host portion of `resolved_addr` is not an IP
address or is inconsistent with the socket's address family as
validated via `socket.inet_pton()`
"""
raise NotImplementedError
@abc.abstractmethod
def create_streaming_connection(self,
protocol_factory,
sock,
on_done,
ssl_context=None,
server_hostname=None):
"""Perform SSL session establishment, if requested, on the already-
connected socket and link the streaming transport/protocol pair.
NOTE: This method takes ownership of the socket.
:param callable protocol_factory: called without args, returns an
instance with the `AbstractStreamProtocol` interface. The protocol's
`connection_made(transport)` method will be called to link it to
the transport after remaining connection activity (e.g., SSL session
establishment), if any, is completed successfully.
:param socket.socket sock: Already-connected, non-blocking
`socket.SOCK_STREAM` socket to be used by the transport. We take
ownership of this socket.
:param callable on_done: User callback
`on_done(BaseException | (transport, protocol))` to be notified when
the asynchronous operation completes. An exception arg indicates
failure (check for `BaseException`); otherwise the two-tuple will
contain the linked transport/protocol pair having
AbstractStreamTransport and AbstractStreamProtocol interfaces
respectively.
:param None | ssl.SSLContext ssl_context: if None, this will proceed as
a plaintext connection; otherwise, if not None, SSL session
establishment will be performed prior to linking the transport and
protocol.
:param str | None server_hostname: For use during SSL session
establishment to match against the target server's certificate. The
value `None` disables this check (which is a huge security risk)
:rtype: AbstractIOReference
"""
raise NotImplementedError
class AbstractFileDescriptorServices(pika.compat.AbstractBase):
"""Interface definition of common non-blocking file descriptor services
required by some utility implementations.
NOTE: This is not a public API. Pika users should rely on the native I/O
loop APIs (e.g., asyncio event loop, tornado ioloop, twisted reactor, etc.)
that corresponds to the chosen Connection adapter.
"""
@abc.abstractmethod
def set_reader(self, fd, on_readable):
"""Call the given callback when the file descriptor is readable.
Replace prior reader, if any, for the given file descriptor.
:param fd: file descriptor
:param callable on_readable: a callback taking no args to be notified
when fd becomes readable.
"""
raise NotImplementedError
@abc.abstractmethod
def remove_reader(self, fd):
"""Stop watching the given file descriptor for readability
:param fd: file descriptor
:returns: True if reader was removed; False if none was registered.
:rtype: bool
"""
raise NotImplementedError
@abc.abstractmethod
def set_writer(self, fd, on_writable):
"""Call the given callback whenever the file descriptor is writable.
Replace prior writer callback, if any, for the given file descriptor.
IMPLEMENTATION NOTE: For portability, implementations of
`set_writable()` should also watch for indication of error on the
socket and treat it as equivalent to the writable indication (e.g.,
also adding the socket to the `exceptfds` arg of `socket.select()`
and calling the `on_writable` callback if `select.select()`
indicates that the socket is in error state). Specifically, Windows
(unlike POSIX) only indicates error on the socket (but not writable)
when connection establishment fails.
:param fd: file descriptor
:param callable on_writable: a callback taking no args to be notified
when fd becomes writable.
"""
raise NotImplementedError
@abc.abstractmethod
def remove_writer(self, fd):
"""Stop watching the given file descriptor for writability
:param fd: file descriptor
:returns: True if reader was removed; False if none was registered.
:rtype: bool
"""
raise NotImplementedError
class AbstractTimerReference(pika.compat.AbstractBase):
"""Reference to asynchronous operation"""
@abc.abstractmethod
def cancel(self):
"""Cancel callback. If already cancelled, has no affect.
"""
raise NotImplementedError
class AbstractIOReference(pika.compat.AbstractBase):
"""Reference to asynchronous I/O operation"""
@abc.abstractmethod
def cancel(self):
"""Cancel pending operation
:returns: False if was already done or cancelled; True otherwise
:rtype: bool
"""
raise NotImplementedError
class AbstractStreamProtocol(pika.compat.AbstractBase):
"""Stream protocol interface. It's compatible with a subset of
`asyncio.protocols.Protocol` for compatibility with asyncio-based
`AbstractIOServices` implementation.
"""
@abc.abstractmethod
def connection_made(self, transport):
"""Introduces transport to protocol after transport is connected.
:param AbstractStreamTransport transport:
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def connection_lost(self, error):
"""Called upon loss or closing of connection.
NOTE: `connection_made()` and `connection_lost()` are each called just
once and in that order. All other callbacks are called between them.
:param BaseException | None error: An exception (check for
`BaseException`) indicates connection failure. None indicates that
connection was closed on this side, such as when it's aborted or
when `AbstractStreamProtocol.eof_received()` returns a result that
doesn't evaluate to True.
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def eof_received(self):
"""Called after the remote peer shuts its write end of the connection.
:returns: A falsy value (including None) will cause the transport to
close itself, resulting in an eventual `connection_lost()` call
from the transport. If a truthy value is returned, it will be the
protocol's responsibility to close/abort the transport.
:rtype: falsy|truthy
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def data_received(self, data):
"""Called to deliver incoming data to the protocol.
:param data: Non-empty data bytes.
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
# pylint: disable=W0511
# TODO Undecided whether we need write flow-control yet, although it seems
# like a good idea.
# @abc.abstractmethod
# def pause_writing(self):
# """Called when the transport's write buffer size becomes greater than or
# equal to the transport's high-water mark. It won't be called again until
# the transport's write buffer gets back to its low-water mark and then
# returns to/past the hight-water mark again.
# """
# raise NotImplementedError
#
# @abc.abstractmethod
# def resume_writing(self):
# """Called when the transport's write buffer size becomes less than or
# equal to the transport's low-water mark.
# """
# raise NotImplementedError
class AbstractStreamTransport(pika.compat.AbstractBase):
"""Stream transport interface. It's compatible with a subset of
`asyncio.transports.Transport` for compatibility with asyncio-based
`AbstractIOServices` implementation.
"""
@abc.abstractmethod
def abort(self):
"""Close connection abruptly without waiting for pending I/O to
complete. Will invoke the corresponding protocol's `connection_lost()`
method asynchronously (not in context of the abort() call).
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def get_protocol(self):
"""Return the protocol linked to this transport.
:rtype: AbstractStreamProtocol
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def write(self, data):
"""Buffer the given data until it can be sent asynchronously.
:param bytes data:
:raises ValueError: if called with empty data
:raises Exception: Exception-based exception on error
"""
raise NotImplementedError
@abc.abstractmethod
def get_write_buffer_size(self):
"""
:returns: Current size of output data buffered by the transport
:rtype: int
"""
raise NotImplementedError
# pylint: disable=W0511
# TODO Udecided whether we need write flow-control yet, although it seems
# like a good idea.
# @abc.abstractmethod
# def set_write_buffer_limits(self, high, low):
# """Set thresholds for calling the protocol's `pause_writing()`
# and `resume_writing()` methods. `low` must be less than or equal to
# `high`.
#
# NOTE The unintuitive order of the args is preserved to match the
# corresponding method in `asyncio.WriteTransport`. I would expect `low`
# to be the first arg, especially since
# `asyncio.WriteTransport.get_write_buffer_limits()` returns them in the
# opposite order. This seems error-prone.
#
# See `asyncio.WriteTransport.get_write_buffer_limits()` for more details
# about the args.
#
# :param int high: non-negative high-water mark.
# :param int low: non-negative low-water mark.
# """
# raise NotImplementedError

View File

@@ -0,0 +1,600 @@
"""
Implementation of `nbio_interface.AbstractIOServices` on top of a
selector-based I/O loop, such as tornado's and our home-grown
select_connection's I/O loops.
"""
import abc
import logging
import socket
import threading
from pika.adapters.utils import nbio_interface, io_services_utils
from pika.adapters.utils.io_services_utils import (check_callback_arg,
check_fd_arg)
LOGGER = logging.getLogger(__name__)
class AbstractSelectorIOLoop(object):
"""Selector-based I/O loop interface expected by
`selector_ioloop_adapter.SelectorIOServicesAdapter`
NOTE: this interface follows the corresponding methods and attributes
of `tornado.ioloop.IOLoop` in order to avoid additional adapter layering
when wrapping tornado's IOLoop.
"""
@property
@abc.abstractmethod
def READ(self): # pylint: disable=C0103
"""The value of the I/O loop's READ flag; READ/WRITE/ERROR may be used
with bitwise operators as expected.
Implementation note: the implementations can simply replace these
READ/WRITE/ERROR properties with class-level attributes
"""
@property
@abc.abstractmethod
def WRITE(self): # pylint: disable=C0103
"""The value of the I/O loop's WRITE flag; READ/WRITE/ERROR may be used
with bitwise operators as expected
"""
@property
@abc.abstractmethod
def ERROR(self): # pylint: disable=C0103
"""The value of the I/O loop's ERROR flag; READ/WRITE/ERROR may be used
with bitwise operators as expected
"""
@abc.abstractmethod
def close(self):
"""Release IOLoop's resources.
the `close()` method is intended to be called by the application or test
code only after `start()` returns. After calling `close()`, no other
interaction with the closed instance of `IOLoop` should be performed.
"""
@abc.abstractmethod
def start(self):
"""Run the I/O loop. It will loop until requested to exit. See `stop()`.
"""
@abc.abstractmethod
def stop(self):
"""Request exit from the ioloop. The loop is NOT guaranteed to
stop before this method returns.
To invoke `stop()` safely from a thread other than this IOLoop's thread,
call it via `add_callback_threadsafe`; e.g.,
`ioloop.add_callback(ioloop.stop)`
"""
@abc.abstractmethod
def call_later(self, delay, callback):
"""Add the callback to the IOLoop timer to be called after delay seconds
from the time of call on best-effort basis. Returns a handle to the
timeout.
:param float delay: The number of seconds to wait to call callback
:param callable callback: The callback method
:returns: handle to the created timeout that may be passed to
`remove_timeout()`
:rtype: object
"""
@abc.abstractmethod
def remove_timeout(self, timeout_handle):
"""Remove a timeout
:param timeout_handle: Handle of timeout to remove
"""
@abc.abstractmethod
def add_callback(self, callback):
"""Requests a call to the given function as soon as possible in the
context of this IOLoop's thread.
NOTE: This is the only thread-safe method in IOLoop. All other
manipulations of IOLoop must be performed from the IOLoop's thread.
For example, a thread may request a call to the `stop` method of an
ioloop that is running in a different thread via
`ioloop.add_callback_threadsafe(ioloop.stop)`
:param callable callback: The callback method
"""
@abc.abstractmethod
def add_handler(self, fd, handler, events):
"""Start watching the given file descriptor for events
:param int fd: The file descriptor
:param callable handler: When requested event(s) occur,
`handler(fd, events)` will be called.
:param int events: The event mask using READ, WRITE, ERROR.
"""
@abc.abstractmethod
def update_handler(self, fd, events):
"""Changes the events we watch for
:param int fd: The file descriptor
:param int events: The event mask using READ, WRITE, ERROR
"""
@abc.abstractmethod
def remove_handler(self, fd):
"""Stop watching the given file descriptor for events
:param int fd: The file descriptor
"""
class SelectorIOServicesAdapter(io_services_utils.SocketConnectionMixin,
io_services_utils.StreamingConnectionMixin,
nbio_interface.AbstractIOServices,
nbio_interface.AbstractFileDescriptorServices):
"""Implements the
:py:class:`.nbio_interface.AbstractIOServices` interface
on top of selector-style native loop having the
:py:class:`AbstractSelectorIOLoop` interface, such as
:py:class:`pika.selection_connection.IOLoop` and :py:class:`tornado.IOLoop`.
NOTE:
:py:class:`.nbio_interface.AbstractFileDescriptorServices`
interface is only required by the mixins.
"""
def __init__(self, native_loop):
"""
:param AbstractSelectorIOLoop native_loop: An instance compatible with
the `AbstractSelectorIOLoop` interface, but not necessarily derived
from it.
"""
self._loop = native_loop
# Active watchers: maps file descriptors to `_FileDescriptorCallbacks`
self._watchers = dict()
# Native loop-specific event masks of interest
self._readable_mask = self._loop.READ
# NOTE: tying ERROR to WRITE is particularly handy for Windows, whose
# `select.select()` differs from Posix by reporting
# connection-establishment failure only through exceptfds (ERROR event),
# while the typical application workflow is to wait for the socket to
# become writable when waiting for socket connection to be established.
self._writable_mask = self._loop.WRITE | self._loop.ERROR
def get_native_ioloop(self):
"""Implement
:py:meth:`.nbio_interface.AbstractIOServices.get_native_ioloop()`.
"""
return self._loop
def close(self):
"""Implement :py:meth:`.nbio_interface.AbstractIOServices.close()`.
"""
self._loop.close()
def run(self):
"""Implement :py:meth:`.nbio_interface.AbstractIOServices.run()`.
"""
self._loop.start()
def stop(self):
"""Implement :py:meth:`.nbio_interface.AbstractIOServices.stop()`.
"""
self._loop.stop()
def add_callback_threadsafe(self, callback):
"""Implement
:py:meth:`.nbio_interface.AbstractIOServices.add_callback_threadsafe()`.
"""
self._loop.add_callback(callback)
def call_later(self, delay, callback):
"""Implement :py:meth:`.nbio_interface.AbstractIOServices.call_later()`.
"""
return _TimerHandle(self._loop.call_later(delay, callback), self._loop)
def getaddrinfo(self,
host,
port,
on_done,
family=0,
socktype=0,
proto=0,
flags=0):
"""Implement :py:meth:`.nbio_interface.AbstractIOServices.getaddrinfo()`.
"""
return _SelectorIOLoopIOHandle(
_AddressResolver(
native_loop=self._loop,
host=host,
port=port,
family=family,
socktype=socktype,
proto=proto,
flags=flags,
on_done=on_done).start())
def set_reader(self, fd, on_readable):
"""Implement
:py:meth:`.nbio_interface.AbstractFileDescriptorServices.set_reader()`.
"""
LOGGER.debug('SelectorIOServicesAdapter.set_reader(%s, %r)', fd,
on_readable)
check_fd_arg(fd)
check_callback_arg(on_readable, 'on_readable')
try:
callbacks = self._watchers[fd]
except KeyError:
self._loop.add_handler(fd, self._on_reader_writer_fd_events,
self._readable_mask)
self._watchers[fd] = _FileDescriptorCallbacks(reader=on_readable)
LOGGER.debug('set_reader(%s, _) added handler Rd', fd)
else:
if callbacks.reader is None:
assert callbacks.writer is not None
self._loop.update_handler(
fd, self._readable_mask | self._writable_mask)
LOGGER.debug('set_reader(%s, _) updated handler RdWr', fd)
else:
LOGGER.debug('set_reader(%s, _) replacing reader', fd)
callbacks.reader = on_readable
def remove_reader(self, fd):
"""Implement
:py:meth:`.nbio_interface.AbstractFileDescriptorServices.remove_reader()`.
"""
LOGGER.debug('SelectorIOServicesAdapter.remove_reader(%s)', fd)
check_fd_arg(fd)
try:
callbacks = self._watchers[fd]
except KeyError:
LOGGER.debug('remove_reader(%s) neither was set', fd)
return False
if callbacks.reader is None:
assert callbacks.writer is not None
LOGGER.debug('remove_reader(%s) reader wasn\'t set Wr', fd)
return False
callbacks.reader = None
if callbacks.writer is None:
del self._watchers[fd]
self._loop.remove_handler(fd)
LOGGER.debug('remove_reader(%s) removed handler', fd)
else:
self._loop.update_handler(fd, self._writable_mask)
LOGGER.debug('remove_reader(%s) updated handler Wr', fd)
return True
def set_writer(self, fd, on_writable):
"""Implement
:py:meth:`.nbio_interface.AbstractFileDescriptorServices.set_writer()`.
"""
LOGGER.debug('SelectorIOServicesAdapter.set_writer(%s, %r)', fd,
on_writable)
check_fd_arg(fd)
check_callback_arg(on_writable, 'on_writable')
try:
callbacks = self._watchers[fd]
except KeyError:
self._loop.add_handler(fd, self._on_reader_writer_fd_events,
self._writable_mask)
self._watchers[fd] = _FileDescriptorCallbacks(writer=on_writable)
LOGGER.debug('set_writer(%s, _) added handler Wr', fd)
else:
if callbacks.writer is None:
assert callbacks.reader is not None
# NOTE: Set the writer func before setting the mask!
# Otherwise a race condition can occur where ioloop tries to
# call writer when it is still None.
callbacks.writer = on_writable
self._loop.update_handler(
fd, self._readable_mask | self._writable_mask)
LOGGER.debug('set_writer(%s, _) updated handler RdWr', fd)
else:
LOGGER.debug('set_writer(%s, _) replacing writer', fd)
callbacks.writer = on_writable
def remove_writer(self, fd):
"""Implement
:py:meth:`.nbio_interface.AbstractFileDescriptorServices.remove_writer()`.
"""
LOGGER.debug('SelectorIOServicesAdapter.remove_writer(%s)', fd)
check_fd_arg(fd)
try:
callbacks = self._watchers[fd]
except KeyError:
LOGGER.debug('remove_writer(%s) neither was set.', fd)
return False
if callbacks.writer is None:
assert callbacks.reader is not None
LOGGER.debug('remove_writer(%s) writer wasn\'t set Rd', fd)
return False
callbacks.writer = None
if callbacks.reader is None:
del self._watchers[fd]
self._loop.remove_handler(fd)
LOGGER.debug('remove_writer(%s) removed handler', fd)
else:
self._loop.update_handler(fd, self._readable_mask)
LOGGER.debug('remove_writer(%s) updated handler Rd', fd)
return True
def _on_reader_writer_fd_events(self, fd, events):
"""Handle indicated file descriptor events requested via `set_reader()`
and `set_writer()`.
:param fd: file descriptor
:param events: event mask using native loop's READ/WRITE/ERROR. NOTE:
depending on the underlying poller mechanism, ERROR may be indicated
upon certain file description state even though we don't request it.
We ignore ERROR here since `set_reader()`/`set_writer()` don't
request for it.
"""
callbacks = self._watchers[fd]
if events & self._readable_mask and callbacks.reader is None:
# NOTE: we check for consistency here ahead of the writer callback
# because the writer callback, if any, can change the events being
# watched
LOGGER.warning(
'READ indicated on fd=%s, but reader callback is None; '
'events=%s', fd, bin(events))
if events & self._writable_mask:
if callbacks.writer is not None:
callbacks.writer()
else:
LOGGER.warning(
'WRITE indicated on fd=%s, but writer callback is None; '
'events=%s', fd, bin(events))
if events & self._readable_mask:
if callbacks.reader is not None:
callbacks.reader()
else:
# Reader callback might have been removed in the scope of writer
# callback.
pass
class _FileDescriptorCallbacks(object):
"""Holds reader and writer callbacks for a file descriptor"""
__slots__ = ('reader', 'writer')
def __init__(self, reader=None, writer=None):
self.reader = reader
self.writer = writer
class _TimerHandle(nbio_interface.AbstractTimerReference):
"""This module's adaptation of `nbio_interface.AbstractTimerReference`.
"""
def __init__(self, handle, loop):
"""
:param opaque handle: timer handle from the underlying loop
implementation that may be passed to its `remove_timeout()` method
:param AbstractSelectorIOLoop loop: the I/O loop instance that created
the timeout.
"""
self._handle = handle
self._loop = loop
def cancel(self):
if self._loop is not None:
self._loop.remove_timeout(self._handle)
self._handle = None
self._loop = None
class _SelectorIOLoopIOHandle(nbio_interface.AbstractIOReference):
"""This module's adaptation of `nbio_interface.AbstractIOReference`
"""
def __init__(self, subject):
"""
:param subject: subject of the reference containing a `cancel()` method
"""
self._cancel = subject.cancel
def cancel(self):
"""Cancel pending operation
:returns: False if was already done or cancelled; True otherwise
:rtype: bool
"""
return self._cancel()
class _AddressResolver(object):
"""Performs getaddrinfo asynchronously using a thread, then reports result
via callback from the given I/O loop.
NOTE: at this stage, we're using a thread per request, which may prove
inefficient and even prohibitive if the app performs many of these
operations concurrently.
"""
NOT_STARTED = 0
ACTIVE = 1
CANCELED = 2
COMPLETED = 3
def __init__(self, native_loop, host, port, family, socktype, proto, flags,
on_done):
"""
:param AbstractSelectorIOLoop native_loop:
:param host: `see socket.getaddrinfo()`
:param port: `see socket.getaddrinfo()`
:param family: `see socket.getaddrinfo()`
:param socktype: `see socket.getaddrinfo()`
:param proto: `see socket.getaddrinfo()`
:param flags: `see socket.getaddrinfo()`
:param on_done: on_done(records|BaseException) callback for reporting
result from the given I/O loop. The single arg will be either an
exception object (check for `BaseException`) in case of failure or
the result returned by `socket.getaddrinfo()`.
"""
check_callback_arg(on_done, 'on_done')
self._state = self.NOT_STARTED
self._result = None
self._loop = native_loop
self._host = host
self._port = port
self._family = family
self._socktype = socktype
self._proto = proto
self._flags = flags
self._on_done = on_done
self._mutex = threading.Lock()
self._threading_timer = None
def _cleanup(self):
"""Release resources
"""
self._loop = None
self._threading_timer = None
self._on_done = None
def start(self):
"""Start asynchronous DNS lookup.
:rtype: nbio_interface.AbstractIOReference
"""
assert self._state == self.NOT_STARTED, self._state
self._state = self.ACTIVE
self._threading_timer = threading.Timer(0, self._resolve)
self._threading_timer.start()
return _SelectorIOLoopIOHandle(self)
def cancel(self):
"""Cancel the pending resolver
:returns: False if was already done or cancelled; True otherwise
:rtype: bool
"""
# Try to cancel, but no guarantees
with self._mutex:
if self._state == self.ACTIVE:
LOGGER.debug('Canceling resolver for %s:%s', self._host,
self._port)
self._state = self.CANCELED
# Attempt to cancel, but not guaranteed
self._threading_timer.cancel()
self._cleanup()
return True
else:
LOGGER.debug(
'Ignoring _AddressResolver cancel request when not ACTIVE; '
'(%s:%s); state=%s', self._host, self._port, self._state)
return False
def _resolve(self):
"""Call `socket.getaddrinfo()` and return result via user's callback
function on the given I/O loop
"""
try:
# NOTE: on python 2.x, can't pass keyword args to getaddrinfo()
result = socket.getaddrinfo(self._host, self._port, self._family,
self._socktype, self._proto,
self._flags)
except Exception as exc: # pylint: disable=W0703
LOGGER.error('Address resolution failed: %r', exc)
result = exc
self._result = result
# Schedule result to be returned to user via user's event loop
with self._mutex:
if self._state == self.ACTIVE:
self._loop.add_callback(self._dispatch_result)
else:
LOGGER.debug(
'Asynchronous getaddrinfo cancellation detected; '
'in thread; host=%r', self._host)
def _dispatch_result(self):
"""This is called from the user's I/O loop to pass the result to the
user via the user's on_done callback
"""
if self._state == self.ACTIVE:
self._state = self.COMPLETED
try:
LOGGER.debug(
'Invoking asynchronous getaddrinfo() completion callback; '
'host=%r', self._host)
self._on_done(self._result)
finally:
self._cleanup()
else:
LOGGER.debug(
'Asynchronous getaddrinfo cancellation detected; '
'in I/O loop context; host=%r', self._host)