Skip to content

Fix #447: Consolidate CoinGecko API calls and cache prices - #450

Merged
NovaCode37 merged 4 commits into
NovaCode37:mainfrom
marioalbu08:fix/crypto-coingecko
Oct 1, 2026
Merged

NovaCode37 merged 4 commits into
NovaCode37:mainfrom
marioalbu08:fix/crypto-coingecko

Conversation

@marioalbu08

Copy link
Copy Markdown
Contributor

Summary

Resolves #447 by consolidating the three separate CoinGecko API calls into a single request and implementing a class-level cache to prevent rate-limiting during bulk scans.

Changes

  • Replaced _btc_price, _eth_price, and _ltc_price with a single _fetch_prices() function that requests ids=bitcoin,ethereum,litecoin in one go.
  • Implemented CryptoLookup._prices_cache and CryptoLookup._prices_error class variables to ensure prices are only fetched once per run, regardless of how many CryptoLookup instances are initialized.
  • Updated lookup_bitcoin, lookup_ethereum, and lookup_litecoin to gracefully handle rate limits (HTTP 429) by appending the failure reason to the new price_unavailable field in the result output.
  • Added a reset_cache fixture and a test_single_price_request_and_429 test to tests/test_modules_extended.py to prove the rate-limit handling and verify that only 1 network request is made.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

  • I have tested these changes locally
  • I have added/updated tests as needed

Screenshots

N/A

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Thanks for the first pull request here. CI needs a maintainer to approve the run before it starts, so it may sit for a bit before anything happens. pytest tests/ -q passing is the main thing I look at.

@github-actions github-actions Bot added the python Pull requests that update python code label Oct 1, 2026
@NovaCode37

Copy link
Copy Markdown
Owner

Thanks, one request instead of three is exactly the point, and price_unavailable with the reason is what the issue asked for. One real bug and some trimming:

The lock does not cover the request. After with CryptoLookup._prices_lock: only the double-check is inside the block. The try: that calls CoinGecko is dedented back to the method level, so it runs after the lock is released, and two concurrent scans still fire two requests. Either indent the fetch into the with, or drop the lock: the issue only asked for one request per run, and a duplicate request in a race is harmless.

Smaller:

  • import threading inside the class body makes threading a class attribute. Move it, and import time, to the top of the module with the other imports.
  • Please drop the inline comment; this codebase does not use them.
  • A one-hour, process-wide price cache is a bigger change than the issue describes. It is fine to keep, but say so in the PR description so it is a decision, not a side effect.

The tests look good; keep the one that checks a single request for BTC plus ETH.

@NovaCode37
NovaCode37 merged commit 2bafcdb into NovaCode37:main Oct 1, 2026
8 checks passed
@NovaCode37 NovaCode37 added the hacktoberfest-accepted Counts toward Hacktoberfest label Oct 1, 2026
@marioalbu08
marioalbu08 deleted the fix/crypto-coingecko branch October 1, 2026 17:14
@marioalbu08
marioalbu08 restored the fix/crypto-coingecko branch October 1, 2026 17:14
@marioalbu08
marioalbu08 deleted the fix/crypto-coingecko branch October 1, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest-accepted Counts toward Hacktoberfest python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crypto_lookup: one CoinGecko request instead of three copies of the same function

2 participants