Skip to content

Commit

Permalink
feat: make all caching duration configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Feb 4, 2025
1 parent 006342b commit f1fc9e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/aleph/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def get_defaults():
"max_concurrency": 20,
},
},
"cache": {
"ttl": {
"total_aleph_messages": 120,
"eth_height": 600,
"metrics": 10,
},
},
},
"p2p": {
# Port used for HTTP communication between nodes.
Expand Down
13 changes: 8 additions & 5 deletions src/aleph/web/controllers/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

LOGGER = getLogger("WEB.metrics")

config = get_config()


def format_dict_for_prometheus(values: Dict) -> str:
"""Format a dict to a Prometheus tags string"""
Expand Down Expand Up @@ -101,8 +103,8 @@ class Metrics(DataClassJsonMixin):
)


# Cache Aleph messages count for 2 minutes
@cached(ttl=120)
# Cache Aleph messages count for 2 minutes by default
@cached(ttl=config.aleph.cache.ttl.total_aleph_messages)
async def fetch_reference_total_messages() -> Optional[int]:
"""Obtain the total number of Aleph messages from another node."""
LOGGER.debug("Fetching Aleph messages count")
Expand All @@ -124,8 +126,8 @@ async def fetch_reference_total_messages() -> Optional[int]:
return None


# Cache ETH height for 10 minutes
@cached(ttl=600)
# Cache ETH height for 10 minutes by default
@cached(ttl=config.aleph.cache.ttl.eth_height)
async def fetch_eth_height() -> Optional[int]:
"""Obtain the height of the Ethereum blockchain."""
LOGGER.debug("Fetching ETH height")
Expand All @@ -143,7 +145,8 @@ async def fetch_eth_height() -> Optional[int]:
return -1 # We got a boggus value!


@cached(ttl=10)
# Cache metrics for 10 seconds by default
@cached(ttl=config.aleph.cache.ttl.metrics)
async def get_metrics(session: DbSession, node_cache: NodeCache) -> Metrics:
sync_messages_reference_total = await fetch_reference_total_messages()
eth_reference_height = await fetch_eth_height()
Expand Down

0 comments on commit f1fc9e2

Please sign in to comment.