Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for connecting via unix socket #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions aiomcache/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ def wrapper(self, *args, **kwargs):


class Client(object):

def __init__(self, host, port=11211, *,
pool_size=2, pool_minsize=None, loop=None):
def __init__(self, host='127.0.0.1', port=11211, *, pool_size=2, pool_minsize=None, loop=None, unix='/tmp/memcached.sock'):
if not pool_minsize:
pool_minsize = pool_size
self._pool = MemcachePool(
host, port, minsize=pool_minsize, maxsize=pool_size, loop=loop)
self._pool = MemcachePool(host, port, minsize=pool_minsize, maxsize=pool_size, loop=loop, unix=unix)

# key supports ascii sans space and control chars
# \x21 is !, right after space, and \x7e is -, right before DEL
Expand Down
11 changes: 8 additions & 3 deletions aiomcache/pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import os.path
from collections import namedtuple

__all__ = ['MemcachePool']
Expand All @@ -9,7 +10,7 @@

class MemcachePool:

def __init__(self, host, port, *, minsize, maxsize, loop=None):
def __init__(self, host, port, *, minsize, maxsize, loop=None, unix=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should accept host/port or unix. Currently a unix connection requires the host/port even though it is unused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, we could try unix first, and then fallback to host/port if the connection fails. In which case, we should support host/port as None to disable this fallback behaviour and always get an error if the unix connection fails.

loop = loop if loop is not None else asyncio.get_event_loop()
self._host = host
self._port = port
Expand All @@ -18,6 +19,7 @@ def __init__(self, host, port, *, minsize, maxsize, loop=None):
self._loop = loop
self._pool = asyncio.Queue(loop=loop)
self._in_use = set()
self._unix = unix

@asyncio.coroutine
def clear(self):
Expand Down Expand Up @@ -69,8 +71,11 @@ def release(self, conn):
@asyncio.coroutine
def _create_new_conn(self):
if self.size() < self._maxsize:
reader, writer = yield from asyncio.open_connection(
self._host, self._port, loop=self._loop)
if isinstance(self._unix, str) and os.path.exists(self._unix):
reader, writer = yield from asyncio.open_unix_connection(path=self._unix, loop=self._loop)
else:
reader, writer = yield from asyncio.open_connection(self._host, self._port, loop=self._loop)

if self.size() < self._maxsize:
return _connection(reader, writer)
else:
Expand Down