Skip to content

Commit

Permalink
Added support for UNIX sockets in HashClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope committed Sep 2, 2020
1 parent 969a124 commit acd962b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
52 changes: 37 additions & 15 deletions pymemcache/client/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def __init__(
Constructor.
Args:
servers: list(tuple(hostname, port))
servers: list() of tuple(hostname, port) or string containing a UNIX
socket path.
hasher: optional class three functions ``get_node``, ``add_node``,
and ``remove_node``
defaults to Rendezvous (HRW) hash.
Expand Down Expand Up @@ -101,30 +102,51 @@ def __init__(
'lock_generator': lock_generator
})

for server, port in servers:
self.add_server(server, port)
for server in servers:
self.add_server(server)
self.encoding = encoding
self.tls_context = tls_context

def add_server(self, server, port):
key = '%s:%s' % (server, port)
def add_server(self, server, port=None):
# To maintain backward compatibility, if a port is provided, assume
# that server wasn't provided as a (host, port) tuple.
if port is not None:
if not isinstance(server, six.string_types):
raise TypeError('Server must be a string when passing port.')
server = (server, port)

if isinstance(server, six.string_types):
key = server
else:
key = '%s:%s' % server

if self.use_pooling:
client = PooledClient(
(server, port),
server,
**self.default_kwargs
)
else:
client = self.client_class((server, port), **self.default_kwargs)
client = self.client_class(server, **self.default_kwargs)

self.clients[key] = client
self.hasher.add_node(key)

def remove_server(self, server, port):
def remove_server(self, server, port=None):
# To maintain backward compatibility, if a port is provided, assume
# that server wasn't provided as a (host, port) tuple.
if port is not None:
if not isinstance(server, six.string_types):
raise TypeError('Server must be a string when passing port.')
server = (server, port)

if isinstance(server, six.string_types):
key = server
else:
key = '%s:%s' % server

dead_time = time.time()
self._failed_clients.pop((server, port))
self._dead_clients[(server, port)] = dead_time
key = '%s:%s' % (server, port)
self._failed_clients.pop(server)
self._dead_clients[server] = dead_time
self.hasher.remove_node(key)

def _retry_dead(self):
Expand All @@ -141,7 +163,7 @@ def _retry_dead(self):
'bringing server back into rotation %s',
server
)
self.add_server(*server)
self.add_server(server)
del self._dead_clients[server]
self._last_dead_check_time = current_time

Expand Down Expand Up @@ -185,7 +207,7 @@ def _safely_run_func(self, client, func, default_val, *args, **kwargs):
# We've reached our max retry attempts, we need to mark
# the sever as dead
logger.debug('marking server as dead: %s', client.server)
self.remove_server(*client.server)
self.remove_server(client.server)

result = func(*args, **kwargs)
return result
Expand Down Expand Up @@ -239,7 +261,7 @@ def _safely_run_set_many(self, client, values, *args, **kwargs):
# We've reached our max retry attempts, we need to mark
# the sever as dead
logger.debug('marking server as dead: %s', client.server)
self.remove_server(*client.server)
self.remove_server(client.server)

succeeded, failed, err = self._set_many(
client, values, *args, **kwargs
Expand Down Expand Up @@ -289,7 +311,7 @@ def _mark_failed_server(self, server):
'attempts': 0,
}
logger.debug("marking server as dead %s", server)
self.remove_server(*server)
self.remove_server(server)
# This client has failed previously, we need to update the metadata
# to reflect that we have attempted it again
else:
Expand Down
35 changes: 31 additions & 4 deletions pymemcache/test/test_client_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .test_client import ClientTestMixin, MockSocket
import unittest
import os
import pytest
import mock
import socket
Expand Down Expand Up @@ -42,7 +43,7 @@ def test_setup_client_without_pooling(self):
client_class = 'pymemcache.client.hash.HashClient.client_class'
with mock.patch(client_class) as internal_client:
client = HashClient([], timeout=999, key_prefix='foo_bar_baz')
client.add_server('127.0.0.1', '11211')
client.add_server(('127.0.0.1', '11211'))

assert internal_client.call_args[0][0] == ('127.0.0.1', '11211')
kwargs = internal_client.call_args[1]
Expand Down Expand Up @@ -313,7 +314,7 @@ def test_server_encoding_client(self):
@mock.patch("pymemcache.client.hash.HashClient.client_class")
def test_dead_server_comes_back(self, client_patch):
client = HashClient([], dead_timeout=0, retry_attempts=0)
client.add_server("127.0.0.1", 11211)
client.add_server(("127.0.0.1", 11211))

test_client = client_patch.return_value
test_client.server = ("127.0.0.1", 11211)
Expand All @@ -332,7 +333,7 @@ def test_dead_server_comes_back(self, client_patch):
@mock.patch("pymemcache.client.hash.HashClient.client_class")
def test_failed_is_retried(self, client_patch):
client = HashClient([], retry_attempts=1, retry_timeout=0)
client.add_server("127.0.0.1", 11211)
client.add_server(("127.0.0.1", 11211))

assert client_patch.call_count == 1

Expand All @@ -354,7 +355,33 @@ class MyClient(Client):

client = HashClient([])
client.client_class = MyClient
client.add_server('host', 11211)
client.add_server(('host', 11211))
assert isinstance(client.clients['host:11211'], MyClient)

def test_mixed_inet_and_unix_sockets(self):
servers = [
'/tmp/pymemcache.{pid}'.format(pid=os.getpid()),
('127.0.0.1', 11211),
]
client = HashClient(servers)
assert set(servers) == {c.server for c in client.clients.values()}

def test_legacy_add_remove_server_signature(self):
server = ('127.0.0.1', 11211)
client = HashClient([])
assert client.clients == {}
client.add_server(*server) # Unpack (host, port) tuple.
assert ('%s:%s' % server) in client.clients
client._mark_failed_server(server)
assert server in client._failed_clients
client.remove_server(*server) # Unpack (host, port) tuple.
assert server in client._dead_clients
assert server not in client._failed_clients

# Ensure that server is a string if passing port argument:
with pytest.raises(TypeError):
client.add_server(server, server[-1])
with pytest.raises(TypeError):
client.remove_server(server, server[-1])

# TODO: Test failover logic

0 comments on commit acd962b

Please sign in to comment.