From d6cc47f40e7662eb16172b893c62e19f70430196 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 21 Apr 2011 11:59:50 +0700 Subject: [PATCH 1/2] [tls] write data by small (1500 bytes) chunks, see http://www.belshe.com/2010/12/17/performance-and-the-tls-record-size/ --- lib/tls.js | 6 ++---- src/node_crypto.cc | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index b5356971580..2a5f4fb31b1 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -382,8 +382,8 @@ CryptoStream.prototype._pull = function() { this.pair._maybeInitFinished(); - if (rv === 0 || rv < 0) { - this._pending.unshift(tmp); + if (rv < tmp.length) { + this._pending.unshift(tmp.slice(Math.max(rv, 0))); this._pendingCallbacks.unshift(cb); break; } @@ -392,8 +392,6 @@ CryptoStream.prototype._pull = function() { assert(this._pendingBytes >= 0); if (cb) cb(); - - assert(rv === tmp.length); } // If we've cleared all of incoming encrypted data, emit drain. diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 2d82ff063ed..b715eddb8d1 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -959,12 +959,38 @@ Handle Connection::ClearIn(const Arguments& args) { if (rv < 0) return scope.Close(Integer::New(rv)); } - int bytes_written = SSL_write(ss->ssl_, buffer_data + off, len); + const int chunkSize = 1500; + int rv = 0; + int offset = 0; + int total_bytes_written = 0; + + while (len > 0) { + int chunkSize_ = chunkSize > len ? len : chunkSize; + int bytes_written = SSL_write(ss->ssl_, + buffer_data + off + offset, + chunkSize_); + bytes_written = ss->HandleSSLError("SSL_write:ClearIn", + bytes_written); + + // Written nothing at all + if (bytes_written <= 0) { + break; + } + + total_bytes_written += bytes_written; + + // Written less than planned + if (bytes_written < chunkSize_) { + break; + } + + offset += chunkSize_; + len -= chunkSize_; + } - ss->HandleSSLError("SSL_write:ClearIn", bytes_written); ss->SetShutdownFlags(); - return scope.Close(Integer::New(bytes_written)); + return scope.Close(Integer::New(total_bytes_written)); } From 1807d90a538e2d156db3e33c482ca253312edf6d Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 21 Apr 2011 12:04:33 +0700 Subject: [PATCH 2/2] [tls] if we failed to write first chunk - total_bytes_written should be <= 0 , not just 0 --- src/node_crypto.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index b715eddb8d1..dbc912fb7b6 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -974,6 +974,11 @@ Handle Connection::ClearIn(const Arguments& args) { // Written nothing at all if (bytes_written <= 0) { + // If we has failed to write first chunk + // total_bytes_written can have negative value + if (total_bytes_written == 0) { + total_bytes_written += bytes_written; + } break; }