mysql-pause 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mysql-pause/constants.rb +1 -1
- data/lib/mysql-pause/proxy.rb +26 -1
- metadata +1 -1
@@ -1,2 +1,2 @@
|
|
1
1
|
APP_NAME = 'mysql-pause'
|
2
|
-
Version = '0.1.
|
2
|
+
Version = '0.1.3'
|
data/lib/mysql-pause/proxy.rb
CHANGED
@@ -2,14 +2,25 @@ require 'eventmachine'
|
|
2
2
|
require 'mysql-pause/backend'
|
3
3
|
require 'mysql-pause/error'
|
4
4
|
require 'logger'
|
5
|
+
require 'socket'
|
5
6
|
|
6
7
|
module MysqlPause
|
7
8
|
class Proxy < EM::Connection
|
8
9
|
|
9
10
|
def initialize(be_host, be_port, options)
|
10
|
-
@backend = EM.connect(be_host, be_port, MysqlPause::Backend, self)
|
11
11
|
@options = options
|
12
12
|
@logger = Logger.new($stdout)
|
13
|
+
|
14
|
+
begin
|
15
|
+
ping(be_host, be_port)
|
16
|
+
@backend = EM.connect(be_host, be_port, MysqlPause::Backend, self)
|
17
|
+
raise('connect error') if @backend.error?
|
18
|
+
rescue => e
|
19
|
+
@logger.error("#{e.class.name}: #{e.message}")
|
20
|
+
close_backend_connection
|
21
|
+
sleep(@options[:interval])
|
22
|
+
retry
|
23
|
+
end
|
13
24
|
end
|
14
25
|
|
15
26
|
def receive_data(data)
|
@@ -22,9 +33,12 @@ module MysqlPause
|
|
22
33
|
|
23
34
|
if @backend.error?
|
24
35
|
@logger.info("backend error: #{data.inspect}")
|
36
|
+
close_backend_connection
|
37
|
+
|
25
38
|
payload_length, sequence_id = parse_mysql_packet(data)
|
26
39
|
error_message = MysqlPause::Error.create_error_message(MysqlPause::Error::ABORTED_BACKEND_CONNECTION, sequence_id + 1)
|
27
40
|
send_data(error_message)
|
41
|
+
|
28
42
|
close_connection_after_writing
|
29
43
|
else
|
30
44
|
@backend.send_data(data)
|
@@ -41,5 +55,16 @@ module MysqlPause
|
|
41
55
|
[payload_length, sequence_id]
|
42
56
|
end
|
43
57
|
|
58
|
+
def close_backend_connection
|
59
|
+
@backend.close_connection if @backend
|
60
|
+
rescue Exception => e
|
61
|
+
@logger.warn("#{e.class.name}: #{e.message}")
|
62
|
+
end
|
63
|
+
|
64
|
+
def ping(addr, port)
|
65
|
+
sock = TCPSocket.open(addr, port)
|
66
|
+
sock.close
|
67
|
+
end
|
68
|
+
|
44
69
|
end # Proxy
|
45
70
|
end # MysqlPause
|