Older Entries »

consistent hash ring in node.js

If you ever need to create a consistent hash ring in node.js, this may help you. This is compatible with redis-rb's dht algorithm. This is not well tested yet, but in preliminary tests it is working good. Thanks to this stackoverflow post for the crc32 algo. Future improvements may include parts of this re-done as a c module for added speed. Currently I am not using this per inbound request, only once per server start, so speed is less of an immediate priority. Enjoy!

this.ring = {};
this.sorted_keys = [];

this.generateRing = function(servers) {
  for(var j = 0; j < servers.length; j++) {
    this.addServerToRing(servers[j]);
  }
}

this.addServerToRing = function(server) {
  for(r = 0; r < 160; r++) {
      key = this.crc32("redis://"+server+"/0:"+r);
      this.ring[key] = server;
      this.sorted_keys.push(key);
  }

  this.sorted_keys.sort(function (a, b) {
    return a-b;
  });
}

this.getServer = function(key_query) {
  key = this.crc32(key_query);
  index = this.ringSearch(key);
  return this.ring[this.sorted_keys[index]];
}

this.ringSearch = function(val) {

  var upper = this.sorted_keys.length - 1;
  var begin = upper;
  var lower = 0;
  var k = 0;

  while(lower <= upper) {
    k = parseInt(((lower + upper) / 2),10);
    idx = this.sorted_keys[k];

    if(idx == val) {
      return k;
    } else if ( idx > val ) {
      upper = k - 1;
    } else {
      lower = k + 1;
    }
  }

  if(upper < 0) {
    upper = begin;
  }
  return upper;
}


this.crc32 = (function() {
    function utf8encode(str) {
        var utf8CharCodes = [];

        for (var i = 0, len = str.length, c; i < len; ++i) {
            c = str.charCodeAt(i);
            if (c < 128) {
                utf8CharCodes.push(c);
            } else if (c < 2048) {
                utf8CharCodes.push((c >> 6) | 192, (c & 63) | 128);
            } else {
                utf8CharCodes.push((c >> 12) | 224, ((c >> 6) & 63) | 128, (c & 63) | 128);
            }
        }
        return utf8CharCodes;
    }

    var cachedCrcTable = null;

    function buildCRCTable() {
        var table = [];
        for (var i = 0, j, crc; i < 256; ++i) {
            crc = i;
            j = 8;
            while (j--) {
                if ((crc & 1) == 1) {
                    crc = (crc >>> 1) ^ 0xEDB88320;
                } else {
                    crc >>>= 1;
                }
            }
            table[i] = crc >>> 0;
        }
        return table;
    }

    function getCrcTable() {
        if (!cachedCrcTable) {
            cachedCrcTable = buildCRCTable();
        }
        return cachedCrcTable;
    }

    return function(str) {
        var utf8CharCodes = utf8encode(str), crc = -1, crcTable = getCrcTable();
        for (var i = 0, len = utf8CharCodes.length, y; i < len; ++i) {
            y = (crc ^ utf8CharCodes[i]) & 0xFF;
            crc = (crc >>> 8) ^ crcTable[y];
        }
        return (crc ^ -1) >>> 0;
    };
})();

Altly Begins

Since the last update, I have quit my job, moved to the beach, and started a company with a small team of awesome people. For the first time in quite some time, I am getting a lot of fresh air and sun light regularly, and it is an awesome thing.

Reserve your username today! More details soon :)


zeromq adventures and linkfinder

I have been hearing about ZeroMQ for a bit now, but had never really dug in until 72 hours ago. I must say, I am impressed. Maybe I am easily amused (likely), or maybe I have just been having a good run with technology lately, but nevertheless, I will share a bit of my experience.

What is ZeroMQ (aka '0mq') you ask? At first glance it comes across as a networking library, but that is just the tip of the iceberg. Once cracked open it is a concurrency library, networking library, and socket library all wrapped into one really simple and concise api.

Sounds like it is chalk full of features and bloatware, but right on the front of their site they have 'Less is more' in big bold letters. I like this concept in engineering most of the time. Actually I like this concept applied to life itself most of the time. Unless I am at a vegas buffet anyways. Hah!

So I figured for a quick test trial of the library I would write a service in C that returns links in a given webpage. One or more clients will connect to the service and send a url that it would like scanned. This url is then downloaded, parsed, and the links in it, if any, are returned in JSON format.

Without further ado, check it out on github: https://github.com/cowboyrushforth/linkfinder. Turned out to be about 245 sloc and only took a few spare hours to write. How it is setup is that the main thread has one big event loop that 0mq runs and accepts requests. These requests go into a queue that one of 4 worker threads pick up. The worker picks up a request, does all the downloading, parsing, and then sends the request back to the mainthread which then delivers it to the client. It could use a bit of additional error handling thought, but is incredibly simple.

If your feeling like a technologically inspirational read, check out the ZeroMQ guide. It is very comprehensive and really shows the full potential of the library.

Cheers!

on node.js, trendy technology, and the development community

Ok, so I have dived into node.js, as seen on my recent entry on building a realtime, web based, minesweeper clone. I dove into this mostly to just see what all the fuss is about, but I would be lying if I said I was not pleasantly surprised.

This seems to be a topic that one must tread lightly about as node is getting quite a bit of flack as well as quite a few praises and success stories. Seems like a love/hate relationship with no counselor! Why the great divide?

My goal in writing this is to just outline both sides of the story and hopefully give a fairly neutral opinion on the matter but shining some light on all angles. So here goes: (continued after break)
Continue Reading -->

MineSpotter v0.0.1

So with all the excitement in the node.js community, I thought I would see what all the fuss is about, so I went to work on my first node.js application, MineSpotter.

MineSpotter is essentially a minesweeper clone, but heavily inspired by a website called WordSquared. MineSpotter is to MineSweeper as WordSquared is to Scrabble. Ultimately the game is designed to have an infinitely expanding board, and can be played in realtime, with multiple players.

MineSpotter was built using node.js, along with a node library called DNode on top of the geospatial features of MongoDB.

Still have a lot of things to finish, such as a leader board, facebook connect, and more work on flagging and explosions, but the core gameplay is pretty much completed.

Source is up at github here for your reading and hacking pleasure, and thanks to DuoStack I have been able to get some free hosting for it.

Check it out at http://play.minespotter.com!

Older Entries »

Tag cloud

  1. 1 entries are tagged with 2007
  2. 1 entries are tagged with 2008
  3. 1 entries are tagged with 2009
  4. 2 entries are tagged with 2010
  5. 1 entries are tagged with altly
  6. 2 entries are tagged with applevalley
  7. 1 entries are tagged with archlinux
  8. 1 entries are tagged with artichoke
  9. 1 entries are tagged with automation
  10. 1 entries are tagged with batcountry
  11. 1 entries are tagged with beats
  12. 1 entries are tagged with bigsur
  13. 3 entries are tagged with bm2009
  14. 1 entries are tagged with bm2010
  15. 8 entries are tagged with burningman
  16. 2 entries are tagged with c
  17. 1 entries are tagged with christmas
  18. 8 entries are tagged with code
  19. 1 entries are tagged with consistent
  20. 1 entries are tagged with cplusplus
  21. 1 entries are tagged with desert
  22. 1 entries are tagged with drinks
  23. 1 entries are tagged with dspam
  24. 2 entries are tagged with dunes
  25. 1 entries are tagged with energy
  26. 1 entries are tagged with esplanade
  27. 5 entries are tagged with europe
  28. 1 entries are tagged with evdo
  29. 2 entries are tagged with flv
  30. 1 entries are tagged with gadgets
  31. 1 entries are tagged with government
  32. 1 entries are tagged with haiku
  33. 1 entries are tagged with hashring
  34. 1 entries are tagged with icecast
  35. 1 entries are tagged with internetradio
  36. 1 entries are tagged with iphone
  37. 1 entries are tagged with linkfinder
  38. 2 entries are tagged with losangeles
  39. 1 entries are tagged with math
  40. 1 entries are tagged with minespotter
  41. 1 entries are tagged with minesweeper
  42. 3 entries are tagged with mix
  43. 1 entries are tagged with motivation
  44. 1 entries are tagged with moving
  45. 3 entries are tagged with music
  46. 1 entries are tagged with newserver
  47. 4 entries are tagged with node.js
  48. 1 entries are tagged with obama
  49. 1 entries are tagged with oil
  50. 1 entries are tagged with productivity
  51. 1 entries are tagged with radio
  52. 1 entries are tagged with rails
  53. 3 entries are tagged with recipe
  54. 1 entries are tagged with redmine
  55. 2 entries are tagged with reflections
  56. 1 entries are tagged with rmine
  57. 8 entries are tagged with ruby
  58. 1 entries are tagged with salmon
  59. 1 entries are tagged with salsa
  60. 1 entries are tagged with shoutcast
  61. 3 entries are tagged with site
  62. 2 entries are tagged with snow
  63. 1 entries are tagged with spill
  64. 1 entries are tagged with spotmanradio
  65. 1 entries are tagged with streamtranscoder
  66. 1 entries are tagged with summer
  67. 1 entries are tagged with tecate
  68. 1 entries are tagged with technology
  69. 1 entries are tagged with thanksgiving
  70. 1 entries are tagged with thoughts
  71. 6 entries are tagged with travel
  72. 1 entries are tagged with trip
  73. 1 entries are tagged with turkey
  74. 3 entries are tagged with twitter
  75. 1 entries are tagged with website
  76. 1 entries are tagged with wordsquared
  77. 4 entries are tagged with work
  78. 1 entries are tagged with x4200
  79. 3 entries are tagged with yz250
  80. 1 entries are tagged with zeromq

Twitter Updates

Links to check out

Subscribe RSS