Created
July 14, 2016 00:56
-
-
Save 72squared/5ca619eaa404cc0c170cfe67ef7731bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent | |
__all__ = ['RedisReplay'] | |
class _RedisReplayPipeline(object): | |
def __init__(self, primary_pipe, replay_pipe): | |
self._primary_pipe = primary_pipe | |
self._replay_pipe = replay_pipe | |
def __getattr__(self, item): | |
f = getattr(self._primary, item) | |
if not callable(f): | |
return f | |
r = getattr(self._replay, item) | |
if item == 'execute': | |
def inner(*args, **kwargs): | |
g = gevent.Greenlet.spawn(r, *args, **kwargs) | |
res = f(*args, **kwargs) | |
g.join(0.2) | |
return res | |
else: | |
def inner(*args, **kwargs): | |
r(*args, **kwargs) | |
res = f(*args, **kwargs) | |
return res | |
return inner | |
class RedisReplay(object): | |
def __init__(self, primary, replay): | |
self._primary = primary | |
self._replay = replay | |
def __getattr__(self, item): | |
f = getattr(self._primary, item) | |
if not callable(f): | |
return f | |
r = getattr(self._replay, item) | |
def inner(*args, **kwargs): | |
g = gevent.Greenlet.spawn(r, *args, **kwargs) | |
res = f(*args, **kwargs) | |
g.join(timeout=0.2) | |
return res | |
return inner | |
def pipeline(self, transaction=True, shard_hint=None): | |
return _RedisReplayPipeline( | |
self._primary.pipeline(transaction=transaction, | |
shard_hint=shard_hint), | |
self._replay.pipeline(transaction=transaction, | |
shard_hint=shard_hint) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment