Skip to content

Commit 7e59b05

Browse files
committed
Moving Ping to separate operation, so we have only one way to send requests
1 parent db8bcc3 commit 7e59b05

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/tarantool/connection.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,22 +284,24 @@ def update(self, space_name, key, op_list, return_tuple=False):
284284
return self._send_request(request, space_name)
285285

286286

287-
def ping(self, notime = False):
287+
def ping(self, notime=False):
288288
'''\
289289
Execute PING request.
290290
Send empty request and receive empty response from server.
291291
292292
:return: response time in seconds
293293
:rtype: float
294294
'''
295-
self._opt_reconnect()
295+
296+
request = RequestPing(self)
296297
t0 = time.time()
297-
self._socket.sendall(struct_LLL.pack(0xff00, 0, 0))
298-
request_type, body_length, request_id = struct_LLL.unpack(self._socket.recv(12)) # pylint: disable=W0612
298+
response = self._send_request(request)
299299
t1 = time.time()
300-
assert request_type == 0xff00
301-
assert body_length == 0
302-
if no_time:
300+
301+
assert response._request_type == REQUEST_TYPE_PING
302+
assert response._body_length == 0
303+
304+
if notime:
303305
return "Success"
304306
return t1 - t0
305307

src/tarantool/const.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121
struct_BQ = struct.Struct("<BQ")
2222

2323

24-
REQUEST_TYPE_CALL = 22
25-
REQUEST_TYPE_DELETE = 21
2624
REQUEST_TYPE_INSERT = 13
2725
REQUEST_TYPE_SELECT = 17
2826
REQUEST_TYPE_UPDATE = 19
27+
REQUEST_TYPE_DELETE = 21
28+
REQUEST_TYPE_CALL = 22
29+
REQUEST_TYPE_PING = 65280
2930

3031

3132
UPDATE_OPERATION_CODE = {'=': 0, '+': 1, '&': 2, '^': 3, '|': 4, 'splice': 5}
3233

3334
# Default value for socket timeout (seconds)
34-
SOCKET_TIMEOUT = 1
35+
SOCKET_TIMEOUT = None
3536
# Default maximum number of attempts to reconnect
3637
RECONNECT_MAX_ATTEMPTS = 10
3738
# Default delay between attempts to reconnect (seconds)

src/tarantool/request.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,15 @@ def __init__(self, conn, proc_name, args, return_tuple): # pylint: disable=W0
291291
self.pack_tuple([k for k in args])
292292

293293
self._bytes = self.header(len(request_body)) + request_body
294+
295+
class RequestPing(Request):
296+
'''
297+
Ping body is empty, so body_length == 0 and there's no body
298+
|--------------- header ----------------|
299+
<request_type><body_length><request_id>
300+
'''
301+
request_typle = REQUEST_TYPE_PING
302+
303+
def __init__(self, conn, notime):
304+
super(RequestPing, self).__init__(conn)
305+
self._bytes = self.header(0)

0 commit comments

Comments
 (0)