Sha256: 0e8c8731545596b7f82b97647621197380360a4fb0164f8461fabb9933ef06f5

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require "concurrent"

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?

              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

        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

        # rubocop:disable Lint/DuplicateMethods
        # rubocop:disable Lint/NestedMethodDefinition
        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
        # rubocop:enable Lint/NestedMethodDefinition
        # rubocop:enable Lint/DuplicateMethods

        timed_task_started
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protobuf-activerecord-7.0.0 lib/protobuf/active_record/middleware/connection_management_async.rb