distribute_reads 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +13 -0
- data/CHANGELOG.md +4 -0
- data/README.md +18 -2
- data/distribute_reads.gemspec +1 -0
- data/lib/distribute_reads.rb +5 -0
- data/lib/distribute_reads/appropriate_pool.rb +4 -1
- data/lib/distribute_reads/global_methods.rb +4 -3
- data/lib/distribute_reads/job_methods.rb +9 -0
- data/lib/distribute_reads/version.rb +1 -1
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96dec398d9905bde755d83b504ec450047b11128
|
4
|
+
data.tar.gz: 63dc0b236f1829b9fb2df0400299a35713717766
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c96152f662c6fca7f75a51d2db9ab75e236ccc15f0aa3543ff73084fb509a34642422b53727f856fc11992a1ab643ca5fbda4251e1d794a634633000111cb871
|
7
|
+
data.tar.gz: 3f5ac5f959d935953795fab5877e01f666fde7c6b77d0cce6b0e9e237afacf3cf57c5a56456cde598e0322a06c3c296fbb80c1d1810ab5c17115a5da070513e7
|
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm: 2.4.1
|
4
|
+
script: bundle exec rake test
|
5
|
+
before_script:
|
6
|
+
- psql -c 'create database distribute_reads_test_primary;' -U postgres
|
7
|
+
- psql -c 'create database distribute_reads_test_replica;' -U postgres
|
8
|
+
notifications:
|
9
|
+
email:
|
10
|
+
on_success: never
|
11
|
+
on_failure: change
|
12
|
+
gemfile:
|
13
|
+
- Gemfile
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,8 @@ 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://travis-ci.org/ankane/distribute_reads.svg?branch=master)](https://travis-ci.org/ankane/distribute_reads)
|
8
|
+
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
Add this line to your application’s Gemfile:
|
@@ -18,13 +20,13 @@ gem 'distribute_reads'
|
|
18
20
|
|
19
21
|
```yml
|
20
22
|
default: &default
|
21
|
-
|
23
|
+
url: postgresql-makara:///
|
22
24
|
makara:
|
23
25
|
sticky: true
|
24
26
|
connections:
|
25
27
|
- role: master
|
26
28
|
name: primary
|
27
|
-
url: <%= ENV["
|
29
|
+
url: <%= ENV["DATABASE_URL"] %>
|
28
30
|
- name: replica
|
29
31
|
url: <%= ENV["REPLICA_DATABASE_URL"] %>
|
30
32
|
|
@@ -54,6 +56,20 @@ distribute_reads do
|
|
54
56
|
end
|
55
57
|
```
|
56
58
|
|
59
|
+
## Jobs
|
60
|
+
|
61
|
+
Distribute all reads in a job with:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class TestJob < ApplicationJob
|
65
|
+
distribute_reads
|
66
|
+
|
67
|
+
def perform
|
68
|
+
# ...
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
57
73
|
## Options
|
58
74
|
|
59
75
|
Raise an error when replica lag is too high - *PostgreSQL only*
|
data/distribute_reads.gemspec
CHANGED
data/lib/distribute_reads.rb
CHANGED
@@ -2,7 +2,10 @@ module DistributeReads
|
|
2
2
|
module AppropriatePool
|
3
3
|
def _appropriate_pool(*args)
|
4
4
|
if Thread.current[:distribute_reads]
|
5
|
-
if needs_master?(*args) || @slave_pool.completely_blacklisted?
|
5
|
+
if needs_master?(*args) || @slave_pool.completely_blacklisted?
|
6
|
+
stick_to_master(*args) unless DistributeReads.default_to_primary
|
7
|
+
@master_pool
|
8
|
+
elsif in_transaction?
|
6
9
|
@master_pool
|
7
10
|
else
|
8
11
|
@slave_pool
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module DistributeReads
|
2
2
|
module GlobalMethods
|
3
3
|
def distribute_reads(max_lag: nil)
|
4
|
+
if max_lag && DistributeReads.lag > max_lag
|
5
|
+
raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
|
6
|
+
end
|
7
|
+
|
4
8
|
previous_value = Thread.current[:distribute_reads]
|
5
9
|
begin
|
6
|
-
if max_lag && DistributeReads.lag > max_lag
|
7
|
-
raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
|
8
|
-
end
|
9
10
|
Thread.current[:distribute_reads] = true
|
10
11
|
yield
|
11
12
|
ensure
|
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.1.
|
4
|
+
version: 0.1.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-
|
11
|
+
date: 2017-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: makara
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activejob
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description:
|
84
98
|
email:
|
85
99
|
- andrew@chartkick.com
|
@@ -88,6 +102,7 @@ extensions: []
|
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
91
106
|
- CHANGELOG.md
|
92
107
|
- Gemfile
|
93
108
|
- README.md
|
@@ -96,6 +111,7 @@ files:
|
|
96
111
|
- lib/distribute_reads.rb
|
97
112
|
- lib/distribute_reads/appropriate_pool.rb
|
98
113
|
- lib/distribute_reads/global_methods.rb
|
114
|
+
- lib/distribute_reads/job_methods.rb
|
99
115
|
- lib/distribute_reads/version.rb
|
100
116
|
homepage: https://github.com/ankane/distribute_reads
|
101
117
|
licenses: []
|
@@ -116,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
132
|
version: '0'
|
117
133
|
requirements: []
|
118
134
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.6.
|
135
|
+
rubygems_version: 2.6.11
|
120
136
|
signing_key:
|
121
137
|
specification_version: 4
|
122
138
|
summary: Scale database reads with replicas in Rails
|