Module: PgPipeline::PoolOps

Defined in:
lib/pg_pipeline/pool.rb

Class Method Summary collapse

Class Method Details

.finite_float!(value, name, allow_zero: false) ⇒ Object



540
541
542
543
544
545
546
547
548
549
# File 'lib/pg_pipeline/pool.rb', line 540

def finite_float!(value, name, allow_zero: false)
  number = Float(value)
  bound_ok = allow_zero ? number >= 0 : number.positive?
  raise ArgumentError unless number.finite? && bound_ok

  number
rescue ArgumentError, TypeError
  requirement = allow_zero ? "non-negative finite" : "positive finite"
  raise ArgumentError, "#{name} must be a #{requirement} number (got #{value.inspect})"
end

.new_connection(connection_args) ⇒ Object



584
585
586
# File 'lib/pg_pipeline/pool.rb', line 584

def new_connection(connection_args)
  connection_args.nil? ? PG::Connection.new : PG::Connection.new(connection_args)
end

.nonnegative_integer!(value, name) ⇒ Object



551
552
553
554
555
556
557
558
# File 'lib/pg_pipeline/pool.rb', line 551

def nonnegative_integer!(value, name)
  integer = Integer(value)
  raise ArgumentError, "#{name} must be >= 0" if integer.negative?

  integer
rescue ArgumentError, TypeError
  raise ArgumentError, "#{name} must be an integer >= 0"
end

.positive_integer!(value, name) ⇒ Object



531
532
533
534
535
536
537
538
# File 'lib/pg_pipeline/pool.rb', line 531

def positive_integer!(value, name)
  integer = Integer(value)
  raise ArgumentError, "#{name} must be >= 1" if integer < 1

  integer
rescue ArgumentError, TypeError
  raise ArgumentError, "#{name} must be an integer >= 1"
end

.safe_close(conn) ⇒ Object



606
607
608
609
610
# File 'lib/pg_pipeline/pool.rb', line 606

def safe_close(conn)
  conn.close unless conn.finished?
rescue StandardError
  nil
end

.sanitize_pinned_connection(conn) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/pg_pipeline/pool.rb', line 588

def sanitize_pinned_connection(conn)
  raise ConnectionLostError, "pinned connection is closed" if conn.finished?
  raise ConnectionLostError, "pinned connection is bad" unless conn.status == PG::CONNECTION_OK

  case conn.transaction_status
  when PG::PQTRANS_IDLE
    nil
  when PG::PQTRANS_INTRANS, PG::PQTRANS_INERROR
    conn.exec("ROLLBACK")
  else
    raise ConnectionLostError,
          "pinned connection returned in unsafe transaction state #{conn.transaction_status}"
  end

  conn.exec("DISCARD ALL")
  true
end

.select_driver(drivers, rr) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/pg_pipeline/pool.rb', line 560

def select_driver(drivers, rr)
  size = drivers.length
  return [nil, rr] if size.zero?

  best = nil
  best_index = nil
  best_load = nil

  size.times do |offset|
    index = (rr + offset) % size
    driver = drivers[index]
    next unless driver.available?

    load = driver.load
    if best.nil? || load < best_load
      best = driver
      best_index = index
      best_load = load
    end
  end

  best ? [best, (best_index + 1) % size] : [nil, rr]
end