Skip to content

Commit f4d505c

Browse files
author
taras
committed
Attempt to send data immediately if all write buffers are empty
1 parent 4083a94 commit f4d505c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

uvloop/handles/stream.pyx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,48 @@ cdef class UVStream(UVBaseTransport):
680680
if self._conn_lost:
681681
self._conn_lost += 1
682682
return
683+
684+
cdef int sent
685+
686+
if (self._buffer_size == 0 and
687+
(<uv.uv_stream_t*>self._handle).write_queue_size == 0):
688+
689+
sent_ = self._try_write(buf)
690+
691+
if sent_ is None:
692+
# A `self._fatal_error` was called.
693+
# It might not raise an exception under some
694+
# conditions.
695+
if not self._closing:
696+
raise RuntimeError('stream is open after '
697+
'UVStream._try_write returned None')
698+
699+
return
700+
701+
sent = sent_
702+
703+
if sent == 0:
704+
# All data was successfully written.
705+
# on_write will call "maybe_resume_protocol".
706+
self._on_write()
707+
return
708+
709+
if sent > 0:
710+
if UVLOOP_DEBUG:
711+
if sent == len(buf):
712+
raise RuntimeError('_try_write sent all data and '
713+
'returned non-zero')
714+
715+
if PyBytes_CheckExact(buf):
716+
# Cast bytes to memoryview to avoid copying
717+
# data that wasn't sent.
718+
buf = memoryview(buf)
719+
buf = buf[sent:]
720+
721+
# At this point it's either data was sent partially,
722+
# or an EAGAIN has happened.
723+
# buffer remaining data and send it later
724+
683725
self._buffer_write(buf)
684726
self._initiate_write()
685727

0 commit comments

Comments
 (0)