[memcached] bradfitz, r311: + * support access via unix domain...

commits at code.sixapart.com commits at code.sixapart.com
Mon Aug 21 20:05:33 UTC 2006


+       * support access via unix domain sockets. (Nathan Neulinger <nneul at umr.edu>)



U   trunk/api/perl/ChangeLog
U   trunk/api/perl/lib/Cache/Memcached.pm


Modified: trunk/api/perl/ChangeLog
===================================================================
--- trunk/api/perl/ChangeLog	2006-08-21 20:01:42 UTC (rev 310)
+++ trunk/api/perl/ChangeLog	2006-08-21 20:05:32 UTC (rev 311)
@@ -1,3 +1,5 @@
+	* support access via unix domain sockets. (Nathan Neulinger <nneul at umr.edu>)
+
 	* abstract out response parsing into own class, and add XS-module
 	  detection, so if you have the XS (C) version, things'll be faster.
 	  that part's not done yet.

Modified: trunk/api/perl/lib/Cache/Memcached.pm
===================================================================
--- trunk/api/perl/lib/Cache/Memcached.pm	2006-08-21 20:01:42 UTC (rev 310)
+++ trunk/api/perl/lib/Cache/Memcached.pm	2006-08-21 20:05:32 UTC (rev 311)
@@ -10,7 +10,7 @@
 use strict;
 no strict 'refs';
 use Storable ();
-use Socket qw( MSG_NOSIGNAL PF_INET IPPROTO_TCP SOCK_STREAM );
+use Socket qw( MSG_NOSIGNAL PF_INET PF_UNIX IPPROTO_TCP SOCK_STREAM );
 use IO::Handle ();
 use Time::HiRes ();
 use String::CRC32;
@@ -235,31 +235,43 @@
     my $sin;
     my $proto = $PROTO_TCP ||= getprotobyname('tcp');
 
-    # if a preferred IP is known, try that first.
-    if ($self && $self->{pref_ip}{$ip}) {
-        socket($sock, PF_INET, SOCK_STREAM, $proto);
-        my $prefip = $self->{pref_ip}{$ip};
-        $sin = Socket::sockaddr_in($port,Socket::inet_aton($prefip));
-        if (_connect_sock($sock,$sin,$self->{connect_timeout})) {
-            $connected = 1;
-        } else {
-            if (my $cb = $self->{cb_connect_fail}) {
-                $cb->($prefip);
+    if ( index($host, '/') != 0 )
+    {
+        # if a preferred IP is known, try that first.
+        if ($self && $self->{pref_ip}{$ip}) {
+            socket($sock, PF_INET, SOCK_STREAM, $proto);
+            my $prefip = $self->{pref_ip}{$ip};
+            $sin = Socket::sockaddr_in($port,Socket::inet_aton($prefip));
+            if (_connect_sock($sock,$sin,$self->{connect_timeout})) {
+                $connected = 1;
+            } else {
+                if (my $cb = $self->{cb_connect_fail}) {
+                    $cb->($prefip);
+                }
+                close $sock;
             }
-            close $sock;
         }
-    }
 
-    # normal path, or fallback path if preferred IP failed
-    unless ($connected) {
-        socket($sock, PF_INET, SOCK_STREAM, $proto);
-        $sin = Socket::sockaddr_in($port,Socket::inet_aton($ip));
+        # normal path, or fallback path if preferred IP failed
+        unless ($connected) {
+            socket($sock, PF_INET, SOCK_STREAM, $proto);
+            $sin = Socket::sockaddr_in($port,Socket::inet_aton($ip));
+            my $timeout = $self ? $self->{connect_timeout} : 0.25;
+            unless (_connect_sock($sock,$sin,$timeout)) {
+                my $cb = $self ? $self->{cb_connect_fail} : undef;
+                $cb->($ip) if $cb;
+                return _dead_sock($sock, undef, 20 + int(rand(10)));
+            }
+        }
+    } else { # it's a unix domain/local socket
+        socket($sock, PF_UNIX, SOCK_STREAM, 0);
+        $sin = Socket::sockaddr_un($host);
         my $timeout = $self ? $self->{connect_timeout} : 0.25;
         unless (_connect_sock($sock,$sin,$timeout)) {
             my $cb = $self ? $self->{cb_connect_fail} : undef;
-            $cb->($ip) if $cb;
+            $cb->($host) if $cb;
             return _dead_sock($sock, undef, 20 + int(rand(10)));
-        }
+        }    
     }
 
     # make the new socket not buffer writes.
@@ -895,7 +907,7 @@
   use Cache::Memcached;
 
   $memd = new Cache::Memcached {
-    'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212",
+    'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212", "/var/sock/memcached",
                    "10.0.0.17:11211", [ "10.0.0.17:11211", 3 ] ],
     'debug' => 0,
     'compress_threshold' => 10_000,




More information about the memcached-commits mailing list