Skip to content

Commit

Permalink
Make memcached allow no port = default port
Browse files Browse the repository at this point in the history
Change-Id: I5a6cb714a4fd7a57db63aa17bb043fcc7a8eb29b
  • Loading branch information
gholt committed Jan 12, 2012
1 parent 05ae868 commit e1597a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 7 additions & 1 deletion swift/common/memcached.py
Expand Up @@ -27,6 +27,8 @@
from bisect import bisect
from hashlib import md5

DEFAULT_MEMCACHED_PORT = 11211

CONN_TIMEOUT = 0.3
IO_TIMEOUT = 2.0
PICKLE_FLAG = 1
Expand Down Expand Up @@ -104,7 +106,11 @@ def _get_conns(self, key):
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)
Expand Down
24 changes: 20 additions & 4 deletions test/unit/common/test_memcached.py
Expand Up @@ -21,6 +21,7 @@
import socket
import time
import unittest
from uuid import uuid4

from swift.common import memcached

Expand Down Expand Up @@ -137,10 +138,25 @@ def test_get_conns(self):
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'])
Expand Down

0 comments on commit e1597a0

Please sign in to comment.