rails-pg-adapter 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8492318d406e356186bb5d0830087f1e3c21eeeefbc806b0881680a026c3e85b
4
- data.tar.gz: 52d95814d9f0df186c464932e4a81f30ff6d2aabf8715740b64d4973877a7681
3
+ metadata.gz: b13533de6e67440feaaee7799c583984672793b16b10aee60ca89091cadef545
4
+ data.tar.gz: e6927d01f1604871d426d8fa894dc400ac3626191c151b034b12771ee68b91ac
5
5
  SHA512:
6
- metadata.gz: f51318474f857c6733e86f7cb6a19d7d2e7d84cd4de5125b780e1a44a7ff8bdb6325e9dd934c197b47566bf6d359454e0fc0761563b64e5629d752fa2886f10c
7
- data.tar.gz: 7a98ab05cc5d99a1b26244ea000634fd4efdb8d855a1c81c101342e6aa1149ecac01fd5c8131837ff3ae0ae9d75e1c98bdbed2974be7c38e9810643b22580d8b
6
+ metadata.gz: d60b542abec512d9f5d1567f65d4327e3bc299d8ac740616fbec5aeb394f612698db6c74f5a462640cd96bdb1fb7013ff433f28780ef5cb5c3c0157ca0fa10b4
7
+ data.tar.gz: 16bbac80afd152b275c451243331318be772c88b9dcce2ddb1eb30be5875556f489b476b55e3b34ec618e6047a00405ca6d9caa73729d83d480101c1849a0ec8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.1.7] - 2023-04-21
2
+
3
+ - Simplify connection management and introduce upper bound to retries
4
+
1
5
  ## [0.1.6] - 2023-04-19
2
6
 
3
7
  - Disconnect and remove connection when in read-only
@@ -13,34 +13,55 @@ module RailsPgAdapter
13
13
  "the database system is starting up",
14
14
  "connection is closed",
15
15
  "could not connect",
16
+ "is not currently accepting connections",
16
17
  ].freeze
17
18
  CONNECTION_ERROR_RE = /#{CONNECTION_ERROR.map { |w| Regexp.escape(w) }.join("|")}/.freeze
18
19
 
19
20
  CONNECTION_SCHEMA_ERROR = ["PG::UndefinedColumn"].freeze
20
21
  CONNECTION_SCHEMA_RE = /#{CONNECTION_SCHEMA_ERROR.map { |w| Regexp.escape(w) }.join("|")}/.freeze
21
22
 
22
- CONNECTION_READ_ONLY_ERROR = [
23
- "read-only",
24
- "PG::ReadOnlySqlTransaction",
25
- ].freeze
26
- CONNECTION_READ_ONLY_ERROR_RE = /#{CONNECTION_READ_ONLY_ERROR.map { |w| Regexp.escape(w) }.join("|")}/.freeze
27
-
28
23
  private
29
24
 
30
25
  def exec_cache(*args)
31
- super(*args)
32
- rescue ::ActiveRecord::StatementInvalid, ::ActiveRecord::ConnectionNotEstablished => e
33
- raise unless supported_errors?(e)
34
-
35
- try_reconnect?(e) ? retry : handle_error(e)
26
+ sleep_times = RailsPgAdapter.configuration.reconnect_with_backoff.dup
27
+ begin
28
+ super(*args)
29
+ rescue ::ActiveRecord::StatementInvalid,
30
+ ::ActiveRecord::ConnectionNotEstablished,
31
+ ::ActiveRecord::NoDatabaseError => e
32
+ raise unless supported_errors?(e)
33
+
34
+ if try_reconnect?(e)
35
+ sleep_time = sleep_times.shift
36
+ handle_error(e) unless sleep_time
37
+ warn("Retry query failed, retrying again in #{sleep_time} sec.")
38
+ sleep(sleep_time)
39
+ retry
40
+ else
41
+ handle_error(e)
42
+ end
43
+ end
36
44
  end
37
45
 
38
46
  def exec_no_cache(*args)
39
- super(*args)
40
- rescue ::ActiveRecord::StatementInvalid, ::ActiveRecord::ConnectionNotEstablished => e
41
- raise unless supported_errors?(e)
42
-
43
- try_reconnect?(e) ? retry : handle_error(e)
47
+ sleep_times = RailsPgAdapter.configuration.reconnect_with_backoff.dup
48
+ begin
49
+ super(*args)
50
+ rescue ::ActiveRecord::StatementInvalid,
51
+ ::ActiveRecord::ConnectionNotEstablished,
52
+ ::ActiveRecord::NoDatabaseError => e
53
+ raise unless supported_errors?(e)
54
+
55
+ if try_reconnect?(e)
56
+ sleep_time = sleep_times.shift
57
+ handle_error(e) unless sleep_time
58
+ warn("Retry query failed, retrying again in #{sleep_time} sec.")
59
+ sleep(sleep_time)
60
+ retry
61
+ else
62
+ handle_error(e)
63
+ end
64
+ end
44
65
  end
45
66
 
46
67
  def try_reconnect?(e)
@@ -49,8 +70,7 @@ module RailsPgAdapter
49
70
  return false unless RailsPgAdapter.reconnect_with_backoff?
50
71
 
51
72
  begin
52
- disconnect_and_remove_conn! if read_only_error?(e.message)
53
- reconnect!
73
+ disconnect_conn!
54
74
  true
55
75
  rescue ::ActiveRecord::ConnectionNotEstablished
56
76
  false
@@ -60,11 +80,13 @@ module RailsPgAdapter
60
80
  def handle_error(e)
61
81
  if failover_error?(e.message) && RailsPgAdapter.failover_patch?
62
82
  warn("clearing connections due to #{e} - #{e.message}")
63
- disconnect_and_remove_conn!
83
+ disconnect_conn!
64
84
  raise(e)
65
85
  end
66
86
 
67
- return unless missing_column_error?(e.message) && RailsPgAdapter.reset_column_information_patch?
87
+ unless missing_column_error?(e.message) && RailsPgAdapter.reset_column_information_patch?
88
+ return
89
+ end
68
90
 
69
91
  warn("clearing column information due to #{e} - #{e.message}")
70
92
 
@@ -80,7 +102,7 @@ module RailsPgAdapter
80
102
  CONNECTION_SCHEMA_RE.match?(error_message)
81
103
  end
82
104
 
83
- def disconnect_and_remove_conn!
105
+ def disconnect_conn!
84
106
  disconnect!
85
107
  ::ActiveRecord::Base.connection_pool.remove(::ActiveRecord::Base.connection)
86
108
  end
@@ -103,10 +125,6 @@ module RailsPgAdapter
103
125
  end
104
126
  false
105
127
  end
106
-
107
- def read_only_error?(error_message)
108
- CONNECTION_READ_ONLY_ERROR_RE.match?(error_message)
109
- end
110
128
  end
111
129
  end
112
130
 
@@ -123,12 +141,12 @@ module ActiveRecord
123
141
  sleep_times = RailsPgAdapter.configuration.reconnect_with_backoff.dup
124
142
  begin
125
143
  old_new_client_method.bind(self).call(args)
126
- rescue ::ActiveRecord::ConnectionNotEstablished => e
144
+ rescue ::ActiveRecord::ConnectionNotEstablished, ::ActiveRecord::NoDatabaseError => e
127
145
  raise(e) unless RailsPgAdapter.failover_patch? && RailsPgAdapter.reconnect_with_backoff?
128
146
 
129
147
  sleep_time = sleep_times.shift
130
148
  raise unless sleep_time
131
- warn( "Could not establish a connection from new_client, retrying again in #{sleep_time} sec.")
149
+ warn("Could not establish a connection from new_client, retrying again in #{sleep_time} sec.")
132
150
  sleep(sleep_time)
133
151
  retry
134
152
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPgAdapter
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-pg-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tines Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-19 00:00:00.000000000 Z
11
+ date: 2023-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails