Skip to content

Commit

Permalink
Code review clean ups and doc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek committed Jul 2, 2015
1 parent f43cf74 commit a935261
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
7 changes: 4 additions & 3 deletions docs/apidoc/pymemcache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ Subpackages

.. toctree::

pymemcache.client
pymemcache.test

Submodules
----------

pymemcache.client module
------------------------
pymemcache.exceptions module
----------------------------

.. automodule:: pymemcache.client
.. automodule:: pymemcache.exceptions
:members:
:undoc-members:
:show-inheritance:
Expand Down
22 changes: 19 additions & 3 deletions docs/apidoc/pymemcache.test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pymemcache.test.benchmark module
:undoc-members:
:show-inheritance:

pymemcache.test.integration module
----------------------------------
pymemcache.test.conftest module
-------------------------------

.. automodule:: pymemcache.test.integration
.. automodule:: pymemcache.test.conftest
:members:
:undoc-members:
:show-inheritance:
Expand All @@ -28,6 +28,22 @@ pymemcache.test.test_client module
:undoc-members:
:show-inheritance:

pymemcache.test.test_client_hash module
---------------------------------------

.. automodule:: pymemcache.test.test_client_hash
:members:
:undoc-members:
:show-inheritance:

pymemcache.test.test_integration module
---------------------------------------

.. automodule:: pymemcache.test.test_integration
:members:
:undoc-members:
:show-inheritance:

pymemcache.test.test_utils module
---------------------------------

Expand Down
11 changes: 4 additions & 7 deletions pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class Client(object):
"""
A client for a single memcached server.
Keys and Values
----------------
*Keys and Values*
Keys must have a __str__() method which should return a str with no more
than 250 ASCII characters and no whitespace or control characters. Unicode
Expand All @@ -106,8 +105,7 @@ class Client(object):
already implemented serializers, including one that is compatible with
the python-memcache library.
Serialization and Deserialization
----------------------------------
*Serialization and Deserialization*
The constructor takes two optional functions, one for "serialization" of
values, and one for "deserialization". The serialization function takes
Expand All @@ -134,8 +132,7 @@ def deserialize_json(key, value, flags):
raise Exception("Unknown flags for value: {1}".format(flags))
Error Handling
---------------
*Error Handling*
All of the methods in this class that talk to memcached can throw one of
the following exceptions:
Expand Down Expand Up @@ -650,7 +647,7 @@ def _fetch_cmd(self, name, keys, expect_cas):
result[key] = value
else:
raise MemcacheUnknownError(line[:32])
except Exception as e:
except Exception:
self.close()
if self.ignore_exc:
return {}
Expand Down
41 changes: 22 additions & 19 deletions pymemcache/client/hash.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import socket
import time
import logging

from pymemcache.client.base import Client, PooledClient, _check_key
from clandestined import RendezvousHash as RH

logger = logging.getLogger(__name__)


class RendezvousHash(RH):
def get_node(self, key):
Expand All @@ -18,6 +22,7 @@ def __init__(
servers,
hasher=None,
serializer=None,
deserializer=None,
connect_timeout=None,
timeout=None,
no_delay=False,
Expand All @@ -31,10 +36,10 @@ def __init__(
use_pooling=False,
):
"""
Constructor.
Args:
servers: list(tuple(hostname, port))
serializer: optional class with ``serialize`` and ``deserialize``
functions.
hasher: optional class three functions ``get_node``, ``add_node``,
and ``remove_node``
defaults to Rendezvous (HRW) hash.
Expand All @@ -45,10 +50,10 @@ def __init__(
retry_attempts: Amount of times a client should be tried before it
is marked dead and removed from the pool.
retry_timeout: Time in seconds that should pass between retry
attempts.
dead_timeout: Time in seconds before attempting to add a node back in
the pool.
retry_timeout (float): Time in seconds that should pass between retry
attempts.
dead_timeout (float): Time in seconds before attempting to add a node
back in the pool.
Further arguments are interpreted as for :py:class:`.Client`
constructor.
Expand All @@ -72,14 +77,10 @@ def __init__(
'no_delay': no_delay,
'socket_module': socket_module,
'key_prefix': key_prefix,
'serializer': serializer,
'deserializer': deserializer,
}

if serializer is not None:
self.default.kwargs.update({
'serializer': serializer.serialize,
'deserializer': serializer.deserialize,
})

if use_pooling is True:
self.default_kwargs.update({
'max_pool_size': max_pool_size,
Expand Down Expand Up @@ -120,12 +121,14 @@ def _get_client(self, key):
if current_time - ldc > self.dead_timeout:
for server, dead_time in self._dead_clients.items():
if current_time - dead_time > self.dead_timeout:
print('bringing server back in rotation')
logger.debug(
'bringing server back into rotation %s',
server
)
self.add_server(*server)
self._last_dead_check_time = current_time

server = self.hasher.get_node(key)
print('got server %s' % server)
client = self.clients[server]
return client

Expand All @@ -141,8 +144,9 @@ def _safely_run_func(self, client, func, *args, **kwargs):
if failed_metadata['attempts'] < self.retry_attempts:
failed_time = failed_metadata['failed_time']
if time.time() - failed_time > self.retry_timeout:
print(failed_metadata)
print('retrying')
logger.debug(
'retrying failed server: %s', client.server
)
result = func(*args, **kwargs)
# we were successful, lets remove it from the failed
# clients
Expand All @@ -152,7 +156,7 @@ def _safely_run_func(self, client, func, *args, **kwargs):
else:
# We've reached our max retry attempts, we need to mark
# the sever as dead
print('marking as dead')
logger.debug('marking server as dead: %s', client.server)
self.remove_server(*client.server)

result = func(*args, **kwargs)
Expand Down Expand Up @@ -180,12 +184,11 @@ def _safely_run_func(self, client, func, *args, **kwargs):
'failed_time': time.time(),
'attempts': 0,
}
print("marking node as dead %s" % client.server)
logger.debug("marking server as dead %s" % client.server)
self.remove_server(*client.server)
# This client has failed previously, we need to update the metadata
# to reflect that we have attempted it again
else:
print('incrementing')
failed_metadata = self._failed_clients[client.server]
failed_metadata['attempts'] += 1
failed_metadata['failed_time'] = time.time()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ commands =

[testenv:docs]
commands =
pip install -r docs-requirements.txt
pip install -r docs-requirements.txt -r test-requirements.txt
sphinx-apidoc -o docs/apidoc/ pymemcache
sphinx-build -b html docs/ docs/_build

0 comments on commit a935261

Please sign in to comment.