Class: MultiThink::Connection
- Inherits:
-
Object
- Object
- MultiThink::Connection
- Defined in:
- lib/multithink/connection.rb
Constant Summary collapse
- DEFAULTS =
{retries: 10}
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(servers, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #reconnect ⇒ Object
- #run(query) ⇒ Object
Constructor Details
#initialize(servers, options = {}) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 |
# File 'lib/multithink/connection.rb', line 11 def initialize(servers, = {}) @servers = servers = DEFAULTS.merge() @retries = .fetch(:retries) connect end |
Instance Method Details
#connect ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/multithink/connection.rb', line 18 def connect # TODO try all servers until we get a connection = @servers.first begin @conn = r.connect() rescue @tried ||= 0 sleep 1 retry if (@tried += 1) < @retries end end |
#reconnect ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/multithink/connection.rb', line 39 def reconnect begin # try fast path first @conn.reconnect rescue # if that fails then try get a new connection connect end end |
#run(query) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/multithink/connection.rb', line 30 def run(query) # TODO handle connection failure begin query.run(@conn) rescue RuntimeError => e reconnect end end |