From d114c5e17bc4311776c77d3dc67eee642dc93795 Mon Sep 17 00:00:00 2001 From: Jaymz Julian Date: Tue, 12 Dec 2023 14:29:47 -0800 Subject: [PATCH] Adjust the buffer write function to clear the buffer once, rather than piece by piece. This avoids a case where a large write (in our case, around 70mbytes) will consume 100% of CPU. This takes a webrick GET request via SSL from around 200kbyts/sec and consuming 100% of a core, to line speed on gigabit ethernet and 6% cpu utlization. --- lib/openssl/buffering.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 9570f14f3..50308e9b9 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -345,13 +345,18 @@ def do_write(s) @wbuffer << s @wbuffer.force_encoding(Encoding::BINARY) @sync ||= false - if @sync or @wbuffer.size > BLOCK_SIZE - until @wbuffer.empty? - begin - nwrote = syswrite(@wbuffer) - rescue Errno::EAGAIN - retry + buffer_size = @wbuffer.size + if @sync or buffer_size > BLOCK_SIZE + nwrote = 0 + begin + while nwrote < buffer_size do + begin + nwrote += syswrite(@wbuffer) + rescue Errno::EAGAIN + retry + end end + ensure @wbuffer[0, nwrote] = "" end end