-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.py
253 lines (189 loc) · 6.32 KB
/
nats.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/python3
import json
import os
import socket
import threading
from uuid import uuid4
"""
The key-values for this are set at the end of each method handler
definition for the message types. See nats.io for the protocol spec.
"""
_op_handlers = {}
class NatsClient(object):
def __init__(self, *args, client_name=None):
self.subscriptions = {}
self.next_sid = 1
self.lock = threading.Lock()
self.server_info = {}
self.response_inbox = 0
if client_name is not None:
self.client_name = client_name
else:
self.client_name = str(uuid4())
def __del__(self):
self.s.close()
def _process_messages(self):
while True:
msg = _next_message(self)
if msg['op'].upper() == 'MSG':
if msg['sid'] in self.subscriptions:
self.subscriptions[msg['sid']](msg)
elif msg['op'].upper() == 'PING':
self._send_pong()
elif msg['op'].upper() == 'PONG':
self._send_ping()
elif msg['op'].upper() == 'INFO':
self.server_info = msg['info']
def _send_pong(self):
self.lock.acquire()
self.s.send(b'PONG\r\n')
self.lock.release()
def _send_ping(self):
self.lock.acquire()
self.s.send(b'PING\r\n')
self.lock.release()
def connect(self, server=None):
if server is None:
server = os.environ['NATS_SERVER']
server = server.replace("nats://", "")
host, port = server.split(":")
port = int(port)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port))
connect_params = {'verbose': False,
'pedantic': True,
'ssl_required': False,
'name': self.client_name,
'lang': 'en-us',
'version': '0.1'}
message = 'CONNECT {}\r\n'.format(json.dumps(connect_params)).encode('utf8')
self.s.send(message)
self.thread = threading.Thread(target=self._process_messages)
self.thread.daemon = True
self.thread.start()
def request(self, subject, payload, *args, cb=None):
if cb is None:
return
# Generate reply_to inbox
self.lock.acquire()
reply_to = "requests.{}.{}".format(self.client_name, self.response_inbox)
self.response_inbox += 1
self.lock.release()
# Subscribe to reply inbox
sid = self.subscribe(reply_to, cb=cb)
self.unsubscribe(sid, 1)
# Send message request
self.lock.acquire()
message = 'PUB {} {} {}\r\n'.format(subject, reply_to, len(payload)).encode('utf8')
message += payload
message += '\r\n'.encode('utf8')
self.s.send(message)
self.lock.release()
def publish(self, subject, payload):
self.lock.acquire()
message = 'PUB {} {}\r\n'.format(subject, len(payload)).encode('utf8')
message += payload
message += '\r\n'.encode('utf8')
self.s.send(message)
self.lock.release()
def subscribe(self, subject, queue_group=None, *args, cb=None):
if cb is None:
return
self.lock.acquire()
sid = self.next_sid
self.next_sid += 1
message = 'SUB {} '.format(subject).encode('utf8')
if queue_group is not None:
message += '{} '.format(queue_group).encode('utf8')
message += '{}\r\n'.format(sid).encode('utf8')
self.subscriptions[sid] = cb
self.s.send(message)
self.lock.release()
return sid
def unsubscribe(self, sid, max_messages=None):
self.lock.acquire()
message = "UNSUB {}".format(sid).encode('utf8')
if max_messages is not None:
message += " {}".format(max_messages).encode('utf8')
self.s.send(message + '\r\n'.encode('utf8'))
self.lock.release()
def _is_json(val):
try:
json.loads(val)
except:
return False
return True
def _read_word(nc):
word = ''
last_char = ''
while last_char != ' ' and last_char != '\r':
word += last_char
next_char = nc.s.recv(1)
last_char = next_char.decode('utf8')
return word.replace('\n', '').replace('\r', '').replace(' ', '')
def _read_quote(nc):
opening_char = ''.join(nc.s.recv(1))
payload = ''
last_char = ''
while last_char != opening_char:
payload += last_char
last_char = nc.s.recv(1).decode('utf8')
return opening_char + payload + opening_char
def _read_json(nc):
"""
This is easily the laziest piece of code in this library. In the future, I want
to modify this to be a simple CFG parser to identify valid JSON, but in the meantime,
performance wasn't my priority, and this gets the job done.
"""
json_str = ''
last_char = ''
while not _is_json(json_str):
json_str += last_char
last_char = nc.s.recv(1).decode('utf8')
return json_str
def _next_message(nc):
op = _read_word(nc)
return {'op': op,
**_op_handlers[op.upper()](nc)}
def _handle_info(nc):
result = json.loads(_read_json(nc))
nc.s.recv(2)
return {'info': result}
_op_handlers['INFO'] = _handle_info
def _handle_msg(nc):
subject = _read_word(nc)
sid = int(_read_word(nc))
reply_to = _read_word(nc)
if reply_to.isnumeric():
num_bytes = int(reply_to)
reply_to = None
else:
num_bytes = int(_read_word(nc))
nc.s.recv(1)
payload = []
while sum(map(lambda x: len(x), payload)) < num_bytes:
payload.append(nc.s.recv(num_bytes - sum(map(lambda x: len(x), payload))))
payload = b''.join(payload)
nc.s.recv(2)
return {'subject': subject,
'sid': sid,
'reply_to': reply_to,
'payload': payload}
_op_handlers['MSG'] = _handle_msg
def _handle_ping(nc):
nc.s.recv(1)
return {}
_op_handlers['PING'] = _handle_ping
def _handle_pong(nc):
nc.s.recv(1)
return {}
_op_handlers['PONG'] = _handle_pong
def _handle_ok(nc):
nc.s.recv(1)
return {}
_op_handlers['+OK'] = _handle_ok
def _handle_err(nc):
error = _read_quote(nc)
nc.s.recv(2)
return {'error': error[1:-1]}
_op_handlers['-ERR'] = _handle_err