Module: RSMP::SiteProxyConnection

Included in:
SiteProxy
Defined in:
lib/rsmp/proxy/site/connection.rb

Overview

Connection management for supervisor-side site proxies.

Instance Method Summary collapse

Instance Method Details

#connectObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rsmp/proxy/site/connection.rb', line 44

def connect
  log "Connecting to site #{@site_id} at #{@ip}:#{@port}", level: :info
  begin_session
  self.state = :connecting
  opened = open_socket
  return opened if opened.failure?

  self.state = :connected
  @logger.unmute @ip, @port
  log "Connected to site #{@site_id} at #{@ip}:#{@port}", level: :info
  Result.success(self)
end

#open_socketObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rsmp/proxy/site/connection.rb', line 57

def open_socket
  endpoint = IO::Endpoint.tcp(@ip, @port)
  timeout = @site_settings.dig('timeouts', 'connect') || 1.1
  task.with_timeout(timeout) { @socket = endpoint.connect }
  @stream = IO::Stream::Buffered.new(@socket)
  @protocol = RSMP::Protocol.new(@stream)
  Result.success(self)
rescue SystemCallError, SocketError, IOError, Async::TimeoutError => e
  Result.failure(
    :connection_failed,
    message: "Could not connect to site #{@site_id} at #{@ip}:#{@port}: #{e.message}",
    source: :transport,
    context: { ip: @ip, port: @port, session_id: @session_id },
    cause: e
  )
end

#reconnect_delay?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'lib/rsmp/proxy/site/connection.rb', line 74

def reconnect_delay?
  return false if @site_settings['intervals']['reconnect'] == :no

  interval = @site_settings['intervals']['reconnect'] || 0.1
  log "Will try to reconnect again every #{interval} seconds...", level: :info
  @logger.mute @ip, @port
  @task.sleep interval
  true
end

#runObject

handle communication when we're created, the socket is already open



6
7
8
9
10
11
12
# File 'lib/rsmp/proxy/site/connection.rb', line 6

def run
  if @protocol
    run_accepted_connection
  else
    run_outbound_connection
  end
end

#run_accepted_connectionObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/rsmp/proxy/site/connection.rb', line 14

def run_accepted_connection
  begin_session
  self.state = :connected
  start_reader
  ended = wait_for_reader
  close_from_result(ended)
ensure
  close(reason: :internal_failure) if $ERROR_INFO
  close
end

#run_outbound_connectionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rsmp/proxy/site/connection.rb', line 25

def run_outbound_connection
  loop do
    setup_site_settings
    connected = connect
    unless connected.success?
      publish_connection_attempt_failure(connected.failure)
      break unless reconnect_delay?

      next
    end
    start_reader
    close_from_result(wait_for_reader)
    break unless reconnect_delay?
  ensure
    close(reason: :internal_failure) if $ERROR_INFO
    close
  end
end