bpo-32356: idempotent pause_/resume_reading; new is_reading method. (#4914)

This commit is contained in:
Yury Selivanov 2017-12-18 17:03:23 -05:00 committed by GitHub
parent 2d8f06382e
commit d757aaf9dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 24 deletions

View file

@ -152,21 +152,20 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
self._paused = False
self._loop.call_soon(self._loop_reading)
def is_reading(self):
return not self._paused and not self._closing
def pause_reading(self):
if self._closing:
raise RuntimeError('Cannot pause_reading() when closing')
if self._paused:
raise RuntimeError('Already paused')
if self._closing or self._paused:
return
self._paused = True
if self._loop.get_debug():
logger.debug("%r pauses reading", self)
def resume_reading(self):
if not self._paused:
raise RuntimeError('Not paused')
self._paused = False
if self._closing:
if self._closing or not self._paused:
return
self._paused = False
self._loop.call_soon(self._loop_reading, self._read_fut)
if self._loop.get_debug():
logger.debug("%r resumes reading", self)

View file

@ -702,22 +702,21 @@ class _SelectorSocketTransport(_SelectorTransport):
self._loop.call_soon(futures._set_result_unless_cancelled,
waiter, None)
def is_reading(self):
return not self._paused and not self._closing
def pause_reading(self):
if self._closing:
raise RuntimeError('Cannot pause_reading() when closing')
if self._paused:
raise RuntimeError('Already paused')
if self._closing or self._paused:
return
self._paused = True
self._loop._remove_reader(self._sock_fd)
if self._loop.get_debug():
logger.debug("%r pauses reading", self)
def resume_reading(self):
if not self._paused:
raise RuntimeError('Not paused')
self._paused = False
if self._closing:
if self._closing or not self._paused:
return
self._paused = False
self._loop._add_reader(self._sock_fd, self._read_ready)
if self._loop.get_debug():
logger.debug("%r resumes reading", self)

View file

@ -317,6 +317,12 @@ class _SSLProtocolTransport(transports._FlowControlMixin,
source=self)
self.close()
def is_reading(self):
tr = self._ssl_protocol._transport
if tr is None:
raise RuntimeError('SSL transport has not been initialized yet')
return tr.is_reading()
def pause_reading(self):
"""Pause the receiving end.

View file

@ -44,6 +44,10 @@ class BaseTransport:
class ReadTransport(BaseTransport):
"""Interface for read-only transports."""
def is_reading(self):
"""Return True if the transport is receiving."""
raise NotImplementedError
def pause_reading(self):
"""Pause the receiving end.