My Brain is Open.

思いついたことを適当に列列と

PostgreSQLのコネクションをいっぱいにして保持するツール

需要あんのかな?

#!/usr/bin/env ruby

require 'rubygems'
require 'pg' # postgres

## Setting ##
ConnectionInfo = {
  :host => "localhost",
  :port => 5432,
  :dbname => "postgres",
  :user => "postgres",
}

# get wait_time from ARGV[0]
if ARGV.size < 1 
  puts <<EOS
Usage: ruby #{__FILE__} <wait_time>
    wait_time: Wait <wait_time> seconds with maximum pg connection.
               ( set <= 0 to infinite, ^C to exit )
EOS
  exit 
else
  wait_time = ARGV[0].to_i
end

# Create Connections
connection_array = Array.new

while(connection_array.empty? or connection_array.last.status == PGconn::CONNECTION_OK)
  begin
    connection_array << PGconn.new(ConnectionInfo)
  rescue PGError
    break
  rescue Interrupt # with Interrupt
    connection_array.each { |conn| conn.close }
    puts "All connection cleared."
    exit
  end
  # p connection_array.size
end

# Print number of connections.
puts "Maximum: #{connection_array.size} connections."

# Wait loop.
begin
  if wait_time > 0
    wait_time.downto(1) do |i|
      puts "Remain #{i} second(s)." if i % 10**((Math::log10(wait_time)-0.5).floor) == 0
      sleep 1
    end
  else
    i=1
    loop do
     sleep 1
      puts "After #{i} second(s)." if i % 10**((Math::log10(i)).floor) == 0
      i += 1
    end
  end
rescue Interrupt # if ^C sent, exit with no error. 
ensure
  connection_array.each { |conn| conn.close }
  puts "All connection cleared."
end