switchman 1.5.10 → 1.5.11

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: a864ee73e89a300af961ba7041f8f82ae193d9e5
4
- data.tar.gz: 3b15e10e5bcb9dccd99e3b88b257f1f95c551a9b
3
+ metadata.gz: a6e7bdc0136ef65434df5ec4cd150a80ea3ff48a
4
+ data.tar.gz: 3ac58daa39eb55bc3b1ea743d6995072acf91ab2
5
5
  SHA512:
6
- metadata.gz: aa6ea9736d0316467d7f9493eecb127ed352a2421df4789b8ceb129f5a6eb9ed893ae3863dc72b15268bf7241c55d97dea00b26f78e9dafc0059e18999575a78
7
- data.tar.gz: 0ee49239c7054e8e510c350aa0c9fe529c92e18fd50ee8ee8ceb468e9d2971f99ce788ce4071543d2b2f85bb73dcad94ef1f36b171cc6f09ad420ba8ec50e03e
6
+ metadata.gz: 792fbbdedbdc2028003b2eda51934e9c8821d0fd479c7e5da119c453d3696f232a752bcef868c291f3cc4b31a7acf560cadfda50c4bd48df4aa64826eb27b1f3
7
+ data.tar.gz: 1310d057a27d42f01a7dbad5b70d6ac3be09b42aa06f3abd71f3c933260a387b3542e97a2ca5754b0bd89ee65f0689f7314f8717e5a8929c676dad24879c5ef3
@@ -3,10 +3,12 @@ require 'switchman/errors'
3
3
  module Switchman
4
4
  module ActiveRecord
5
5
  module ConnectionPool
6
- attr_writer :shard
7
-
8
6
  def shard
9
- @shard || Shard.default
7
+ Thread.current["#{object_id}_shard".to_sym] || Shard.default
8
+ end
9
+
10
+ def shard=(value)
11
+ Thread.current["#{object_id}_shard".to_sym] = value
10
12
  end
11
13
 
12
14
  def default_schema
@@ -18,10 +20,14 @@ module Switchman
18
20
  end
19
21
 
20
22
  def checkout_new_connection
21
- # TODO: this might be a threading issue
22
- spec.config[:shard_name] = self.shard.name
23
+ conn = synchronize do
24
+ # ideally I would just keep a thread-local spec that I could modify
25
+ # without locking anything, but if spec returns not-the-object passed
26
+ # to initialize this pool, things break
27
+ spec.config[:shard_name] = self.shard.name
23
28
 
24
- conn = super
29
+ super
30
+ end
25
31
  conn.shard = self.shard
26
32
  conn
27
33
  end
@@ -71,7 +71,7 @@ module Switchman
71
71
  when ::ActiveRecord::Relation
72
72
  Shard.default
73
73
  when nil
74
- Shard.current(klass.respond_to?(:shard_category) ? klass.shard_category : :default)
74
+ Shard.current(klass.shard_category)
75
75
  else
76
76
  raise ArgumentError, "invalid shard value #{shard_value}"
77
77
  end
@@ -85,7 +85,7 @@ module Switchman
85
85
  when ::ActiveRecord::Base
86
86
  shard_value.respond_to?(:associated_shards) ? shard_value.associated_shards : [shard_value.shard]
87
87
  when nil
88
- [Shard.current(klass.respond_to?(:shard_category) ? klass.shard_category : default)]
88
+ [Shard.current(klass.shard_category)]
89
89
  else
90
90
  shard_value
91
91
  end
@@ -13,6 +13,20 @@ module Switchman
13
13
  owner.send(active_record_primary_key) # use sharded id values in association binds
14
14
  end
15
15
  end
16
+
17
+ # cache association scopes by shard.
18
+ if ::Rails.version >= '4.2'
19
+ def association_scope_cache(conn, owner)
20
+ key = conn.prepared_statements
21
+ if polymorphic?
22
+ key = [key, owner._read_attribute(@foreign_type)]
23
+ end
24
+ key = [key, owner.shard.id].flatten
25
+ @association_scope_cache[key] ||= @scope_lock.synchronize {
26
+ @association_scope_cache[key] ||= yield
27
+ }
28
+ end
29
+ end
16
30
  end
17
31
  end
18
32
  end
@@ -7,13 +7,13 @@ module Switchman
7
7
 
8
8
  def initialize(*args)
9
9
  super
10
- self.shard_value = Shard.current(klass.respond_to?(:shard_category) ? klass.shard_category : :default) unless shard_value
10
+ self.shard_value = Shard.current(klass ? klass.shard_category : :default) unless shard_value
11
11
  self.shard_source_value = :implicit unless shard_source_value
12
12
  end
13
13
 
14
14
  def clone
15
15
  result = super
16
- result.shard_value = Shard.current(klass.respond_to?(:shard_category) ? klass.shard_category : :default) unless shard_value
16
+ result.shard_value = Shard.current(klass ? klass.shard_category : :default) unless shard_value
17
17
  result
18
18
  end
19
19
 
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.5.10"
2
+ VERSION = "1.5.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.10
4
+ version: 1.5.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-06-16 00:00:00.000000000 Z
13
+ date: 2016-06-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -225,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
225
  version: '0'
226
226
  requirements: []
227
227
  rubyforge_project:
228
- rubygems_version: 2.4.5.1
228
+ rubygems_version: 2.6.4
229
229
  signing_key:
230
230
  specification_version: 4
231
231
  summary: Rails 4 sharding magic