Class: MysqlPause::Proxy

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/mysql-pause/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(be_host, be_port, options) ⇒ Proxy

Returns a new instance of Proxy.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mysql-pause/proxy.rb', line 16

def initialize(be_host, be_port, options)
  @options = options
  @logger = Logger.new($stdout)

  n = 0

  begin
    ping(be_host, be_port)
    @backend = EM.connect(be_host, be_port, MysqlPause::Backend, self)
    raise('connection error') if @backend.error?
  rescue => e
    @logger.error("#{e.class.name}: #{e.message}")

    if n < RETRY_LIMIT
      close_backend_connection
      sleep RETRY_INTERVAL
      @logger.warn("connection retry")
      n += 1
      retry
    else
      @logger.error("connection abort")
      raise e
    end
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/mysql-pause/proxy.rb', line 14

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/mysql-pause/proxy.rb', line 13

def options
  @options
end

Instance Method Details

#post_initObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/mysql-pause/proxy.rb', line 42

def post_init
  if (peername = get_peername)
    @connect_from = Socket.unpack_sockaddr_in(peername)
  end

  if @options[:debug]
    port, ip = @connect_from
    @logger.debug("proxy: connect from #{ip}:#{port}")
  end
end

#receive_data(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mysql-pause/proxy.rb', line 62

def receive_data(data)
  EM.defer {
    if @options[:pause]
      @logger.info("pause: #{data.inspect}")
      sleep(@options[:interval]) while @options[:pause]
      @logger.info("resume: #{data.inspect}")
    end

    if @backend.error?
      @logger.info("proxy: backend connection error: #{data.inspect}")
      close_backend_connection

      payload_length, sequence_id = parse_mysql_packet(data)
      error_message = MysqlPause::Error.create_error_message(MysqlPause::Error::ABORTED_BACKEND_CONNECTION, sequence_id + 1)
      send_data(error_message)

      close_connection_after_writing
    else
      @backend.send_data(data)
    end
  }
end

#unbindObject



53
54
55
56
57
58
59
60
# File 'lib/mysql-pause/proxy.rb', line 53

def unbind
  close_backend_connection

  if @options[:debug]
    port, ip = @connect_from
    @logger.debug("proxy: unbind connection from #{ip}:#{port}")
  end
end