From 2dee91d7bd617c4109179e839023b436147b593b 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 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 9570f14f3..e1e507a34 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -345,15 +345,17 @@ def do_write(s) @wbuffer << s @wbuffer.force_encoding(Encoding::BINARY) @sync ||= false - if @sync or @wbuffer.size > BLOCK_SIZE - until @wbuffer.empty? + buffer_size = @wbuffer.size + if @sync or buffer_size > BLOCK_SIZE + nwrote = 0 + while nwrote < buffer_size do begin - nwrote = syswrite(@wbuffer) + nwrote += syswrite(@wbuffer) rescue Errno::EAGAIN retry end - @wbuffer[0, nwrote] = "" end + @wbuffer[0, nwrote] = "" end end