slave_pools 1.0.0.rc4 → 1.1.0

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: d874a3eed928f50de0897109c923c87a142b8530
4
- data.tar.gz: 525dd736b487ef2c1c7d025742363ac2e8bbcdf5
3
+ metadata.gz: c0fd6bf772d8b4651a9c582e9e04c079d7fdc788
4
+ data.tar.gz: 61e8af6660ecb8de50e5160dfba7a90ee3162935
5
5
  SHA512:
6
- metadata.gz: 6671beaae7b1d6e997b973c15ae8d795c9a8cf5c11b2f2e2c374cd6881270bea3b1211f61ff94e204f99c92ee8f40fc0edc478f9ee36f5aafdd6a1ca2baabf49
7
- data.tar.gz: 54ab55daf2d791f86c5f3d707c6bf98c4c6d5d7113b84a7ef75fa4164f6b06e61f267cee8115dfabc78f109c6f9b25d07249bceb0754533dd962c1181680d43b
6
+ metadata.gz: b1063bfd67d77804819728d33e1c13d11daa65c295e1647ee7a2af4acdf115810ec777545964c085feb4b6c86cae78312699c2ff7a11621d37f766eac3407389
7
+ data.tar.gz: bf754bf16417db1effd756db76bd536a745eb82e6275bb842926888a5f8b2f2e945e55d7db37d12f94a5f1576fe849d0856d09fac1a8836ed59901bfade12626
data/README.md CHANGED
@@ -88,6 +88,17 @@ Add a `config/initializers/slave_pools.rb` if you want to change config settings
88
88
 
89
89
  SlavePools.config.defaults_to_master = true
90
90
 
91
+ #### Configure Errors to not replay on master
92
+
93
+ Some errors you may not want to replay on master, e.g. queries that timeout.
94
+
95
+ This can be configured by updating the no_replay_on_master with a hash of errors and the corresponding messages that should not be replayed, e.g.:
96
+
97
+ SlavePools.config.no_replay_on_master = {
98
+ 'Mysql2::Error' => ['Timeout waiting for a response from the last query', 'other error message that you don't want to replay'],
99
+ 'TimeoutError' => ['some message']
100
+ }
101
+
91
102
  ## Usage
92
103
 
93
104
  Toggle to next replica:
@@ -13,10 +13,16 @@ module SlavePools
13
13
  # Defaults are based on Rails version.
14
14
  attr_accessor :safe_methods
15
15
 
16
+ # enter a list of errors/messages that shouldn't fall back to master
17
+ # of the form {'ErrorClass' => ['message regex1', 'message regex 2'], }
18
+ # Defaults are {'Mysql2::Error' => ['Timeout waiting for a response from the last query']}.
19
+ attr_accessor :no_replay_on_master
20
+
16
21
  def initialize
17
22
  @environment = 'development'
18
23
  @defaults_to_master = false
19
24
  @safe_methods = []
25
+ @no_replay_on_master = {}
20
26
  end
21
27
  end
22
28
  end
@@ -121,11 +121,14 @@ module SlavePools
121
121
  end
122
122
 
123
123
  # decides whether to replay query against master based on the
124
- # exception raised. this could become more sophisticated.
124
+ # exception and message.
125
+ # These can be adjusted by setting SlavePools.configs.no_replay_on_master.
125
126
  def safe_to_replay(e)
126
- # don't replay queries that time out. we don't have the time, and they
127
- # could be dangerous.
128
- ! e.message.match(/Timeout waiting for a response from the last query/)
127
+ return true unless flagged_messages_for_error = SlavePools.config.no_replay_on_master[e.class.to_s]
128
+
129
+ return false if flagged_messages_for_error.any? {|m| e.message.match(m)}
130
+
131
+ true
129
132
  end
130
133
 
131
134
  private
@@ -21,6 +21,9 @@ module SlavePools
21
21
  else
22
22
  warn "Unsupported ActiveRecord version #{ActiveRecord.version}. Please whitelist the safe methods."
23
23
  end
24
+ SlavePools.config.no_replay_on_master = {
25
+ 'Mysql2::Error' => ['Timeout waiting for a response from the last query']
26
+ }
24
27
  end
25
28
 
26
29
  config.after_initialize do
@@ -1,3 +1,3 @@
1
1
  module SlavePools
2
- VERSION = "1.0.0.rc4"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -143,25 +143,20 @@ describe SlavePools do
143
143
  @proxy.should respond_to(:unsafe)
144
144
  end
145
145
 
146
- it 'should NOT rescue a non Mysql2::Error' do
147
- @default_slave1.should_receive(:select_all).once.and_raise(RuntimeError.new('some error'))
148
- @default_slave2.should_not_receive(:select_all)
149
- @master.should_not_receive(:select_all)
150
- lambda { @proxy.select_all(@sql) }.should raise_error
151
- end
152
-
153
- it 'should rescue a Mysql::Error fall back to the master' do
154
- @default_slave1.should_receive(:select_all).once.and_raise(Mysql2::Error.new('connection error'))
146
+ it 'should rescue an error not flagged as no replay' do
147
+ SlavePools.config.no_replay_on_master = {'Mysql2::Error' => ['random message']}
148
+ @default_slave1.should_receive(:select_all).once.and_raise(Mysql2::Error.new('Timeout waiting for a response'))
155
149
  @default_slave2.should_not_receive(:select_all)
156
150
  @master.should_receive(:select_all).and_return(true)
157
151
  lambda { @proxy.select_all(@sql) }.should_not raise_error
158
152
  end
159
153
 
160
- it 'should re-raise a Mysql::Error from a query timeout and not fall back to master' do
161
- @default_slave1.should_receive(:select_all).once.and_raise(Mysql2::Error.new('Timeout waiting for a response from the last query. (waited 5 seconds)'))
154
+ it 'should re-raise a Error that is flagged as no replay' do
155
+ SlavePools.config.no_replay_on_master = {'ArgumentError' => ['random message']}
156
+ @default_slave1.should_receive(:select_all).once.and_raise(ArgumentError.new('random message'))
162
157
  @default_slave2.should_not_receive(:select_all)
163
158
  @master.should_not_receive(:select_all)
164
- lambda { @proxy.select_all(@sql) }.should raise_error
159
+ lambda { @proxy.select_all(@sql) }.should raise_error(ArgumentError)
165
160
  end
166
161
 
167
162
  it 'should reload models from the master' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slave_pools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Drabik
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-05 00:00:00.000000000 Z
12
+ date: 2014-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  version: '1.2'
140
140
  requirements: []
141
141
  rubyforge_project:
142
- rubygems_version: 2.0.3
142
+ rubygems_version: 2.0.14
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: Connection proxy for ActiveRecord for master / replica setups.