lib/flare_up/connection.rb in flare-up-0.6 vs lib/flare_up/connection.rb in flare-up-0.7
- old
+ new
@@ -1,17 +1,19 @@
module FlareUp
- class HostUnknownOrInaccessibleError < StandardError
+ class ConnectionError < StandardError
end
- class TimeoutError < StandardError
+ class HostUnknownOrInaccessibleError < ConnectionError
end
- class NoDatabaseError < StandardError
+ class TimeoutError < ConnectionError
end
- class AuthenticationError < StandardError
+ class NoDatabaseError < ConnectionError
end
- class UnknownError < StandardError
+ class AuthenticationError < ConnectionError
end
+ class UnknownError < ConnectionError
+ end
class Connection
attr_accessor :host
attr_accessor :port
@@ -28,19 +30,24 @@
@port = 5439
@connect_timeout = 5
end
- # TODO - Not quite sure how to test this; perhaps fold connect/execute into
- # TODO one method so we can close connections in case of failure, etc.
def execute(statement)
- @pg_conn ||= connect
- @pg_conn.exec(statement)
+ connection.async_exec(statement)
end
+ def cancel_current_command
+ connection.cancel
+ end
+
private
+ def connection
+ @pg_conn ||= connect
+ end
+
def connect
begin
PG.connect(connection_parameters)
rescue PG::ConnectionBad => e
case e.message
@@ -51,22 +58,33 @@
when /database ".+" does not exist/
raise NoDatabaseError, "Database #{@dbname} does not exist"
when /password authentication failed for user/
raise AuthenticationError, "Either username '#{@user}' or password invalid"
else
- raise UnknownError
+ raise UnknownError, e.message
end
end
end
+ # http://www.postgresql.org/docs/9.3/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS
def connection_parameters
{
:host => @host,
:port => @port,
:dbname => @dbname,
:user => @user,
:password => @password,
- :connect_timeout => @connect_timeout
+ :connect_timeout => @connect_timeout,
+ # Enable keep-alives
+ :keepalives => 1,
+ # Idle time in between keep-alives when there is a response from the peer
+ :keepalives_idle => 30,
+ # Interval between keep-alives when there is no response from the peer
+ # This is done to probe the peer until there is a response
+ :keepalives_interval => 10,
+ # Number of keep-alives that can be lost before the client's connection
+ # to the server is considered dead
+ :keepalives_count => 3
}
end
end
\ No newline at end of file