From e1597a0ae2972df2d222af6c92d519cc68c8f9f4 Mon Sep 17 00:00:00 2001 From: gholt Date: Thu, 12 Jan 2012 22:30:32 +0000 Subject: [PATCH] Make memcached allow no port = default port Change-Id: I5a6cb714a4fd7a57db63aa17bb043fcc7a8eb29b --- swift/common/memcached.py | 8 +++++++- test/unit/common/test_memcached.py | 24 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/swift/common/memcached.py b/swift/common/memcached.py index 41c75aaa65..89d1720db8 100644 --- a/swift/common/memcached.py +++ b/swift/common/memcached.py @@ -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) diff --git a/test/unit/common/test_memcached.py b/test/unit/common/test_memcached.py index b54f915dca..73228c9ebd 100644 --- a/test/unit/common/test_memcached.py +++ b/test/unit/common/test_memcached.py @@ -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'])