semian 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2297213cb021d0958a8826c46536af57862e7582
4
- data.tar.gz: cd2683b720caa4b4e5dfe2e8db5a774b822f13ca
3
+ metadata.gz: 4b365b861b701b9fa52df84a771d48cfce3fbfc8
4
+ data.tar.gz: 18fe305ce3d4132fde7d3c358255097e7e6fa5bc
5
5
  SHA512:
6
- metadata.gz: cb26790fd5481ada268ae712f500ccbf52df6b39bb05634d913811c8fe55d75d1e5cd2523157386dca701b154dfacc6f48a233215bccef3485ffed52ab8c9974
7
- data.tar.gz: 1d193bb017105f5d4c5aaa7f4872034997f564d9bd8a863fccc285f7b9e94fe6465a917101161e43dfee0383dccc1157d83fc0054479526ecb8a288bcab55e60
6
+ metadata.gz: 3f2acf17c3d39ebbc595b58c099ba0711f4bcc356f4c9fdc00d7e401e26959cf547d5fe2395aa3324dbf632abecb6802885a69e76d08ce8ced17bf139868b310
7
+ data.tar.gz: fb0343a60116861285186ec5009db34e8a4296c7b08f4d16e94c96628ef11829d3425b1f37b9c60d3fd3a6aa3e40b8c71ca4784bf96f1713cf01a7c8ecf901c6
data/lib/semian/mysql2.rb CHANGED
@@ -21,12 +21,13 @@ module Semian
21
21
 
22
22
  CONNECTION_ERROR = Regexp.union(
23
23
  /Can't connect to MySQL server on/i,
24
- /Lost connection to MySQL server during query/i,
24
+ /Lost connection to MySQL server/i,
25
25
  /MySQL server has gone away/i,
26
26
  )
27
27
 
28
28
  ResourceBusyError = ::Mysql2::ResourceBusyError
29
29
  CircuitOpenError = ::Mysql2::CircuitOpenError
30
+ PingFailure = Class.new(::Mysql2::Error)
30
31
 
31
32
  DEFAULT_HOST = 'localhost'
32
33
  DEFAULT_PORT = 3306
@@ -44,6 +45,9 @@ module Semian
44
45
 
45
46
  base.send(:alias_method, :raw_connect, :connect)
46
47
  base.send(:remove_method, :connect)
48
+
49
+ base.send(:alias_method, :raw_ping, :ping)
50
+ base.send(:remove_method, :ping)
47
51
  end
48
52
 
49
53
  def semian_identifier
@@ -57,6 +61,17 @@ module Semian
57
61
  end
58
62
  end
59
63
 
64
+ def ping
65
+ result = nil
66
+ acquire_semian_resource(adapter: :mysql, scope: :ping) do
67
+ result = raw_ping
68
+ raise PingFailure.new(result.to_s) unless result
69
+ end
70
+ result
71
+ rescue ResourceBusyError, CircuitOpenError, PingFailure
72
+ false
73
+ end
74
+
60
75
  def query(*args)
61
76
  if query_whitelisted?(*args)
62
77
  raw_query(*args)
@@ -84,7 +99,7 @@ module Semian
84
99
  def acquire_semian_resource(*)
85
100
  super
86
101
  rescue ::Mysql2::Error => error
87
- if error.message =~ CONNECTION_ERROR
102
+ if error.message =~ CONNECTION_ERROR || error.is_a?(PingFailure)
88
103
  semian_resource.mark_failed(error)
89
104
  error.semian_identifier = semian_identifier
90
105
  end
@@ -1,3 +1,3 @@
1
1
  module Semian
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.3'
3
3
  end
data/test/mysql2_test.rb CHANGED
@@ -255,11 +255,32 @@ class TestMysql2 < Minitest::Test
255
255
  assert_equal 2, client.query('SELECT 1 + 1 as sum;').to_a.first['sum']
256
256
  end
257
257
 
258
+ def test_pings_are_circuit_broken
259
+ client = connect_to_mysql!
260
+
261
+ def client.raw_ping
262
+ @real_pings ||= 0
263
+ @real_pings += 1
264
+ super
265
+ end
266
+
267
+ @proxy.downstream(:latency, latency: 1200).apply do
268
+ ERROR_THRESHOLD.times do
269
+ client.ping
270
+ end
271
+
272
+ assert_equal false, client.ping
273
+ end
274
+
275
+ assert_equal ERROR_THRESHOLD, client.instance_variable_get(:@real_pings)
276
+ end
277
+
258
278
  private
259
279
 
260
280
  def connect_to_mysql!(semian_options = {})
261
281
  Mysql2::Client.new(
262
282
  connect_timeout: 1,
283
+ read_timeout: 1,
263
284
  host: '127.0.0.1',
264
285
  port: '13306',
265
286
  semian: SEMIAN_OPTIONS.merge(semian_options),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Francis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-10 00:00:00.000000000 Z
12
+ date: 2016-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler