Limit minute poller to last 5 candles after initial fetch

Reduces unnecessary upserts on each 15-second poll cycle by only
processing the 5 most recent candles from Kraken's response.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 13:09:24 +01:00
co-authored by Claude Opus 4.6
parent b49e8afd59
commit f27c198634
@@ -55,13 +55,14 @@
(defn- poll! (defn- poll!
"Fetch OHLC data including the last in-progress candle, upsert all. "Fetch OHLC data including the last in-progress candle, upsert all.
On initial fetch saves everything; subsequent fetches only take last 5.
Returns the count of saved candles." Returns the count of saved candles."
[client query-fn since-atom] [client query-fn since-atom initial?]
(let [result (fetch-ohlc client @since-atom) (let [result (fetch-ohlc client @since-atom)
last-ts (:last result) last-ts (:last result)
pair-key (first (remove #{:last} (keys result))) pair-key (first (remove #{:last} (keys result)))
raw (get result pair-key) raw (get result pair-key)
candles (map parse-candle raw)] candles (map parse-candle (if initial? raw (take-last 5 raw)))]
(when (seq candles) (when (seq candles)
(save-candles! query-fn candles) (save-candles! query-fn candles)
(when last-ts (when last-ts
@@ -74,14 +75,14 @@
[client query-fn since-atom running?] [client query-fn since-atom running?]
(future (future
(try (try
(poll! client query-fn since-atom) (poll! client query-fn since-atom true)
(catch Exception e (catch Exception e
(log/error e "Kraken minute initial poll failed"))) (log/error e "Kraken minute initial poll failed")))
(while @running? (while @running?
(Thread/sleep 15000) (Thread/sleep 15000)
(when @running? (when @running?
(try (try
(poll! client query-fn since-atom) (poll! client query-fn since-atom false)
(catch Exception e (catch Exception e
(log/error e "Kraken minute poll failed"))))))) (log/error e "Kraken minute poll failed")))))))