def __init__(self, owner: "channels.Channel", loop: typing.Optional[asyncio.AbstractEventLoop], logger: typing.Optional[logging.Logger], transport_policy: typing.Optional[TransportPolicy], timeout: float, outgoing_window_size: int, incoming_window_size: int) -> None: assert timeout >= 0.0, repr(timeout) assert outgoing_window_size >= 0, repr(outgoing_window_size) assert incoming_window_size >= 0, repr(incoming_window_size) self._owner = owner self._transport = Transport(loop, logger, transport_policy) self._timeout = timeout self._outgoing_window_size = outgoing_window_size self._incoming_window_size = incoming_window_size self._state = _ChannelState.CLOSED self._opening: asyncio.Future[None] = utils.Future( loop=self.get_loop()) self._id = b"" self._next_sequence_number = 0 self._pending_method_calls1: Deque[_MethodCall] = Deque( _MIN_CHANNEL_WINDOW_SIZE, self.get_loop()) self._pending_method_calls2: typing.Dict[int, _MethodCall] = {} self._request_count = 0 self._service_handlers: typing.Dict[bytes, ServiceHandler] = {} self._calling_methods1: typing.Dict[int, asyncio.Task[None]] = {} self._calling_methods2: typing.Set[asyncio.Task[None]] = set() self._sending_response: asyncio.Future[typing.Optional[_MethodCall]] = utils\ .make_done_future(self.get_loop())
async def start(self) -> None: assert not self.is_running() if self._starting is not None: await utils.shield(self._starting) return self._starting = utils.Future(loop=self._loop) running = self._loop.create_task(self._run()) try: await utils.delay_cancellation(self._starting) except Exception: running.cancel() await utils.delay_cancellation(running) raise finally: self._starting = None self._running = running
async def start(self) -> None: assert not self.is_running() if self._starting is not None: await utils.shield(self._starting) return self._starting = utils.Future(loop=self.get_loop()) running = self.get_loop().create_task(self._run()) try: await utils.delay_cancellation( self._impl.wait_for_opened_unsafely()) except Exception: running.cancel() await utils.delay_cancellation(running) raise finally: self._starting.set_result(None) self._starting = None self._running = running
async def start(self) -> None: assert not self.is_running() if self._starting is not None: await utils.shield(self._starting) return self._starting = utils.Future(loop=self.get_loop()) running = self.get_loop().create_task(self._run()) session_listener = self._session.add_listener() try: await utils.delay_cancellation(session_listener.get_state_change()) except Exception: running.cancel() await utils.delay_cancellation(running) raise else: self._session.remove_listener(session_listener) finally: self._starting.set_result(None) self._starting = None self._running = running
def __init__(self, type_: WatcherType, path: str, loop: asyncio.AbstractEventLoop) -> None: self._type = type_ self._path = path self._event: asyncio.Future[protocol.WatcherEventType] = utils.Future( loop=loop)
def _set_state(self, new_state: _ChannelState) -> None: old_state = self._state error_class: typing.Optional[typing.Type[errors.Error]] = None if old_state is _ChannelState.CONNECTING: if new_state is old_state: return if new_state is _ChannelState.CONNECTED: pass elif new_state is _ChannelState.CLOSED: error_class = errors.ChannelTimedOutError else: assert False, repr(new_state) elif old_state is _ChannelState.CONNECTED: if new_state is _ChannelState.CONNECTING: error_class = errors.ChannelBrokenError elif new_state is _ChannelState.CLOSED: error_class = errors.ChannelTimedOutError else: assert False, repr(new_state) elif old_state is _ChannelState.ACCEPTING: if new_state is _ChannelState.ACCEPTED: pass elif new_state is _ChannelState.CLOSED: error_class = errors.ChannelTimedOutError else: assert False, repr(new_state) elif old_state is _ChannelState.ACCEPTED: assert new_state is _ChannelState.CLOSED, repr(new_state) error_class = errors.ChannelTimedOutError elif old_state is _ChannelState.CLOSED: assert new_state in (_ChannelState.CONNECTING, _ChannelState.ACCEPTING), repr(new_state) else: assert False, repr(old_state) if error_class is not None: need_retry = error_class is errors.ChannelBrokenError method_call: typing.Optional[_MethodCall] if new_state is _ChannelState.CLOSED: error_class2 = errors.ChannelTimedOutError if not self._transport.is_closed(): self._transport.close() while True: method_call = self._pending_method_calls1.try_remove_head() if method_call is None: break if method_call.response_data is None or method_call.response_data.cancelled( ): continue error_message = "method_call: {!r}".format(method_call) method_call.response_data.set_exception( error_class2(error_message)) self._pending_method_calls1.close(error_class2) for method_call in self._pending_method_calls2.values(): if method_call.response_data is None or method_call.response_data.cancelled( ): continue error_message = "method_call: {!r}".format(method_call) if need_retry and method_call.auto_retry: method_call.response_data.set_exception( error_class2(error_message)) else: method_call.response_data.set_exception( error_class(error_message)) self._pending_method_calls2.clear() else: self._pending_method_calls1.commit_item_removals( len(self._pending_method_calls2)) for method_call in self._pending_method_calls2.values(): if method_call.response_data is None or method_call.response_data.cancelled( ): continue if need_retry and method_call.auto_retry: self._pending_method_calls1.try_insert_tail( method_call) else: error_message = "method_call: {!r}".format(method_call) method_call.response_data.set_exception( error_class(error_message)) self._pending_method_calls2.clear() for calling_method in self._calling_methods1.values(): calling_method.cancel() self._calling_methods2.update(self._calling_methods1.values()) self._calling_methods1.clear() self._state = new_state self.get_logger().info( "channel state change: channel_id={!r} channel_state={!r}".format( self._id.hex(), self._state)) if old_state is _ChannelState.CLOSED: self._opening.set_result(None) elif new_state is _ChannelState.CLOSED: self._opening = utils.Future(loop=self.get_loop())