use LRUCache instead of pylru in wallet server

This commit is contained in:
Jack Robison 2020-12-23 17:45:05 -05:00
parent 10dcb64715
commit 13e38d6fd8
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
4 changed files with 11 additions and 14 deletions

View file

@ -215,7 +215,7 @@ class LRUCache:
'misses' 'misses'
] ]
def __init__(self, capacity: int, metric_name: typing.Optional[str] = None): def __init__(self, capacity: int, metric_name: typing.Optional[str] = None, namespace: str = "daemon_cache"):
self.capacity = capacity self.capacity = capacity
self.cache = collections.OrderedDict() self.cache = collections.OrderedDict()
if metric_name is None: if metric_name is None:
@ -225,10 +225,10 @@ class LRUCache:
self._track_metrics = True self._track_metrics = True
try: try:
self.hits = Counter( self.hits = Counter(
f"{metric_name}_cache_hit_count", "Number of cache hits", namespace="daemon_cache" f"{metric_name}_cache_hit_count", "Number of cache hits", namespace=namespace
) )
self.misses = Counter( self.misses = Counter(
f"{metric_name}_cache_miss_count", "Number of cache misses", namespace="daemon_cache" f"{metric_name}_cache_miss_count", "Number of cache misses", namespace=namespace
) )
except ValueError as err: except ValueError as err:
log.warning("failed to set up prometheus %s_cache_miss_count metric: %s", metric_name, err) log.warning("failed to set up prometheus %s_cache_miss_count metric: %s", metric_name, err)

View file

@ -3,11 +3,10 @@ import itertools
import json import json
import time import time
from functools import wraps from functools import wraps
from pylru import lrucache
import aiohttp import aiohttp
from prometheus_client import Gauge, Histogram from prometheus_client import Gauge, Histogram
from lbry.utils import LRUCache
from lbry.wallet.rpc.jsonrpc import RPCError from lbry.wallet.rpc.jsonrpc import RPCError
from lbry.wallet.server.util import hex_to_bytes, class_logger from lbry.wallet.server.util import hex_to_bytes, class_logger
from lbry.wallet.rpc import JSONRPC from lbry.wallet.rpc import JSONRPC
@ -55,8 +54,8 @@ class Daemon:
self._height = None self._height = None
self.available_rpcs = {} self.available_rpcs = {}
self.connector = aiohttp.TCPConnector() self.connector = aiohttp.TCPConnector()
self._block_hash_cache = lrucache(100000) self._block_hash_cache = LRUCache(100000)
self._block_cache = lrucache(10000) self._block_cache = LRUCache(10000)
async def close(self): async def close(self):
if self.connector: if self.connector:

View file

@ -15,7 +15,6 @@ import ast
import os import os
import time import time
import zlib import zlib
import pylru
import typing import typing
from typing import Optional, List, Tuple, Iterable from typing import Optional, List, Tuple, Iterable
from asyncio import sleep from asyncio import sleep
@ -25,7 +24,7 @@ from glob import glob
from struct import pack, unpack from struct import pack, unpack
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
import attr import attr
from lbry.utils import LRUCache
from lbry.wallet.server import util from lbry.wallet.server import util
from lbry.wallet.server.hash import hash_to_hex_str, HASHX_LEN from lbry.wallet.server.hash import hash_to_hex_str, HASHX_LEN
from lbry.wallet.server.merkle import Merkle, MerkleCache from lbry.wallet.server.merkle import Merkle, MerkleCache
@ -94,7 +93,7 @@ class LevelDB:
self.headers_db = None self.headers_db = None
self.tx_db = None self.tx_db = None
self._tx_and_merkle_cache = pylru.lrucache(100000) self._tx_and_merkle_cache = LRUCache(100000, metric_name='tx_and_merkle', namespace="wallet_server")
self.total_transactions = None self.total_transactions = None
async def _read_tx_counts(self): async def _read_tx_counts(self):

View file

@ -4,7 +4,6 @@ import math
import time import time
import json import json
import zlib import zlib
import pylru
import base64 import base64
import codecs import codecs
import typing import typing
@ -18,11 +17,11 @@ from collections import defaultdict
from functools import partial from functools import partial
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from pylru import lrucache
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
import lbry import lbry
from lbry.utils import LRUCache
from lbry.build_info import BUILD, COMMIT_HASH, DOCKER_TAG from lbry.build_info import BUILD, COMMIT_HASH, DOCKER_TAG
from lbry.wallet.server.block_processor import LBRYBlockProcessor from lbry.wallet.server.block_processor import LBRYBlockProcessor
from lbry.wallet.server.db.writer import LBRYLevelDB from lbry.wallet.server.db.writer import LBRYLevelDB
@ -811,8 +810,8 @@ class LBRYSessionManager(SessionManager):
if self.env.websocket_host is not None and self.env.websocket_port is not None: if self.env.websocket_host is not None and self.env.websocket_port is not None:
self.websocket = AdminWebSocket(self) self.websocket = AdminWebSocket(self)
self.search_cache = self.bp.search_cache self.search_cache = self.bp.search_cache
self.search_cache['search'] = lrucache(10000) self.search_cache['search'] = LRUCache(10000, metric_name='search', namespace=NAMESPACE)
self.search_cache['resolve'] = lrucache(10000) self.search_cache['resolve'] = LRUCache(10000, metric_name='resolve', namespace=NAMESPACE)
async def process_metrics(self): async def process_metrics(self):
while self.running: while self.running: