Browse Source

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 <rusty@rustcorp.com.au>
trytravis
Rusty Russell 6 years ago
committed by Christian Decker
parent
commit
b9b7411d88
  1. 18
      contrib/pylightning/lightning/lightning.py

18
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
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.lstrip()
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

Loading…
Cancel
Save