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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rsmp/proxy/site/connection.rb', line 48

def connect
  log "Connecting to site #{@site_id} at #{@ip}:#{@port}", level: :info
  self.state = :connecting
  open_socket
  self.state = :connected
  @logger.unmute @ip, @port
  log "Connected to site #{@site_id} at #{@ip}:#{@port}", level: :info
rescue SystemCallError => e
  raise ConnectionError, "Could not connect to site #{@site_id} at #{@ip}:#{@port}: Errno #{e.errno} #{e}"
rescue StandardError => e
  raise ConnectionError, "Error while connecting to site #{@site_id} at #{@ip}:#{@port}: #{e}"
end

#open_socketObject



61
62
63
64
65
66
67
# File 'lib/rsmp/proxy/site/connection.rb', line 61

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)
end

#reconnect_delay?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
# File 'lib/rsmp/proxy/site/connection.rb', line 69

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
24
# File 'lib/rsmp/proxy/site/connection.rb', line 14

def run_accepted_connection
  self.state = :connected
  start_reader
  wait_for_reader # run until disconnected
rescue RSMP::ConnectionError => e
  log e, level: :error
rescue StandardError => e
  distribute_error e, level: :internal
ensure
  close
end

#run_outbound_connectionObject



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

def run_outbound_connection
  loop do
    setup_site_settings
    connect
    start_reader
    wait_for_reader
    break unless reconnect_delay?
  rescue Restart
    @logger.mute @ip, @port
    raise
  rescue RSMP::ConnectionError => e
    log e, level: :error
    break unless reconnect_delay?
  rescue StandardError => e
    distribute_error e, level: :internal
    break unless reconnect_delay?
  ensure
    close
    stop_subtasks
  end
end