Sha256: d2fb32fb7f44b16e2f502e0f9ce0766fa0654f6632c88c096ec2f9a9d1cabce6

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'connection_pool'

module Vmpooler
  class PoolManager
    class GenericConnectionPool < ConnectionPool
      # Extend the ConnectionPool class with instrumentation
      # https://github.com/mperham/connection_pool/blob/master/lib/connection_pool.rb

      def initialize(options = {}, &block)
        super(options, &block)
        @metrics = options[:metrics]
        @metric_prefix = options[:metric_prefix]
        @metric_prefix = 'connectionpool' if @metric_prefix.nil? || @metric_prefix == ''
      end

      if Thread.respond_to?(:handle_interrupt)
        # MRI
        def with_metrics(options = {})
          Thread.handle_interrupt(Exception => :never) do
            start = Time.now
            conn = checkout(options)
            timespan_ms = ((Time.now - start) * 1000).to_i
            @metrics&.gauge(@metric_prefix + '.available', @available.length)
            @metrics&.timing(@metric_prefix + '.waited', timespan_ms)
            begin
              Thread.handle_interrupt(Exception => :immediate) do
                yield conn
              end
            ensure
              checkin
              @metrics&.gauge(@metric_prefix + '.available', @available.length)
            end
          end
        end
      else
        # jruby 1.7.x
        def with_metrics(options = {})
          start = Time.now
          conn = checkout(options)
          timespan_ms = ((Time.now - start) * 1000).to_i
          @metrics&.gauge(@metric_prefix + '.available', @available.length)
          @metrics&.timing(@metric_prefix + '.waited', timespan_ms)
          begin
            yield conn
          ensure
            checkin
            @metrics&.gauge(@metric_prefix + '.available', @available.length)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vmpooler-0.12.0 lib/vmpooler/generic_connection_pool.rb
vmpooler-0.11.3 lib/vmpooler/generic_connection_pool.rb
vmpooler-0.11.2 lib/vmpooler/generic_connection_pool.rb
vmpooler-0.11.1 lib/vmpooler/generic_connection_pool.rb
vmpooler-0.11.0 lib/vmpooler/generic_connection_pool.rb