Get history and UTXOs in executor for minimal latency.
This commit is contained in:
parent
4bc35609e3
commit
9972379533
@ -283,21 +283,20 @@ class ServerManager(util.LoggedClass):
|
|||||||
return header
|
return header
|
||||||
|
|
||||||
async def async_get_history(self, hash168):
|
async def async_get_history(self, hash168):
|
||||||
|
'''Get history asynchronously to reduce latency.'''
|
||||||
if hash168 in self.history_cache:
|
if hash168 in self.history_cache:
|
||||||
return self.history_cache[hash168]
|
return self.history_cache[hash168]
|
||||||
|
|
||||||
# History DoS limit. Each element of history is about 99
|
def job():
|
||||||
# bytes when encoded as JSON. This limits resource usage on
|
# History DoS limit. Each element of history is about 99
|
||||||
# bloated history requests, and uses a smaller divisor so
|
# bytes when encoded as JSON. This limits resource usage
|
||||||
# large requests are logged before refusing them.
|
# on bloated history requests, and uses a smaller divisor
|
||||||
limit = self.env.max_send // 97
|
# so large requests are logged before refusing them.
|
||||||
# Python 3.6: use async generators; update callers
|
limit = self.env.max_send // 97
|
||||||
history = []
|
return list(self.bp.get_history(hash168, limit=limit))
|
||||||
for item in self.bp.get_history(hash168, limit=limit):
|
|
||||||
history.append(item)
|
|
||||||
if len(history) % 100 == 0:
|
|
||||||
await asyncio.sleep(0)
|
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
history = await loop.run_in_executor(None, job)
|
||||||
self.history_cache[hash168] = history
|
self.history_cache[hash168] = history
|
||||||
return history
|
return history
|
||||||
|
|
||||||
@ -814,13 +813,11 @@ class ElectrumX(Session):
|
|||||||
return self.bp.read_headers(start_height, count).hex()
|
return self.bp.read_headers(start_height, count).hex()
|
||||||
|
|
||||||
async def get_utxos(self, hash168):
|
async def get_utxos(self, hash168):
|
||||||
# Python 3.6: use async generators; update callers
|
'''Get UTXOs asynchronously to reduce latency.'''
|
||||||
utxos = []
|
def job():
|
||||||
for utxo in self.bp.get_utxos(hash168, limit=None):
|
return list(self.bp.get_utxos(hash168, limit=None))
|
||||||
utxos.append(utxo)
|
loop = asyncio.get_event_loop()
|
||||||
if len(utxos) % 25 == 0:
|
return await loop.run_in_executor(None, job)
|
||||||
await asyncio.sleep(0)
|
|
||||||
return utxos
|
|
||||||
|
|
||||||
async def get_balance(self, hash168):
|
async def get_balance(self, hash168):
|
||||||
utxos = await self.get_utxos(hash168)
|
utxos = await self.get_utxos(hash168)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user