lib/submodules/ably-ruby/lib/ably/realtime/connection.rb in ably-rest-1.0.0 vs lib/submodules/ably-ruby/lib/ably/realtime/connection.rb in ably-rest-1.0.5

- old
+ new

@@ -349,11 +349,11 @@ # @yield [String] The host name used for this connection, for network connection failures a {Ably::FALLBACK_HOSTS fallback host} is used to route around networking or intermittent problems if an Internet connection is available # @api private def determine_host raise ArgumentError, 'Block required' unless block_given? - if can_use_fallback_hosts? + if should_use_fallback_hosts? internet_up? do |internet_is_up_result| @current_host = if internet_is_up_result client.fallback_endpoint.host else client.endpoint.host @@ -422,11 +422,12 @@ v: Ably::PROTOCOL_VERSION, lib: client.rest_client.lib_version_id, ) # Use native websocket heartbeats if possible - url_params['heartbeats'] = 'false' unless defaults.fetch(:websocket_heartbeats_disabled) + # TODO: Fix once https://github.com/ably/ably-ruby/issues/116 is resolved + url_params['heartbeats'] = 'true' # unless defaults.fetch(:websocket_heartbeats_disabled) url_params['clientId'] = client.auth.client_id if client.auth.has_client_id? if connection_resumable? url_params.merge! resume: key, connection_serial: serial @@ -442,10 +443,14 @@ url = URI(client.endpoint).tap do |endpoint| endpoint.query = URI.encode_www_form(url_params) end determine_host do |host| + # Ensure the hostname matches the fallback host name + url.hostname = host + url.port = port + begin logger.debug { "Connection: Opening socket connection to #{host}:#{port}/#{url.path}?#{url.query}" } @transport = create_transport(host, port, url) do |websocket_transport| websocket_deferrable.succeed websocket_transport end @@ -507,10 +512,11 @@ ( (initialized? || connecting? || disconnected?) && client.queue_messages ) end # @api private def create_transport(host, port, url, &block) + logger.debug { "Connection: EventMachine connecting to #{host}:#{port} with URL: #{url}" } EventMachine.connect(host, port, WebsocketTransport, self, url.to_s, &block) end # @api private def connection_state_ttl @@ -639,15 +645,23 @@ def custom_host? !!client.custom_realtime_host end - def can_use_fallback_hosts? + def should_use_fallback_hosts? if client.fallback_hosts && !client.fallback_hosts.empty? - if connecting? && previous_state + if connecting? && previous_state && !disconnected_from_connected_state? use_fallback_if_disconnected? || use_fallback_if_suspended? end end + end + + def disconnected_from_connected_state? + most_recent_state_changes = state_history.last(3).first(2) # Ignore current state + + # A valid connection was disconnected + most_recent_state_changes.last.fetch(:state) == Connection::STATE.Disconnected && + most_recent_state_changes.first.fetch(:state) == Connection::STATE.Connected end def use_fallback_if_disconnected? second_reconnect_attempt_for(:disconnected, 1) end