forked from LBRYCommunity/lbry-sdk
cache encoded headers
This commit is contained in:
parent
5d3704c7ea
commit
038a5f999f
2 changed files with 14 additions and 4 deletions
|
@ -12,6 +12,7 @@
|
|||
import asyncio
|
||||
import array
|
||||
import ast
|
||||
import base64
|
||||
import os
|
||||
import time
|
||||
import zlib
|
||||
|
@ -82,6 +83,7 @@ class LevelDB:
|
|||
self.utxo_db = None
|
||||
self.tx_counts = None
|
||||
self.headers = None
|
||||
self.encoded_headers = LRUCacheWithMetrics(1 << 21, metric_name='encoded_headers', namespace='wallet_server')
|
||||
self.last_flush = time.time()
|
||||
|
||||
self.logger.info(f'using {self.env.db_engine} for DB backend')
|
||||
|
@ -440,6 +442,16 @@ class LevelDB:
|
|||
raise IndexError(f'height {height:,d} out of range')
|
||||
return header
|
||||
|
||||
def encode_headers(self, start_height, count, headers):
|
||||
key = (start_height, count)
|
||||
if not self.encoded_headers.get(key):
|
||||
compressobj = zlib.compressobj(wbits=-15, level=1, memLevel=9)
|
||||
headers = base64.b64encode(compressobj.compress(headers) + compressobj.flush()).decode()
|
||||
if start_height % 1000 != 0:
|
||||
return headers
|
||||
self.encoded_headers[key] = headers
|
||||
return self.encoded_headers.get(key)
|
||||
|
||||
def read_headers(self, start_height, count) -> typing.Tuple[bytes, int]:
|
||||
"""Requires start_height >= 0, count >= 0. Reads as many headers as
|
||||
are available starting at start_height up to count. This
|
||||
|
|
|
@ -3,7 +3,6 @@ import ssl
|
|||
import math
|
||||
import time
|
||||
import json
|
||||
import zlib
|
||||
import base64
|
||||
import codecs
|
||||
import typing
|
||||
|
@ -16,7 +15,7 @@ from asyncio import Event, sleep
|
|||
from collections import defaultdict
|
||||
from functools import partial
|
||||
|
||||
from binascii import hexlify, unhexlify
|
||||
from binascii import hexlify
|
||||
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
|
||||
from prometheus_client import Counter, Info, Histogram, Gauge
|
||||
|
||||
|
@ -1345,8 +1344,7 @@ class LBRYElectrumX(SessionBase):
|
|||
headers, count = self.db.read_headers(start_height, count)
|
||||
|
||||
if b64:
|
||||
compressobj = zlib.compressobj(wbits=-15, level=1, memLevel=9)
|
||||
headers = base64.b64encode(compressobj.compress(headers) + compressobj.flush()).decode()
|
||||
headers = self.db.encode_headers(start_height, count, headers)
|
||||
else:
|
||||
headers = headers.hex()
|
||||
result = {
|
||||
|
|
Loading…
Reference in a new issue