distribute_reads 0.2.0 → 0.2.1

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: 0d5f81a8379060ca196ed7550fa1dcafc706afc7
4
- data.tar.gz: 78f4f6af58f6c6b184d1e56532e9c7d910846719
3
+ metadata.gz: b6f3a694f47520a9cf55b522c9a2acf4ec54be50
4
+ data.tar.gz: 20d1d2439e24112ac8d165ad79623edc65222f9d
5
5
  SHA512:
6
- metadata.gz: 78b965141d1bd574726480b2c795131539fb1fcfa2f8456307f4f10ffc34b893826d2c822d170602588a19ab1b09f3d360ab016555f2b5d8e2e9fb92c4c09f40
7
- data.tar.gz: ef38d4ed23658799456f197bff9d0a25ab044e81994aaa453a37495990d108e761e02de0ac97dd017115ba3b9ace8cefbacd157755de3ff80fc43e00ca77ce6c
6
+ metadata.gz: 5434b9aa41816fed52ed2cb9f4f18445b4d5242b9f8f78ad40830e00981ffa2cd46dad8fbe8e4e77f3b98716624075b4e0854bb99d19554fa07492c5f9c90b31
7
+ data.tar.gz: 7d4116b3c7a8c17a96cf6838fbf29f16a89454162a699786b73dba5e64475d757c749143f12b0275fef333329474db60e822343908e8fda4f34f51af8896d4d3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.1
2
+
3
+ - Fixed lag check for Postgres 10
4
+ - Added `replication_lag` method
5
+
1
6
  ## 0.2.0
2
7
 
3
8
  Breaking
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Andrew Kane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -145,9 +145,17 @@ distribute_reads(primary: true) do
145
145
  end
146
146
  ```
147
147
 
148
+ ## Reference
149
+
150
+ Get replication lag
151
+
152
+ ```ruby
153
+ DistributeReads.replication_lag
154
+ ```
155
+
148
156
  ## Thanks
149
157
 
150
- Thanks to [TaskRabbit](https://github.com/taskrabbit) for Makara and [Nick Elser](https://github.com/nickelser) for the write-through cache.
158
+ Thanks to [TaskRabbit](https://github.com/taskrabbit) for Makara, [Sherin Kurian](https://github.com/sherinkurian) for the max lag option, and [Nick Elser](https://github.com/nickelser) for the write-through cache.
151
159
 
152
160
  ## History
153
161
 
@@ -161,3 +169,14 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
161
169
  - Fix bugs and [submit pull requests](https://github.com/ankane/distribute_reads/pulls)
162
170
  - Write, clarify, or fix documentation
163
171
  - Suggest or add new features
172
+
173
+ To test, run:
174
+
175
+ ```sh
176
+ git clone https://github.com/ankane/distribute_reads.git
177
+ cd distribute_reads
178
+ createdb distribute_reads_test_primary
179
+ createdb distribute_reads_test_replica
180
+ bundle
181
+ bundle exec rake
182
+ ```
@@ -11,6 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = "Scale database reads with replicas in Rails"
13
13
  spec.homepage = "https://github.com/ankane/distribute_reads"
14
+ spec.license = "MIT"
14
15
 
15
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
17
  f.match(%r{^(test|spec|features)/})
@@ -1,3 +1,3 @@
1
1
  module DistributeReads
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -19,6 +19,12 @@ module DistributeReads
19
19
  lag_failover: false
20
20
  }
21
21
 
22
+ def self.replication_lag(connection: nil)
23
+ distribute_reads do
24
+ lag(connection: connection)
25
+ end
26
+ end
27
+
22
28
  def self.lag(connection: nil)
23
29
  raise DistributeReads::Error, "Don't use outside distribute_reads" unless Thread.current[:distribute_reads]
24
30
 
@@ -29,11 +35,23 @@ module DistributeReads
29
35
  warn "[distribute_reads] Multiple replicas available, lag only reported for one"
30
36
  end
31
37
 
38
+ # cache the version number
39
+ @server_version_num ||= {}
40
+ cache_key = connection.pool.object_id
41
+ @server_version_num[cache_key] ||= connection.execute("SHOW server_version_num").first["server_version_num"].to_i
42
+
43
+ lag_condition =
44
+ if @server_version_num[cache_key] >= 100000
45
+ "pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()"
46
+ else
47
+ "pg_last_xlog_receive_location() = pg_last_xlog_replay_location()"
48
+ end
49
+
32
50
  connection.execute(
33
51
  "SELECT CASE
34
- WHEN NOT pg_is_in_recovery() OR pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0
52
+ WHEN NOT pg_is_in_recovery() OR #{lag_condition} THEN 0
35
53
  ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
36
- END AS lag"
54
+ END AS lag".squish
37
55
  ).first["lag"].to_f
38
56
  else
39
57
  raise DistributeReads::Error, "Option not supported with this adapter"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: distribute_reads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-03 00:00:00.000000000 Z
11
+ date: 2017-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: makara
@@ -105,6 +105,7 @@ files:
105
105
  - ".travis.yml"
106
106
  - CHANGELOG.md
107
107
  - Gemfile
108
+ - LICENSE.txt
108
109
  - README.md
109
110
  - Rakefile
110
111
  - distribute_reads.gemspec
@@ -115,7 +116,8 @@ files:
115
116
  - lib/distribute_reads/job_methods.rb
116
117
  - lib/distribute_reads/version.rb
117
118
  homepage: https://github.com/ankane/distribute_reads
118
- licenses: []
119
+ licenses:
120
+ - MIT
119
121
  metadata: {}
120
122
  post_install_message:
121
123
  rdoc_options: []