cache encoded headers

This commit is contained in:
Victor Shyba 2021-02-04 19:44:18 -03:00
parent 5d3704c7ea
commit 038a5f999f
2 changed files with 14 additions and 4 deletions

View file

@ -12,6 +12,7 @@
import asyncio import asyncio
import array import array
import ast import ast
import base64
import os import os
import time import time
import zlib import zlib
@ -82,6 +83,7 @@ class LevelDB:
self.utxo_db = None self.utxo_db = None
self.tx_counts = None self.tx_counts = None
self.headers = None self.headers = None
self.encoded_headers = LRUCacheWithMetrics(1 << 21, metric_name='encoded_headers', namespace='wallet_server')
self.last_flush = time.time() self.last_flush = time.time()
self.logger.info(f'using {self.env.db_engine} for DB backend') 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') raise IndexError(f'height {height:,d} out of range')
return header 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]: def read_headers(self, start_height, count) -> typing.Tuple[bytes, int]:
"""Requires start_height >= 0, count >= 0. Reads as many headers as """Requires start_height >= 0, count >= 0. Reads as many headers as
are available starting at start_height up to count. This are available starting at start_height up to count. This

View file

@ -3,7 +3,6 @@ import ssl
import math import math
import time import time
import json import json
import zlib
import base64 import base64
import codecs import codecs
import typing import typing
@ -16,7 +15,7 @@ from asyncio import Event, sleep
from collections import defaultdict from collections import defaultdict
from functools import partial from functools import partial
from binascii import hexlify, unhexlify from binascii import hexlify
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from prometheus_client import Counter, Info, Histogram, Gauge from prometheus_client import Counter, Info, Histogram, Gauge
@ -1345,8 +1344,7 @@ class LBRYElectrumX(SessionBase):
headers, count = self.db.read_headers(start_height, count) headers, count = self.db.read_headers(start_height, count)
if b64: if b64:
compressobj = zlib.compressobj(wbits=-15, level=1, memLevel=9) headers = self.db.encode_headers(start_height, count, headers)
headers = base64.b64encode(compressobj.compress(headers) + compressobj.flush()).decode()
else: else:
headers = headers.hex() headers = headers.hex()
result = { result = {