From ebe698720a7590f6b52a1fe2d65d5ecb838e3e52 Mon Sep 17 00:00:00 2001 From: Andreas Stokholm Date: Sun, 10 Feb 2013 04:16:01 +0100 Subject: [PATCH 1/3] Fixed crash when server went away. --- lib/memcache.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..ec73a04 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -28,6 +28,7 @@ var tcp = require('net'), var crlf = "\r\n"; var crlf_len = crlf.length; +var self; var error_replies = ['ERROR', 'NOT_FOUND', 'CLIENT_ERROR', 'SERVER_ERROR']; @@ -47,7 +48,7 @@ util.inherits(Client, process.EventEmitter); Client.prototype.connect = function () { if (!this.conn) { this.conn = new tcp.createConnection(this.port, this.host); - var self = this; + self = this; this.conn.addListener("connect", function () { this.setTimeout(0); // try to stay connected. this.setNoDelay(); @@ -107,7 +108,12 @@ Client.prototype.dispatchHandles = function() { Client.prototype.query = function(query, type, callback) { this.callbacks.push({ type: type, fun: callback }); this.sends++; - this.conn.write(query + crlf); + if (this.conn !== null) { + this.conn.write(query + crlf); + } else { + // Server has crashed or in another way become unavailable, connection closed. + self.emit("close"); + } }; Client.prototype.close = function() { From 1ba2a3b858331c9a413a5759db4731c10d8b9cde Mon Sep 17 00:00:00 2001 From: Firer Date: Fri, 31 Aug 2018 16:48:11 +0100 Subject: [PATCH 2/3] Fix for newer versions of Node process.EventEmitter is deprecated --- lib/memcache.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..6f724f6 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -24,7 +24,8 @@ */ var tcp = require('net'), - util = require('util'); + util = require('util'), + EventEmitter = require('events'); var crlf = "\r\n"; var crlf_len = crlf.length; @@ -42,7 +43,7 @@ var Client = exports.Client = function(port, host) { this.handles = []; }; -util.inherits(Client, process.EventEmitter); +util.inherits(Client, EventEmitter); Client.prototype.connect = function () { if (!this.conn) { From 0c6125d563aabb361aa79986fba16e7fcf9181f3 Mon Sep 17 00:00:00 2001 From: Firer Date: Thu, 13 Sep 2018 16:25:52 +0100 Subject: [PATCH 3/3] Fixed bug when 'END' is contained in the cached data From lusentis pull request https://github.com/elbart/node-memcache/pull/23 --- lib/memcache.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 0c505c0..43bbbec 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -278,11 +278,11 @@ Client.prototype.handle_get = function(buffer) { var end_indicator_len = 3; var result_len = 0; - if (buffer.indexOf('END') == 0) { + if (buffer.lastIndexOf('END') == 0) { return [result_value, end_indicator_len + crlf_len]; - } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) { + } else if (buffer.indexOf('VALUE') == 0 && buffer.lastIndexOf('END') != -1) { first_line_len = buffer.indexOf(crlf) + crlf_len; - var end_indicator_start = buffer.indexOf('END'); + var end_indicator_start = buffer.lastIndexOf('END'); result_len = end_indicator_start - first_line_len - crlf_len; result_value = buffer.substr(first_line_len, result_len); return [result_value, first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len]