lib/submodules/ably-ruby/lib/ably/realtime/connection.rb in ably-rest-0.8.13 vs lib/submodules/ably-ruby/lib/ably/realtime/connection.rb in ably-rest-0.8.14

- old
+ new

@@ -62,12 +62,13 @@ # Defaults for automatic connection recovery and timeouts DEFAULTS = { disconnected_retry_timeout: 15, # when the connection enters the DISCONNECTED state, after this delay in milliseconds, if the state is still DISCONNECTED, the client library will attempt to reconnect automatically suspended_retry_timeout: 30, # when the connection enters the SUSPENDED state, after this delay in milliseconds, if the state is still SUSPENDED, the client library will attempt to reconnect automatically - connection_state_ttl: 60, # the duration that Ably will persist the connection state when a Realtime client is abruptly disconnected - realtime_request_timeout: 10 # default timeout when establishing a connection, or sending a HEARTBEAT, CONNECT, ATTACH, DETACH or CLOSE ProtocolMessage + connection_state_ttl: 120, # the duration that Ably will persist the connection state when a Realtime client is abruptly disconnected + max_connection_state_ttl: nil, # allow a max TTL to be passed in for CI test purposes thus overiding any connection_state_ttl sent from Ably + realtime_request_timeout: 10, # default timeout when establishing a connection, or sending a HEARTBEAT, CONNECT, ATTACH, DETACH or CLOSE ProtocolMessage }.freeze # A unique public identifier for this connection, used to identify this member in presence events and messages # @return [String] attr_reader :id @@ -387,11 +388,11 @@ auth_deferrable.callback do |auth_params| url_params = auth_params.merge( format: client.protocol, echo: client.echo_messages, v: Ably::PROTOCOL_VERSION, - lib: Ably::LIB_VERSION_ID, + lib: client.rest_client.lib_version_id, ) url_params['clientId'] = client.auth.client_id if client.auth.has_client_id? if connection_resumable? @@ -405,16 +406,16 @@ end end url = URI(client.endpoint).tap do |endpoint| endpoint.query = URI.encode_www_form(url_params) - end.to_s + end determine_host do |host| begin - logger.debug "Connection: Opening socket connection to #{host}:#{port} and URL '#{url}'" - @transport = EventMachine.connect(host, port, WebsocketTransport, self, url) do |websocket_transport| + 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 rescue EventMachine::ConnectionError => error websocket_deferrable.fail error end @@ -471,10 +472,26 @@ def can_publish_messages? connected? || ( (initialized? || connecting? || disconnected?) && client.queue_messages ) end + # @api private + def create_transport(host, port, url, &block) + EventMachine.connect(host, port, WebsocketTransport, self, url.to_s, &block) + end + + # @api private + def connection_state_ttl + defaults[:max_connection_state_ttl] || # undocumented max TTL configuration + (details && details.connection_state_ttl) || + defaults.fetch(:connection_state_ttl) + end + + def connection_state_ttl=(val) + @connection_state_ttl = val + end + # As we are using a state machine, do not allow change_state to be used # #transition_state_machine must be used instead private :change_state private @@ -525,13 +542,24 @@ def when_initialized EventMachine.next_tick { yield } end def connection_resumable? - !key.nil? && !serial.nil? + !key.nil? && !serial.nil? && connection_state_available? end + def connection_state_available? + return true if connected? + + connected_last = state_history.reverse.find { |connected| connected.fetch(:state) == :connected } + if connected_last.nil? || (connected_last.fetch(:transitioned_at) < Time.now - connection_state_ttl) + false + else + true + end + end + def connection_recoverable? connection_recover_parts end def connection_recover_parts @@ -553,10 +581,10 @@ def custom_host? !!client.custom_realtime_host end def can_use_fallback_hosts? - if production? && !custom_port? && !custom_host? + if client.fallback_hosts && !client.fallback_hosts.empty? if connecting? && previous_state use_fallback_if_disconnected? || use_fallback_if_suspended? end end end