Make memcached allow no port = default port

Change-Id: I5a6cb714a4fd7a57db63aa17bb043fcc7a8eb29b
This commit is contained in:
gholt 2012-01-12 22:30:32 +00:00
parent 05ae8686b9
commit e1597a0ae2
2 changed files with 27 additions and 5 deletions

View File

@ -27,6 +27,8 @@ import time
from bisect import bisect
from hashlib import md5
DEFAULT_MEMCACHED_PORT = 11211
CONN_TIMEOUT = 0.3
IO_TIMEOUT = 2.0
PICKLE_FLAG = 1
@ -104,7 +106,11 @@ class MemcacheRing(object):
yield server, fp, sock
except IndexError:
try:
host, port = server.split(':')
if ':' in server:
host, port = server.split(':')
else:
host = server
port = DEFAULT_MEMCACHED_PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.settimeout(self._connect_timeout)

View File

@ -21,6 +21,7 @@ import logging
import socket
import time
import unittest
from uuid import uuid4
from swift.common import memcached
@ -137,10 +138,25 @@ class TestMemcached(unittest.TestCase):
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.bind(('127.0.0.1', 0))
sock2.listen(1)
sock2ipport = '%s:%s' % sock2.getsockname()
memcache_client = memcached.MemcacheRing([sock1ipport, sock2ipport])
for conn in memcache_client._get_conns('40000000000000000000000000000000'):
self.assert_('%s:%s' % conn[2].getpeername() in (sock1ipport, sock2ipport))
orig_port = memcached.DEFAULT_MEMCACHED_PORT
try:
sock2ip, memcached.DEFAULT_MEMCACHED_PORT = sock2.getsockname()
sock2ipport = '%s:%s' % (sock2ip, memcached.DEFAULT_MEMCACHED_PORT)
# We're deliberately using sock2ip (no port) here to test that the
# default port is used.
memcache_client = memcached.MemcacheRing([sock1ipport, sock2ip])
one = two = True
while one or two: # Run until we match hosts one and two
key = uuid4().hex
for conn in memcache_client._get_conns(key):
peeripport = '%s:%s' % conn[2].getpeername()
self.assert_(peeripport in (sock1ipport, sock2ipport))
if peeripport == sock1ipport:
one = False
if peeripport == sock2ipport:
two = False
finally:
memcached.DEFAULT_MEMCACHED_PORT = orig_port
def test_set_get(self):
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])