Class: Protobuf::ActiveRecord::Middleware::ConnectionManagementAsync

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/active_record/middleware/connection_management_async.rb

Constant Summary collapse

START_MUTEX =
::Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ConnectionManagementAsync



36
37
38
# File 'lib/protobuf/active_record/middleware/connection_management_async.rb', line 36

def initialize(app)
  @app = app
end

Class Method Details

.start_timed_task!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protobuf/active_record/middleware/connection_management_async.rb', line 9

def self.start_timed_task!
  if timed_task_started.false?
    START_MUTEX.synchronize do
      return if timed_task_started.true?

      args = {
        execution_interval: ::Protobuf::ActiveRecord.config.connection_reaping_interval,
        timeout_interval: ::Protobuf::ActiveRecord.config.connection_reaping_timeout_interval
      }
      timed_task = ::Concurrent::TimerTask.new(args) do
        ::ActiveRecord::Base.clear_active_connections!
      end

      timed_task.execute
      timed_task_started.make_true
    end
  end
end

.timed_task_startedObject



28
29
30
31
32
33
34
# File 'lib/protobuf/active_record/middleware/connection_management_async.rb', line 28

def self.timed_task_started
  if @timed_task_started.nil?
    @timed_task_started = ::Concurrent::AtomicBoolean.new(false)
  end

  @timed_task_started
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Lint/DuplicateMethods rubocop:disable Lint/NestedMethodDefinition



42
43
44
45
46
47
48
49
50
51
# File 'lib/protobuf/active_record/middleware/connection_management_async.rb', line 42

def call(env)
  def call(env)
    ::ActiveRecord::Base.connection_pool.with_connection do
      @app.call(env)
    end
  end

  self.class.start_timed_task!
  call(env)
end