Skip to content

Commit

Permalink
perf(recipe): Give TreeCache standalone queue
Browse files Browse the repository at this point in the history
This commit lets TreeCache do not use queue of connection routine any more.
  • Loading branch information
tonyseek authored and jeffwidman committed Mar 23, 2018
1 parent cbd02f5 commit 4456f18
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion kazoo/recipe/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TreeCache(object):
STATE_STARTED = 1
STATE_CLOSED = 2

_STOP = object()

def __init__(self, client, path):
self._client = client
self._root = TreeNode.make_root(self, path)
Expand All @@ -44,6 +46,8 @@ def __init__(self, client, path):
self._is_initialized = False
self._error_listeners = []
self._event_listeners = []
self._task_queue = client.handler.queue_impl()
self._task_thread = None

def start(self):
"""Starts the cache.
Expand All @@ -66,6 +70,7 @@ def start(self):
else:
raise KazooException('already started')

self._task_thread = self._client.handler.spawn(self._do_background)
self._client.add_listener(self._session_watcher)
self._client.ensure_path(self._root._path)

Expand All @@ -87,6 +92,7 @@ def close(self):
"""
if self._state == self.STATE_STARTED:
self._state = self.STATE_CLOSED
self._task_queue.put(self._STOP)
self._client.remove_listener(self._session_watcher)
with handle_exception(self._error_listeners):
self._root.on_deleted()
Expand Down Expand Up @@ -168,7 +174,16 @@ def _do_publish_event(self, event):
listener(event)

def _in_background(self, func, *args, **kwargs):
self._client.handler.callback_queue.put(lambda: func(*args, **kwargs))
self._task_queue.put((func, args, kwargs))

def _do_background(self):
while True:
with handle_exception(self._error_listeners):
cb = self._task_queue.get()
if cb is self._STOP:
break
func, args, kwargs = cb
func(*args, **kwargs)

def _session_watcher(self, state):
if state == KazooState.SUSPENDED:
Expand Down

0 comments on commit 4456f18

Please sign in to comment.