Make memcached allow no port = default port
Change-Id: I5a6cb714a4fd7a57db63aa17bb043fcc7a8eb29b
This commit is contained in:
parent
05ae8686b9
commit
e1597a0ae2
@ -27,6 +27,8 @@ import time
|
|||||||
from bisect import bisect
|
from bisect import bisect
|
||||||
from hashlib import md5
|
from hashlib import md5
|
||||||
|
|
||||||
|
DEFAULT_MEMCACHED_PORT = 11211
|
||||||
|
|
||||||
CONN_TIMEOUT = 0.3
|
CONN_TIMEOUT = 0.3
|
||||||
IO_TIMEOUT = 2.0
|
IO_TIMEOUT = 2.0
|
||||||
PICKLE_FLAG = 1
|
PICKLE_FLAG = 1
|
||||||
@ -104,7 +106,11 @@ class MemcacheRing(object):
|
|||||||
yield server, fp, sock
|
yield server, fp, sock
|
||||||
except IndexError:
|
except IndexError:
|
||||||
try:
|
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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
sock.settimeout(self._connect_timeout)
|
sock.settimeout(self._connect_timeout)
|
||||||
|
@ -21,6 +21,7 @@ import logging
|
|||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from swift.common import memcached
|
from swift.common import memcached
|
||||||
|
|
||||||
@ -137,10 +138,25 @@ class TestMemcached(unittest.TestCase):
|
|||||||
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock2.bind(('127.0.0.1', 0))
|
sock2.bind(('127.0.0.1', 0))
|
||||||
sock2.listen(1)
|
sock2.listen(1)
|
||||||
sock2ipport = '%s:%s' % sock2.getsockname()
|
orig_port = memcached.DEFAULT_MEMCACHED_PORT
|
||||||
memcache_client = memcached.MemcacheRing([sock1ipport, sock2ipport])
|
try:
|
||||||
for conn in memcache_client._get_conns('40000000000000000000000000000000'):
|
sock2ip, memcached.DEFAULT_MEMCACHED_PORT = sock2.getsockname()
|
||||||
self.assert_('%s:%s' % conn[2].getpeername() in (sock1ipport, sock2ipport))
|
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):
|
def test_set_get(self):
|
||||||
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
memcache_client = memcached.MemcacheRing(['1.2.3.4:11211'])
|
||||||
|
Loading…
Reference in New Issue
Block a user