You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
_btc_price,_eth_price, and_ltc_pricewith a single_fetch_prices()function that requestsids=bitcoin,ethereum,litecoinin one go.CryptoLookup._prices_cacheandCryptoLookup._prices_errorclass variables to ensure prices are only fetched once per run, regardless of how manyCryptoLookupinstances are initialized.lookup_bitcoin,lookup_ethereum, andlookup_litecointo gracefully handle rate limits (HTTP 429) by appending the failure reason to the newprice_unavailablefield in the result output.reset_cachefixture and atest_single_price_request_and_429test totests/test_modules_extended.pyto prove the rate-limit handling and verify that only 1 network request is made.Type of change
Testing
Screenshots
N/A