distribute_reads 0.3.5 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/LICENSE.txt +1 -1
- data/README.md +7 -7
- data/lib/distribute_reads/job_methods.rb +1 -5
- data/lib/distribute_reads/version.rb +1 -1
- data/lib/distribute_reads.rb +7 -17
- metadata +20 -7
- data/lib/distribute_reads/cache_store.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 498115398e131f837029f3ad86c21ea646204b399f732a800b32687135bee154
|
4
|
+
data.tar.gz: 40c983fa023cab9fe00b5f0e7b03925d2f1d7c6e3d30056a26e9c147fd92d4b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a3a60620721ef9b23d1faf8a742386dfd58d3830ee7b110dbc44d100d7fd7670189e8893f1d21eab4d7bced008942f8fe8116756ae1aae6cec0f9427b3018d4
|
7
|
+
data.tar.gz: 56b65735bffb296bcfc8719c847988c0ecc4610c21ecad650115181431ac85b0b7cd686c7a409cede3cfc25c9a54de74e6f053556ea9ca6c28feb242e6ad1709
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
## 0.5.0 (2024-06-24)
|
2
|
+
|
3
|
+
- Dropped support for Makara 0.4
|
4
|
+
- Dropped support for Ruby < 3.1 and Active Record < 6.1
|
5
|
+
|
6
|
+
## 0.4.0 (2022-12-28)
|
7
|
+
|
8
|
+
- Made `distribute_reads` method private to behave like `Kernel` methods
|
9
|
+
- Dropped support for Makara 0.3
|
10
|
+
- Dropped support for Ruby < 2.6 and Active Record < 5.2
|
11
|
+
|
1
12
|
## 0.3.5 (2022-01-30)
|
2
13
|
|
3
14
|
- Added support for Aurora Postgres 13 replication lag
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Scale database reads to replicas in Rails
|
|
4
4
|
|
5
5
|
:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
|
6
6
|
|
7
|
-
[![Build Status](https://github.com/ankane/distribute_reads/workflows/build/badge.svg
|
7
|
+
[![Build Status](https://github.com/ankane/distribute_reads/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/distribute_reads/actions)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -16,7 +16,7 @@ gem "distribute_reads"
|
|
16
16
|
|
17
17
|
## How to Use
|
18
18
|
|
19
|
-
[Makara](https://github.com/
|
19
|
+
[Makara](https://github.com/instacart/makara) does most of the work. First, update `database.yml` to use it:
|
20
20
|
|
21
21
|
```yml
|
22
22
|
default: &default
|
@@ -74,7 +74,7 @@ You can pass any options as well.
|
|
74
74
|
|
75
75
|
## Lazy Evaluation
|
76
76
|
|
77
|
-
|
77
|
+
Active Record uses [lazy evaluation](https://www.theodinproject.com/courses/ruby-on-rails/lessons/active-record-queries), which can delay the execution of a query to outside of a `distribute_reads` block. In this case, the primary will be used.
|
78
78
|
|
79
79
|
```ruby
|
80
80
|
users = distribute_reads { User.where(orders_count: 1) } # not executed yet
|
@@ -185,9 +185,9 @@ distribute_reads(replica: true) do
|
|
185
185
|
end
|
186
186
|
```
|
187
187
|
|
188
|
-
## Rails
|
188
|
+
## Rails
|
189
189
|
|
190
|
-
Rails 6 has [native support for replicas](https://guides.rubyonrails.org/active_record_multiple_databases.html) :tada:
|
190
|
+
Rails 6+ has [native support for replicas](https://guides.rubyonrails.org/active_record_multiple_databases.html) :tada:
|
191
191
|
|
192
192
|
```ruby
|
193
193
|
ActiveRecord::Base.connected_to(role: :reading) do
|
@@ -195,11 +195,11 @@ ActiveRecord::Base.connected_to(role: :reading) do
|
|
195
195
|
end
|
196
196
|
```
|
197
197
|
|
198
|
-
However, it’s not able to
|
198
|
+
However, it’s not able to do automatic statement-based routing like Makara yet.
|
199
199
|
|
200
200
|
## Thanks
|
201
201
|
|
202
|
-
Thanks to [TaskRabbit](https://github.com/taskrabbit) for Makara, [Sherin Kurian](https://github.com/
|
202
|
+
Thanks to [TaskRabbit](https://github.com/taskrabbit) for Makara, [Sherin Kurian](https://github.com/sherin) for the max lag option, and [Nick Elser](https://github.com/nickelser) for the write-through cache.
|
203
203
|
|
204
204
|
## History
|
205
205
|
|
@@ -7,11 +7,7 @@ module DistributeReads
|
|
7
7
|
included do
|
8
8
|
before_perform do
|
9
9
|
if DistributeReads.by_default
|
10
|
-
|
11
|
-
Makara::Context.set_current(Makara::Context.generate)
|
12
|
-
else
|
13
|
-
Makara::Context.release_all
|
14
|
-
end
|
10
|
+
Makara::Context.release_all
|
15
11
|
end
|
16
12
|
end
|
17
13
|
end
|
data/lib/distribute_reads.rb
CHANGED
@@ -3,10 +3,9 @@ require "active_support"
|
|
3
3
|
require "makara"
|
4
4
|
|
5
5
|
# modules
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
require "distribute_reads/version"
|
6
|
+
require_relative "distribute_reads/appropriate_pool"
|
7
|
+
require_relative "distribute_reads/global_methods"
|
8
|
+
require_relative "distribute_reads/version"
|
10
9
|
|
11
10
|
module DistributeReads
|
12
11
|
class Error < StandardError; end
|
@@ -126,7 +125,6 @@ module DistributeReads
|
|
126
125
|
end
|
127
126
|
end
|
128
127
|
|
129
|
-
# private
|
130
128
|
def self.backtrace_cleaner
|
131
129
|
@backtrace_cleaner ||= begin
|
132
130
|
bc = ActiveSupport::BacktraceCleaner.new
|
@@ -136,8 +134,8 @@ module DistributeReads
|
|
136
134
|
bc
|
137
135
|
end
|
138
136
|
end
|
137
|
+
private_class_method :backtrace_cleaner
|
139
138
|
|
140
|
-
# private
|
141
139
|
def self.with_replica
|
142
140
|
previous_value = Thread.current[:distribute_reads]
|
143
141
|
begin
|
@@ -147,14 +145,7 @@ module DistributeReads
|
|
147
145
|
Thread.current[:distribute_reads] = previous_value
|
148
146
|
end
|
149
147
|
end
|
150
|
-
|
151
|
-
# private
|
152
|
-
def self.makara3?
|
153
|
-
unless defined?(@makara3)
|
154
|
-
@makara3 = Gem::Version.new(Makara::VERSION.to_s) < Gem::Version.new("0.4.0")
|
155
|
-
end
|
156
|
-
@makara3
|
157
|
-
end
|
148
|
+
private_class_method :with_replica
|
158
149
|
|
159
150
|
# legacy
|
160
151
|
def self.default_to_primary
|
@@ -169,10 +160,9 @@ end
|
|
169
160
|
|
170
161
|
Makara::Proxy.prepend DistributeReads::AppropriatePool
|
171
162
|
Object.include DistributeReads::GlobalMethods
|
172
|
-
|
173
|
-
# Object.send :private, :distribute_reads
|
163
|
+
Object.send :private, :distribute_reads
|
174
164
|
|
175
165
|
ActiveSupport.on_load(:active_job) do
|
176
|
-
|
166
|
+
require_relative "distribute_reads/job_methods"
|
177
167
|
include DistributeReads::JobMethods
|
178
168
|
end
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distribute_reads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.1'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: makara
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
33
|
+
version: '0.5'
|
20
34
|
type: :runtime
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
40
|
+
version: '0.5'
|
27
41
|
description:
|
28
42
|
email: andrew@ankane.org
|
29
43
|
executables: []
|
@@ -35,7 +49,6 @@ files:
|
|
35
49
|
- README.md
|
36
50
|
- lib/distribute_reads.rb
|
37
51
|
- lib/distribute_reads/appropriate_pool.rb
|
38
|
-
- lib/distribute_reads/cache_store.rb
|
39
52
|
- lib/distribute_reads/global_methods.rb
|
40
53
|
- lib/distribute_reads/job_methods.rb
|
41
54
|
- lib/distribute_reads/version.rb
|
@@ -51,14 +64,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
64
|
requirements:
|
52
65
|
- - ">="
|
53
66
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
67
|
+
version: '3.1'
|
55
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
69
|
requirements:
|
57
70
|
- - ">="
|
58
71
|
- !ruby/object:Gem::Version
|
59
72
|
version: '0'
|
60
73
|
requirements: []
|
61
|
-
rubygems_version: 3.
|
74
|
+
rubygems_version: 3.5.11
|
62
75
|
signing_key:
|
63
76
|
specification_version: 4
|
64
77
|
summary: Scale database reads with replicas in Rails
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module DistributeReads
|
2
|
-
class CacheStore
|
3
|
-
def read(key)
|
4
|
-
memory_cached = memory_store.read(key)
|
5
|
-
return nil if memory_cached == :nil
|
6
|
-
return memory_cached if memory_cached
|
7
|
-
|
8
|
-
store_cached = store.try(:read, key)
|
9
|
-
memory_store.write(key, store_cached || :nil)
|
10
|
-
store_cached
|
11
|
-
end
|
12
|
-
|
13
|
-
def write(*args)
|
14
|
-
memory_store.write(*args)
|
15
|
-
store.try(:write, *args)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
# use ActiveSupport::Cache::MemoryStore instead?
|
21
|
-
def memory_store
|
22
|
-
@memory_store ||= Makara::Cache::MemoryStore.new
|
23
|
-
end
|
24
|
-
|
25
|
-
def store
|
26
|
-
@store ||= Rails.cache
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|