action_subscriber 4.1.1 → 4.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: ccc5f172b9d43f029ee19e805938749fab60ec6c
4
- data.tar.gz: 7bfd593e871b37ef0fb4b5c8df37afa6878b8aff
3
+ metadata.gz: c531b1ce648c0d3f3057e7b9f9620fa605399fa9
4
+ data.tar.gz: 6134d0333d3689787c0bb0508cacb918fd82602d
5
5
  SHA512:
6
- metadata.gz: b046598b87a58137b0309351e08639528ae785520047bbf323343a25e8cdd3891b996514377a2472e0c25570c0b043b7d75242dec23199951bdf5d1d14ff0c00
7
- data.tar.gz: 7bfc28d4e05131c38c4a69a257f1e06d4f610b2b321d6671c16d0a8bd9c6c4965bc255c715c2a5a4a5bb063bbbf14f3f0d093ee6b729e9d5bca19c914980ccd5
6
+ metadata.gz: 1263da405d3719b7eaadacdd8bd67436b43bb8eb5cc68be20c543fa41fa74072ee3895b4bf3857bc50d8ed2dc3cf135dd444f061385c8f1bc77caf298e24d6e8
7
+ data.tar.gz: c9f2eac5ef35772f65be8abf90c110af9ecc8df9fbdb6b4f26ca86dbfc595b36fd666288de4c15c9fa377f628eb700154affec2591f7d7c687c7a21b25063dda
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  else
27
27
  spec.add_dependency 'bunny', '>= 1.5.0'
28
28
  end
29
+ spec.add_dependency 'concurrent-ruby'
29
30
  spec.add_dependency 'middleware'
30
31
  spec.add_dependency 'thor'
31
32
 
@@ -4,6 +4,8 @@ require "action_subscriber/uri"
4
4
  module ActionSubscriber
5
5
  class Configuration
6
6
  attr_accessor :allow_low_priority_methods,
7
+ :connection_reaping_interval,
8
+ :connection_reaping_timeout_interval,
7
9
  :decoder,
8
10
  :default_exchange,
9
11
  :error_handler,
@@ -28,6 +30,8 @@ module ActionSubscriber
28
30
 
29
31
  DEFAULTS = {
30
32
  :allow_low_priority_methods => false,
33
+ :connection_reaping_interval => 6,
34
+ :connection_reaping_timeout_interval => 5,
31
35
  :default_exchange => 'events',
32
36
  :heartbeat => 5,
33
37
  :host => 'localhost',
@@ -1,16 +1,52 @@
1
+ require "concurrent"
2
+ require "thread"
3
+
1
4
  module ActionSubscriber
2
5
  module Middleware
3
6
  module ActiveRecord
4
7
  class ConnectionManagement
8
+ START_MUTEX = ::Mutex.new
9
+
10
+ def self.start_timed_task!
11
+ if timed_task_started.false?
12
+ START_MUTEX.synchronize do
13
+ return if timed_task_started.true?
14
+
15
+ timed_task = ::Concurrent::TimerTask.new(
16
+ :execution_interval => ::ActionSubscriber.config.connection_reaping_interval,
17
+ :timeout_interval => ::ActionSubscriber.config.connection_reaping_timeout_interval) do
18
+
19
+ ::ActiveRecord::Base.clear_active_connections!
20
+ end
21
+
22
+ timed_task.execute
23
+ timed_task_started.make_true
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.timed_task_started
29
+ if @timed_task_started.nil?
30
+ @timed_task_started = ::Concurrent::AtomicBoolean.new(false)
31
+ end
32
+
33
+ @timed_task_started
34
+ end
35
+
5
36
  def initialize(app)
6
37
  @app = app
7
38
  end
8
39
 
9
40
  def call(env)
10
- @app.call(env)
11
- ensure
12
- ::ActiveRecord::Base.clear_active_connections!
41
+ def call(env)
42
+ @app.call(env)
43
+ end
44
+
45
+ self.class.start_timed_task!
46
+ call(env)
13
47
  end
48
+
49
+ timed_task_started
14
50
  end
15
51
  end
16
52
  end
@@ -2,26 +2,28 @@ module ActionSubscriber
2
2
  module Middleware
3
3
  module ActiveRecord
4
4
  class QueryCache
5
+ CURRENT_CONNECTION = "_action_subscriber_query_cache_current_connection".freeze
6
+
5
7
  def initialize(app)
6
8
  @app = app
7
9
  end
8
10
 
9
11
  def call(env)
10
- enabled = ::ActiveRecord::Base.connection.query_cache_enabled
11
- connection_id = ::ActiveRecord::Base.connection_id
12
- ::ActiveRecord::Base.connection.enable_query_cache!
12
+ connection = ::Thread.current[CURRENT_CONNECTION] = ::ActiveRecord::Base.connection
13
+ enabled = connection.query_cache_enabled
14
+ connection.enable_query_cache!
13
15
 
14
16
  @app.call(env)
15
17
  ensure
16
- restore_query_cache_settings(connection_id, enabled)
18
+ restore_query_cache_settings(enabled)
17
19
  end
18
20
 
19
- private
21
+ private
20
22
 
21
- def restore_query_cache_settings(connection_id, enabled)
22
- ::ActiveRecord::Base.connection_id = connection_id
23
- ::ActiveRecord::Base.connection.clear_query_cache
24
- ::ActiveRecord::Base.connection.disable_query_cache! unless enabled
23
+ def restore_query_cache_settings(enabled)
24
+ ::Thread.current[CURRENT_CONNECTION].clear_query_cache
25
+ ::Thread.current[CURRENT_CONNECTION].disable_query_cache! unless enabled
26
+ ::Thread.current[CURRENT_CONNECTION] = nil
25
27
  end
26
28
  end
27
29
  end
@@ -1,3 +1,3 @@
1
1
  module ActionSubscriber
2
- VERSION = "4.1.1"
2
+ VERSION = "4.2.1"
3
3
  end
@@ -9,8 +9,8 @@ describe ActionSubscriber::Middleware::ActiveRecord::ConnectionManagement do
9
9
 
10
10
  it_behaves_like 'an action subscriber middleware'
11
11
 
12
- it "clears active connections" do
13
- expect(ActiveRecord::Base).to receive(:clear_active_connections!)
12
+ it "starts async task to clear connections" do
13
+ expect(ActionSubscriber::Middleware::ActiveRecord::ConnectionManagement).to receive(:start_timed_task!)
14
14
  subject.call(env)
15
15
  end
16
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_subscriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.1
4
+ version: 4.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Stien
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2017-02-03 00:00:00.000000000 Z
15
+ date: 2017-03-11 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport
@@ -42,6 +42,20 @@ dependencies:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
44
  version: 1.5.0
45
+ - !ruby/object:Gem::Dependency
46
+ name: concurrent-ruby
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ type: :runtime
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
45
59
  - !ruby/object:Gem::Dependency
46
60
  name: middleware
47
61
  requirement: !ruby/object:Gem::Requirement
@@ -268,7 +282,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
282
  version: '0'
269
283
  requirements: []
270
284
  rubyforge_project:
271
- rubygems_version: 2.6.6
285
+ rubygems_version: 2.6.10
272
286
  signing_key:
273
287
  specification_version: 4
274
288
  summary: ActionSubscriber is a DSL that allows a rails app to consume messages from