Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added `removeServer` method for the removal of servers. Updated versi…

…on number.
  • Loading branch information...
commit f8ec76620df28abde59f9cf4cad6125de7ffd0f6 1 parent ce7a6a3
@3rd-Eden authored
View
3  CHANGELOG
@@ -1,3 +1,6 @@
+May 05, 2011
+- Added the missing `removeServer` method.
+
April 22, 2011
- Externalized the bisection and applied indutny / donnerjack's patches.
- Added test suite
View
24 doc/index.html
@@ -279,6 +279,30 @@
</tr>
<tr class="code">
<td class="docs">
+<p>Removes a server from the ring and regenerates the ring.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>String</em> server The IP of the server that needs to be removed</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="class">HashRing</span>.<span class="variable">removeServer</span> = <span class="keyword">function</span>(<span class="variable">server</span>){
+ <span class="keyword">var</span> <span class="variable">index</span> = <span class="this">this</span>.<span class="variable">nodes</span>.<span class="variable">indexOf</span>(<span class="variable">server</span>);
+ <span class="keyword">if</span> (<span class="variable">index</span> == -<span class="number integer">1</span>) <span class="keyword">return</span>; <span class="comment">// no need to re-generate the whole ring, no match</span>
+
+ <span class="this">this</span>.<span class="variable">nodes</span>.<span class="variable">splice</span>(<span class="variable">index</span>, <span class="number integer">1</span>);
+ <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">weights</span>[<span class="variable">server</span>]){
+ <span class="keyword">delete</span> <span class="this">this</span>.<span class="variable">weights</span>[<span class="variable">server</span>];
+ }
+ <span class="comment">// clear all old caches and regenerate</span>
+ <span class="this">this</span>.<span class="variable">ring</span> = {};
+ <span class="this">this</span>.<span class="variable">cache</span> = {};
+ <span class="this">this</span>.<span class="variable">generateRing</span>();
+};</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
<p>Iterates over the nodes for a give key, can be used to create redundancy support.</p>
<h2></h2>
View
22 lib/hashring.js
@@ -202,6 +202,26 @@ HashRing.addServer = function(server, weights){
};
/**
+ * Removes a server from the ring and regenerates the ring.
+ *
+ * @param {String} server The IP of the server that needs to be removed
+ * @api public
+ */
+HashRing.removeServer = function(server){
+ var index = this.nodes.indexOf(server);
+ if (index == -1) return; // no need to re-generate the whole ring, no match
+
+ this.nodes.splice(index, 1);
+ if (this.weights[server]){
+ delete this.weights[server];
+ }
+ // clear all old caches and regenerate
+ this.ring = {};
+ this.cache = {};
+ this.generateRing();
+};
+
+/**
* Iterates over the nodes for a give key, can be used to create redundancy support.
*
* @param {String} key The key that should get a range
@@ -319,6 +339,6 @@ HashRing.crc32HashKey = function(str){
/**
* Library version
*/
-hashRing.version = '0.0.3';
+hashRing.version = '0.0.4';
module.exports = hashRing;
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "hashring"
-, "version": "0.0.3"
+, "version": "0.0.4"
, "author": "Arnout Kazemier"
, "description": "A pure JavaScript hash ring based on libketama"
, "main": "index"
View
6 tests/hashring.test.js
@@ -71,6 +71,12 @@ module.exports = {
ring.cache = {}; // clear cache
ring.getNode('justdied').should.equal(skynet);
}
+
+, 'Removing servers': function(){
+ var ring = new hashring(['192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212']);
+ ring.removeServer('192.168.0.102:11212');
+ ring.nodes.indexOf('192.168.0.102:11212').should.equal(-1);
+ }
// kindly lended from `node-hash-ring` :)
, 'Distribution': function(){
Please sign in to comment.
Something went wrong with that request. Please try again.