Skip to content

Commit 3cc2f8a

Browse files
committed
Check #reconnect_can_restore_state? once, before running block
The block can't affect reconnectability, because we'll be re-running it anyway. Also, only #reconnect! once per query, to avoid squaring the total number of permitted retries while attempting to query a pathologically flakey server.
1 parent 02f5de1 commit 3cc2f8a

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,11 +867,12 @@ def with_raw_connection(allow_retry: false, uses_transaction: true)
867867
materialize_transactions if uses_transaction
868868

869869
retries_available = allow_retry ? connection_retries : 0
870+
reconnectable = reconnect_can_restore_state?
870871

871872
if @verified
872873
# Cool, we're confident the connection's ready to use. (Note this might have
873874
# become true during the above #materialize_transactions.)
874-
elsif reconnect_can_restore_state?
875+
elsif reconnectable
875876
if allow_retry
876877
# Not sure about the connection yet, but if anything goes wrong we can
877878
# just reconnect and re-run our query
@@ -899,9 +900,11 @@ def with_raw_connection(allow_retry: false, uses_transaction: true)
899900
if retryable_query_error?(translated_exception)
900901
backoff(connection_retries - retries_available)
901902
retry
902-
elsif retryable_connection_error?(translated_exception) &&
903-
reconnect_can_restore_state?
903+
elsif reconnectable && retryable_connection_error?(translated_exception)
904904
reconnect!(restore_transactions: true)
905+
# Only allowed to reconnect once, because reconnect! has its own retry
906+
# loop
907+
reconnectable = false
905908
retry
906909
end
907910
end

0 commit comments

Comments
 (0)