-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
keystore.py
92 lines (84 loc) · 2.91 KB
/
keystore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gevent
import gevent.event
import gevent.monkey
gevent.monkey.patch_socket()
import gevent.queue
import storestub
def _shared_hack():
# hack to get around circular import
import shared
global app
app = shared.app
def make_redis():
_shared_hack()
host = app.config["REDIS_HOST"]
port = app.config.get("REDIS_PORT") or 6379
redis = __import__("redis")
return redis.Redis(host=host, port=port, db=0, decode_responses=True)
class Keystore:
def __init__(self):
_shared_hack()
self._backend = app.config.get("STORE_PROVIDER") or "INTERNAL"
if self._backend == "REDIS":
self._client = make_redis()
else:
self._client = storestub.KeystoreClient()
self._client.connect()
def get(self, key):
return self._client.get(key)
def exists(self, key):
if self._backend == "REDIS":
return self._client.exists(key) != 0
else:
return self._client.get(key) != None
def set(self, key, value):
self._client.set(key, value)
def delete(self, key):
self._client.delete(key)
class Pubsub:
def __init__(self):
self._backend = app.config.get("STORE_PROVIDER") or "INTERNAL"
if self._backend == "REDIS":
self._redis_client = make_redis()
self._pubsub_client = self._redis_client.pubsub(ignore_subscribe_messages=True)
self._channels = {}
self._pump_running = False
else:
self._client = storestub.PubsubClient()
self._client.connect()
def subscribe(self, channel):
if self._backend == "REDIS":
self._pubsub_client.subscribe(channel)
else:
self._client.subscribe(channel)
def unsubscribe(self, channel):
if self._backend == "REDIS":
self._pubsub_client.unsubscribe(channel)
else:
self._client.unsubscribe(channel)
def publish(self, channel, message):
if self._backend == "REDIS":
self._redis_client.publish(channel, message)
else:
self._client.publish(channel, message)
def get_message(self, channel):
if self._backend == "REDIS":
self._ensure_pump()
self._ensure_channel(channel)
return self._channels[channel].get()
else:
return self._client.get_message(channel)
def _ensure_pump(self):
if self._pump_running:
return
self._pump_running = True
gevent.spawn(self._redis_pump)
def _redis_pump(self):
for response in self._pubsub_client.listen():
channel = response["channel"]
message = response["data"]
self._ensure_channel(channel)
self._channels[channel].put(message)
def _ensure_channel(self, channel):
if channel not in self._channels:
self._channels[channel] = gevent.queue.Queue()