The personal finance app for everyone (by everyone)
Ruby
Fix Twelve Data per-minute throttle never engaging on Redis (#4328)
hharkins7100428cfab3
diff --git a/app/models/provider/twelve_data.rb b/app/models/provider/twelve_data.rbindex ba332dfe5d..468fce5feb 100644--- a/app/models/provider/twelve_data.rb+++ b/app/models/provider/twelve_data.rb@@ -297,7 +297,9 @@ class Provider::TwelveData < Provider# charged to the minute the request actually fires in, not a stale minute# we slept through (which would undercount the new minute's usage).minute_key = "twelve_data:credits:#{Time.current.to_i / 60}"- current_count = Rails.cache.read(minute_key).to_i+ # `increment` stores a raw integer (Redis INCRBY), so it must be read back+ # with `raw: true`; a plain `read` returns nil on RedisCacheStore.+ current_count = Rails.cache.read(minute_key, raw: true).to_iif current_count + credits > max_requests_per_minutewait_seconds = 60 - (Time.current.to_i % 60) + 1diff --git a/test/models/provider/twelve_data_test.rb b/test/models/provider/twelve_data_test.rbindex b36dc64906..48ecb53c37 100644--- a/test/models/provider/twelve_data_test.rb+++ b/test/models/provider/twelve_data_test.rb@@ -236,4 +236,35 @@ class Provider::TwelveDataTest < ActiveSupport::TestCaseassert_not sleep_called, "Should not sleep when under credit limit"end++ # The tests above stub Rails.cache, and the test env uses a null store, so+ # they cannot notice a counter that is written but never read back. This one+ # runs against a real RedisCacheStore, the production cache store.+ test "throttle_request enforces the per-minute credit limit using a real Redis cache" do+ # A per-run namespace keeps the counter key unique, so concurrent or retried+ # runs sharing one Redis can never read each other's leftover counts.+ store = ActiveSupport::Cache::RedisCacheStore.new(+ url: ENV.fetch("REDIS_URL", "redis://localhost:6379"),+ namespace: "test:twelve_data:#{SecureRandom.hex(8)}"+ )+ Rails.stubs(:cache).returns(store)++ # Freeze 30s into a minute so the wait is predictable+ travel_to Time.utc(2026, 1, 1, 12, 0, 30) do+ minute_key = "twelve_data:credits:#{Time.current.to_i / 60}"++ sleeps = []+ @provider.define_singleton_method(:sleep) { |duration| sleeps << duration }++ with_env_overrides(TWELVE_DATA_MIN_REQUEST_INTERVAL: "0", TWELVE_DATA_MAX_REQUESTS_PER_MINUTE: "3") do+ 3.times { @provider.send(:throttle_request) }+ assert_empty sleeps, "Should not wait while under the per-minute limit"++ @provider.send(:throttle_request)+ assert_equal [ 31 ], sleeps, "Should wait for the next minute once the limit is reached"+ end+ ensure+ store.delete(minute_key)+ end+ endend