@@ -391,17 +391,26 @@ def __incoming_protocol_msgbus__
391391 def determine_host
392392 raise ArgumentError , 'Block required' unless block_given?
393393
394+ @determine_host_call_count ||= 0
395+ @determine_host_call_count += 1
396+
397+ if @determine_host_call_count == 3
398+ logger . debug { "LAWRENCE: On #{ @determine_host_call_count } call to determine_host" }
399+ end
400+
394401 if should_use_fallback_hosts?
395402 internet_up? do |internet_is_up_result |
396403 @current_host = if internet_is_up_result
397404 client . fallback_endpoint . host
398405 else
399406 client . endpoint . host
400407 end
408+ logger . debug { "LAWRENCE: current_host from fallbacks: #{ current_host } " }
401409 yield current_host
402410 end
403411 else
404412 @current_host = client . endpoint . host
413+ logger . debug { "LAWRENCE: current_host from not fallbacks: #{ current_host } " }
405414 yield current_host
406415 end
407416 end
@@ -674,6 +683,17 @@ def custom_host?
674683 end
675684
676685 def should_use_fallback_hosts?
686+ logger . debug { "LAWRENCE: should_use_fallback_hosts? #{ state_history . inspect } " }
687+
688+ # Don't use fallback hosts if the last state change was due to a token error
689+ # if state_history.any?
690+ # last_state_change = state_history.last
691+ # if last_state_change[:reason].respond_to?(:code) && last_state_change[:reason].code
692+ # return false if last_state_change[:reason].code >= 40140 && last_state_change[:reason].code < 40150
693+ # end
694+ # end
695+
696+ # This logic is utterly bizarre. It pays no attention to the error that caused the disconnection.
677697 if client . fallback_hosts && !client . fallback_hosts . empty?
678698 if connecting? && previous_state && !disconnected_from_connected_state?
679699 use_fallback_if_disconnected? || use_fallback_if_suspended?
@@ -690,7 +710,24 @@ def disconnected_from_connected_state?
690710 end
691711
692712 def use_fallback_if_disconnected?
693- second_reconnect_attempt_for ( :disconnected , 1 )
713+ unless second_reconnect_attempt_for ( :disconnected , 1 )
714+ return false
715+ end
716+
717+ last_state_change = state_history . last
718+ logger . debug { "LAWRENCE: last_state_change in disconnected thing: #{ last_state_change . inspect } with reason #{ last_state_change [ :metadata ] . reason } " }
719+ logger . debug { "LAWRENCE: last_disconnected_reason: #{ last_disconnected_reason . inspect } " }
720+
721+ does_error_necessitate_fallback ( last_disconnected_reason )
722+ end
723+
724+ def does_error_necessitate_fallback ( error )
725+ # RSC15l
726+ if error . respond_to? ( :status_code ) && error . status_code == 401 && error . respond_to? ( :code ) && Ably ::Exceptions ::TOKEN_EXPIRED_CODE . include? ( error . code )
727+ return false
728+ end
729+
730+ true
694731 end
695732
696733 def use_fallback_if_suspended?
@@ -700,6 +737,12 @@ def use_fallback_if_suspended?
700737 def second_reconnect_attempt_for ( state , first_attempt_count )
701738 previous_state == state && manager . retry_count_for_state ( state ) >= first_attempt_count
702739 end
740+
741+ def last_disconnected_reason
742+ history_item = state_history . reverse . find do |history_item |
743+ history_item . fetch ( :state ) == :disconnected
744+ end . fetch ( :metadata ) . reason
745+ end
703746 end
704747 end
705748end
0 commit comments