Permalink
Browse files

Can now connect to a server listening on a Unix socket

  • Loading branch information...
alfyboza committed Apr 4, 2012
1 parent f2faf12 commit 4066181a3b713f541cba455ce1edae9b949e1560
Showing with 13 additions and 6 deletions.
  1. +5 −0 examples/memcached_connect_socket.js
  2. +8 −6 lib/memcached.js
@@ -0,0 +1,5 @@
+var nMemcached = require( '../' ),
+ memcached;
+
+// connect to our memcached server listening on Unix socket /tmp/.memcached.sock
+memcached = new nMemcached( '/tmp/.memcached.sock' );
View
@@ -1,5 +1,6 @@
var EventEmitter = require('events').EventEmitter
, Stream = require('net').Stream
+ , Socket = require('net').Socket
, Buffer = require('buffer').Buffer;
var HashRing = require('hashring')
@@ -94,14 +95,15 @@ Client.config = {
if (server in this.connections) return this.connections[server].allocate(callback);
// No connection factory created yet, so we must build one
- var serverTokens = /(.*):(\d+){1,}$/.exec(server).reverse()
+ var serverTokens = server[0] === '/' ? server : /(.*):(\d+){1,}$/.exec(server).reverse()
, memcached = this;
- serverTokens.pop();
+ // Pop original string from array
+ if (Array.isArray(serverTokens)) serverTokens.pop();
var sid = 0;
this.connections[server] = new Manager(server, this.poolSize, function(callback){
- var S = new Stream
+ var S = Array.isArray(serverTokens) ? new Stream : new Socket
, Manager = this;
// config the Stream
@@ -112,7 +114,7 @@ Client.config = {
S.responseBuffer = "";
S.bufferArray = [];
S.serverAddress = server;
- S.tokens = serverTokens;
+ S.tokens = [].concat(serverTokens);
S.memcached = memcached;
// Add the event listeners
@@ -125,8 +127,8 @@ Client.config = {
, end: S.end
});
- // connect the net.Stream [port, hostname]
- S.connect.apply(S, serverTokens);
+ // connect the net.Stream (or net.Socket) [port, hostname]
+ S.connect.apply(S, S.tokens);
return S;
});

0 comments on commit 4066181

Please sign in to comment.