From b9b7411d88073fc5a20103af882de3d96242d3e9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 18 Nov 2018 20:13:39 +1030 Subject: [PATCH] lightning.py: use double-\n as marker for RPC parsing. This doesn't make a performance difference, but even better, it simplifies the code. We hacked test_multirpc to send 200x as many commands, and timed the pytest over 20 runs: Before: =================== 1 passed, 136 deselected in 8.550000-9.400000(9.0045+/-0.2) seconds =================== After: =================== 1 passed, 136 deselected in 8.540000-9.370000(8.97286+/-0.16) seconds =================== Signed-off-by: Rusty Russell --- contrib/pylightning/lightning/lightning.py | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/contrib/pylightning/lightning/lightning.py b/contrib/pylightning/lightning/lightning.py index 06f334cd0..9d1686c3d 100644 --- a/contrib/pylightning/lightning/lightning.py +++ b/contrib/pylightning/lightning/lightning.py @@ -28,19 +28,17 @@ class UnixDomainSocketRpc(object): def _readobj(self, sock, buff=b''): """Read a JSON object, starting with buff; returns object and any buffer left over""" while True: - try: - # Convert late to UTF-8 so glyphs split across recvs do not - # impact us - objs, len_used = self.decoder.raw_decode(buff.decode("UTF-8")) - return objs, buff[len_used:].lstrip() - except ValueError: - # Probably didn't read enough - pass - - b = sock.recv(1024) - buff += b - if len(b) == 0: - return {'error': 'Connection to RPC server lost.'}, buff.lstrip() + parts = buff.split(b'\n\n', 1) + if len(parts) == 1: + # Didn't read enough. + b = sock.recv(1024) + buff += b + if len(b) == 0: + return {'error': 'Connection to RPC server lost.'}, buff + else: + buff = parts[1] + obj, _ = self.decoder.raw_decode(parts[0].decode("UTF-8")) + return obj, buff def __getattr__(self, name): """Intercept any call that is not explicitly defined and call @call