Commit 14ffbc3
Optimize batching for CoinGecko (#4215)
# Description
I have run a bunch of experiment goal of which was to make CoinGecko
batches bigger (fetch price for more tokens at once) to save on costs.
In a previous experiment I increased `concurrent_requests` on the
`CachingNativePriceEstimator` to 500 and that totally worked and
CoinGecko debounces all these requests nicely, but also means we would
be spamming solvers with a lot of requests at once when we start the
service.
The first issue at hand is that before this PR we would create futures
that would hit the cache and return immediately, which was bad, because
they take up a slot in the queue when using buffered() so fewer useful
futures that actually issue a native price request would get to run. I
changed this to buffered_unordered() and that helped a little bit, but
then the queue gets full and after that new futures get in only as
previous futures finishe, so the execution is spread out in time and our
CoinGecko debouncing can't gather enough tokens to form a full batch.
This PR introduces a solution where we omit non-expired tokens
altogether which solves the issue of futures that hit the cache taking
up a spot in the queue _and_ we just run batches of 19 sequentially. We
wait for each batch to finish (max ~3s, limited by QUERY_TIMEOUT) and
only then issues a new batch. As a tradeoff we get a slower cache warm
up/refresh, but we get to save a lot of money on CoinGecko.
In addition to this change I set the refresh rate to 30s from 1s, so we
can gather more expired tokens at once.
<img width="2103" height="480" alt="Screenshot 2026-03-02 at 11 30 33"
src="https://github.com/user-attachments/assets/190bbefe-ac26-4837-b2c2-05a7c63ebbcb"
/>
# Changes
* [x] Don't try to fetch non-expired prices
* [x] Run batches sequentially
* [x] NATIVE_PRICE_CACHE_REFRESH set to 30s instead of 1s in service
config
---------
Co-authored-by: Martin Magnus <martin.beckmann@protonmail.com>1 parent cb58b60 commit 14ffbc3
1 file changed
Lines changed: 32 additions & 7 deletions
File tree
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
485 | 485 | | |
486 | 486 | | |
487 | 487 | | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
494 | 519 | | |
495 | 520 | | |
496 | | - | |
| 521 | + | |
497 | 522 | | |
498 | 523 | | |
499 | 524 | | |
| |||
0 commit comments