Skip to content

Commit

Permalink
do not use private attributes in SendfilePayloadWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdarcet committed Jul 24, 2017
1 parent 65a70f2 commit 4337bd1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
24 changes: 16 additions & 8 deletions aiohttp/http_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,28 @@ def __init__(self, stream, loop, acquire=True):
self._stream.acquire(self)

def set_transport(self, transport):
assert self._transport is None and self._buffer is not None
self._transport = transport

for b in self._buffer:
transport.write(b)
self._transport, self._buffer = transport, None
if self._buffer is not None:
for b in self._buffer:
transport.write(b)
self._buffer = None

if self._drain_waiter is not None:
waiter, self._drain_waiter = self._drain_waiter, None
if not waiter.done():
waiter.set_result(None)

@asyncio.coroutine
def get_transport(self):
if self._transport is None:
if self._drain_waiter is None:
self._drain_waiter = create_future(self.loop)
yield from self._drain_waiter

assert self._transport is not None
return self._transport

@property
def tcp_nodelay(self):
return self._stream.tcp_nodelay
Expand Down Expand Up @@ -272,7 +283,4 @@ def drain(self):
yield from self._stream.drain()
else:
# wait for transport
if self._drain_waiter is None:
self._drain_waiter = create_future(self.loop)

yield from self._drain_waiter
yield from self.get_transport()
30 changes: 12 additions & 18 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

class SendfilePayloadWriter(PayloadWriter):

def set_transport(self, transport):
self._transport = transport

if self._drain_waiter is not None:
waiter, self._drain_maiter = self._drain_maiter, None
if not waiter.done():
waiter.set_result(None)
def __init__(self, *args, **kwargs):
self.__buffer = []
super().__init__(*args, **kwargs)

def _write(self, chunk):
# we overwrite PayloadWriter._write, so nothing can be appended to
# _buffer, and nothing is written to the transport directly by the
# parent class
self.output_size += len(chunk)
self._buffer.append(chunk)
self.__buffer.append(chunk)

def _sendfile_cb(self, fut, out_fd, in_fd,
offset, count, loop, registered):
Expand All @@ -57,33 +56,28 @@ def _sendfile_cb(self, fut, out_fd, in_fd,

@asyncio.coroutine
def sendfile(self, fobj, count):
if self._transport is None:
if self._drain_waiter is None:
self._drain_waiter = create_future(self.loop)

yield from self._drain_waiter
transport = yield from self.get_transport()

out_socket = self._transport.get_extra_info("socket").dup()
out_socket = transport.get_extra_info('socket').dup()
out_socket.setblocking(False)
out_fd = out_socket.fileno()
in_fd = fobj.fileno()
offset = fobj.tell()

loop = self.loop
try:
yield from loop.sock_sendall(out_socket, b''.join(self._buffer))
yield from loop.sock_sendall(out_socket, b''.join(self.__buffer))
fut = create_future(loop)
self._sendfile_cb(fut, out_fd, in_fd, offset, count, loop, False)
yield from fut
except:
server_logger.debug('Socket error')
self._transport.close()
transport.close()
finally:
out_socket.close()

self.output_size += count
self._transport = None
self._stream.release()
yield from super().write_eof()

@asyncio.coroutine
def write_eof(self, chunk=b''):
Expand Down

0 comments on commit 4337bd1

Please sign in to comment.