memcached: better error messaging
authorAntoine Musso <hashar@free.fr>
Fri, 13 Jul 2012 16:20:00 +0000 (18:20 +0200)
committerAntoine Musso <hashar@free.fr>
Fri, 13 Jul 2012 16:20:00 +0000 (18:20 +0200)
MemcachedClient output a generic error message: "Error parsing memcached
response\n" whenever it is not able to read from the socket. It is also
lacking the remote peer it is reading form.

This patch add a new message when fgets( <socket> ) return false and
attempt to get the remote peer address / port to append to the error
message. Might help us find out which memcached server is wild.

Change-Id: If918e824970aaa8231078e42fd28d31e8dd4e319

includes/objectcache/MemcachedClient.php

index ec67a39..eda57c0 100644 (file)
@@ -895,7 +895,10 @@ class MWMemcached {
        function _load_items( $sock, &$ret ) {
                while ( 1 ) {
                        $decl = fgets( $sock );
-                       if ( $decl == "END\r\n" ) {
+                       if( $decl === false ) {
+                               $this->_debugprint( "Error reading socket for a memcached response\n" );
+                               return 0;
+                       } elseif ( $decl == "END\r\n" ) {
                                return true;
                        } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
                                list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
@@ -939,7 +942,12 @@ class MWMemcached {
                                }
 
                        } else {
-                               $this->_debugprint( "Error parsing memcached response\n" );
+                               $peer = $peerAddress = $peerPort = '';
+                               $gotPeer = socket_getpeername( $sock, $peerAddress, $peerPort );
+                               if( $gotPeer ) {
+                                       $peer = " from [$peerAddress:$peerPort";
+                               }
+                               $this->_debugprint( "Error parsing memcached response{$peer}\n" );
                                return 0;
                        }
                }