mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 06:41:44 +01:00
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>
This commit is contained in:
parent
b2378654d7
commit
b9b7411d88
1 changed files with 11 additions and 13 deletions
|
@ -28,19 +28,17 @@ class UnixDomainSocketRpc(object):
|
||||||
def _readobj(self, sock, buff=b''):
|
def _readobj(self, sock, buff=b''):
|
||||||
"""Read a JSON object, starting with buff; returns object and any buffer left over"""
|
"""Read a JSON object, starting with buff; returns object and any buffer left over"""
|
||||||
while True:
|
while True:
|
||||||
try:
|
parts = buff.split(b'\n\n', 1)
|
||||||
# Convert late to UTF-8 so glyphs split across recvs do not
|
if len(parts) == 1:
|
||||||
# impact us
|
# Didn't read enough.
|
||||||
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)
|
b = sock.recv(1024)
|
||||||
buff += b
|
buff += b
|
||||||
if len(b) == 0:
|
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):
|
def __getattr__(self, name):
|
||||||
"""Intercept any call that is not explicitly defined and call @call
|
"""Intercept any call that is not explicitly defined and call @call
|
||||||
|
|
Loading…
Add table
Reference in a new issue