Sha256: 9641e606cb43bffe835ed07245604ef6b611c79d81af7d8a0db26944f7386760

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

require "concurrent"
require "thread"

module Protobuf
  module ActiveRecord
    module Middleware
      class ConnectionManagementAsync
        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 => ::Protobuf::ActiveRecord.config.connection_reaping_interval,
                             :timeout_interval => ::Protobuf::ActiveRecord.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)
            ::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

7 entries across 7 versions & 1 rubygems

Version Path
protobuf-activerecord-3.6.0 lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.5.1 lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.5.0 lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.4.4 lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.4.4.pre lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.4.3 lib/protobuf/active_record/middleware/connection_management_async.rb
protobuf-activerecord-3.4.2 lib/protobuf/active_record/middleware/connection_management_async.rb