multithink 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/multithink/connection.rb +27 -3
- data/lib/multithink/version.rb +1 -1
- metadata +1 -1
@@ -6,20 +6,44 @@ include RethinkDB::Shortcuts
|
|
6
6
|
|
7
7
|
class MultiThink::Connection
|
8
8
|
|
9
|
-
|
9
|
+
DEFAULTS = {retries: 10}
|
10
|
+
|
11
|
+
def initialize(servers, options = {})
|
10
12
|
@servers = servers
|
13
|
+
options = DEFAULTS.merge(options)
|
14
|
+
@retries = options.fetch(:retries)
|
11
15
|
connect
|
12
16
|
end
|
13
17
|
|
14
18
|
def connect
|
15
19
|
# TODO try all servers until we get a connection
|
16
20
|
options = @servers.first
|
17
|
-
|
21
|
+
begin
|
22
|
+
@conn = r.connect(options)
|
23
|
+
rescue
|
24
|
+
@tried ||= 0
|
25
|
+
sleep 1
|
26
|
+
retry if (@tried += 1) < @retries
|
27
|
+
end
|
18
28
|
end
|
19
29
|
|
20
30
|
def run(query)
|
21
31
|
# TODO handle connection failure
|
22
|
-
|
32
|
+
begin
|
33
|
+
query.run(@conn)
|
34
|
+
rescue RuntimeError => e
|
35
|
+
reconnect
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def reconnect
|
40
|
+
begin
|
41
|
+
# try fast path first
|
42
|
+
@conn.reconnect
|
43
|
+
rescue
|
44
|
+
# if that fails then try get a new connection
|
45
|
+
connect
|
46
|
+
end
|
23
47
|
end
|
24
48
|
|
25
49
|
end
|
data/lib/multithink/version.rb
CHANGED