Sha256: 10337d283376c1f8ead261e76d93a32fb57834e5f53a6b2cd6a3d854315cb314

Contents?: true

Size: 1.37 KB

Versions: 42

Compression:

Stored size: 1.37 KB

Contents

require "concurrent"
require "thread"

module ActionSubscriber
  module Middleware
    module ActiveRecord
      class ConnectionManagement
        START_MUTEX = ::Mutex.new

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

              timed_task = ::Concurrent::TimerTask.new(
                :execution_interval => ::ActionSubscriber.config.connection_reaping_interval,
                :timeout_interval => ::ActionSubscriber.config.connection_reaping_timeout_interval) do

                ::ActiveRecord::Base.clear_active_connections!
                end

              timed_task.execute
              timed_task_started.make_true
            end
          end
        end

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

          @timed_task_started
        end

        def initialize(app)
          @app = app
        end

        def call(env)
          def call(env) # redefines so it only gets called once
            ::ActiveRecord::Base.connection_pool.with_connection do
              @app.call(env)
            end
          end

          self.class.start_timed_task!
          call(env)
        end

        timed_task_started
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
action_subscriber-5.3.3-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.3 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.2-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.2 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.1-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.1 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.1.pre-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.1.pre lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.0-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.3.0 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.4-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.4 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.3-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.3 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.2-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.2 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.1-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.1 lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.0-java lib/action_subscriber/middleware/active_record/connection_management.rb
action_subscriber-5.2.0 lib/action_subscriber/middleware/active_record/connection_management.rb