From f3290123f2fbcccdab5ddefb5e033c7a4db41803 Mon Sep 17 00:00:00 2001 From: Tim Sebastian Date: Tue, 6 Oct 2015 11:03:07 +0200 Subject: [PATCH 1/3] initialize "tstack" and "tpos" only in the constructor As they are already initialized in the constructor now, there is no need to overwrite/reinitialize them in the "writeMessageBegin" method --- lib/nodejs/lib/thrift/json_protocol.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/nodejs/lib/thrift/json_protocol.js b/lib/nodejs/lib/thrift/json_protocol.js index e98650c5098..3c7c9ed86dc 100644 --- a/lib/nodejs/lib/thrift/json_protocol.js +++ b/lib/nodejs/lib/thrift/json_protocol.js @@ -99,9 +99,6 @@ TJSONProtocol.prototype.flush = function() { * @param {number} seqid - The sequence number of this call (always 0 in Apache Thrift). */ TJSONProtocol.prototype.writeMessageBegin = function(name, messageType, seqid) { - this.tstack = []; - this.tpos = []; - this.tstack.push([TJSONProtocol.Version, '"' + name + '"', messageType, seqid]); }; From c7c968ff9eeea76d94b3eeafcc7138450fda1437 Mon Sep 17 00:00:00 2001 From: Tim Sebastian Date: Tue, 6 Oct 2015 11:31:44 +0200 Subject: [PATCH 2/3] introduce method "writeToTransportIfStackIfFlushable" this writes to the transport if and only if the tstack has only one item left and we can safely assume its ready to be flushed --- lib/nodejs/lib/thrift/json_protocol.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/nodejs/lib/thrift/json_protocol.js b/lib/nodejs/lib/thrift/json_protocol.js index 3c7c9ed86dc..0106aa3c105 100644 --- a/lib/nodejs/lib/thrift/json_protocol.js +++ b/lib/nodejs/lib/thrift/json_protocol.js @@ -92,6 +92,12 @@ TJSONProtocol.prototype.flush = function() { return this.trans.flush(); }; +TJSONProtocol.prototype.writeToTransportIfStackIsFlushable = function() { + if (this.tstack.length === 1) { + this.trans.write(this.tstack.pop()); + } +}; + /** * Serializes the beginning of a Thrift RPC message. * @param {string} name - The service method to call. From 1544a475fd8bc715064576574cb5b48f0c9b6570 Mon Sep 17 00:00:00 2001 From: Tim Sebastian Date: Tue, 6 Oct 2015 11:35:14 +0200 Subject: [PATCH 3/3] call "writeToTransportIfStackIfFlushable" if appropriate call the "writeToTransportIfStackIfFlushable" method on every "end"-step to make sure we can safely flush --- lib/nodejs/lib/thrift/json_protocol.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/nodejs/lib/thrift/json_protocol.js b/lib/nodejs/lib/thrift/json_protocol.js index 0106aa3c105..a4c0b0ce1d5 100644 --- a/lib/nodejs/lib/thrift/json_protocol.js +++ b/lib/nodejs/lib/thrift/json_protocol.js @@ -89,6 +89,7 @@ TJSONProtocol.RType.set = Type.SET; TJSONProtocol.Version = 1; TJSONProtocol.prototype.flush = function() { + this.writeToTransportIfStackIsFlushable(); return this.trans.flush(); }; @@ -119,6 +120,7 @@ TJSONProtocol.prototype.writeMessageEnd = function() { this.wbuf = '[' + this.wobj.join(',') + ']'; + // we assume there is nothing more to come so we write this.trans.write(this.wbuf); }; @@ -151,6 +153,8 @@ TJSONProtocol.prototype.writeStructEnd = function() { str += '}'; this.tstack[p] = str; + + this.writeToTransportIfStackIsFlushable(); }; /** @@ -181,6 +185,8 @@ TJSONProtocol.prototype.writeFieldEnd = function() { fieldInfo.fieldType + ':' + value + '}'; } this.tpos.pop(); + + this.writeToTransportIfStackIsFlushable(); }; /** @@ -237,6 +243,8 @@ TJSONProtocol.prototype.writeMapEnd = function() { this.tstack[p].push(map); this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; + + this.writeToTransportIfStackIsFlushable(); }; /** @@ -262,6 +270,8 @@ TJSONProtocol.prototype.writeListEnd = function() { } this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; + + this.writeToTransportIfStackIsFlushable(); }; /** @@ -287,6 +297,8 @@ TJSONProtocol.prototype.writeSetEnd = function() { } this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; + + this.writeToTransportIfStackIsFlushable(); }; /** Serializes a boolean */