rails-pg-adapter 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8492318d406e356186bb5d0830087f1e3c21eeeefbc806b0881680a026c3e85b
4
- data.tar.gz: 52d95814d9f0df186c464932e4a81f30ff6d2aabf8715740b64d4973877a7681
3
+ metadata.gz: 17e38c0c8aeb60de1697257595dd0123a262d7e6a10a996de13086e826da96a7
4
+ data.tar.gz: 2bb519b103a9027848ae3ca963efc66f57d47b2b34be969657035c7a4f35256b
5
5
  SHA512:
6
- metadata.gz: f51318474f857c6733e86f7cb6a19d7d2e7d84cd4de5125b780e1a44a7ff8bdb6325e9dd934c197b47566bf6d359454e0fc0761563b64e5629d752fa2886f10c
7
- data.tar.gz: 7a98ab05cc5d99a1b26244ea000634fd4efdb8d855a1c81c101342e6aa1149ecac01fd5c8131837ff3ae0ae9d75e1c98bdbed2974be7c38e9810643b22580d8b
6
+ metadata.gz: f65dba534475afa8db2028ec685822c86d2c80df01ef29b99212440d032b797c3a99cad2150d4276802df26fed34e45fd0810f9051f4e4ce57e4d657898aebd2
7
+ data.tar.gz: 80c9d7257edfefc0f452930cc84f864056df43b1cfea3c2705397411e297c6dce02fa4d43ae55bc0a188a49d778514caa9df6341e36154408a243376880040d1
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,44 +13,81 @@ 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
23
+ class << self
24
+ def supported_errors?(e)
25
+ return true if failover_error?(e.message)
26
+ return true if missing_column_error?(e.message)
27
+ false
28
+ end
29
+
30
+ def failover_error?(error_message)
31
+ CONNECTION_ERROR_RE.match?(error_message) && ::RailsPgAdapter.failover_patch?
32
+ end
33
+
34
+ def missing_column_error?(error_message)
35
+ CONNECTION_SCHEMA_RE.match?(error_message) &&
36
+ ::RailsPgAdapter.reset_column_information_patch?
37
+ end
38
+ end
27
39
 
28
40
  private
29
41
 
30
42
  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)
43
+ sleep_times = ::RailsPgAdapter.configuration.reconnect_with_backoff.dup
44
+ begin
45
+ super(*args)
46
+ rescue ::ActiveRecord::StatementInvalid,
47
+ ::ActiveRecord::ConnectionNotEstablished,
48
+ ::ActiveRecord::NoDatabaseError => e
49
+ raise unless ::RailsPgAdapter::Patch.supported_errors?(e)
50
+
51
+ if try_reconnect?(e)
52
+ sleep_time = sleep_times.shift
53
+ handle_error(e) unless sleep_time
54
+ warn("Retry query failed, retrying again in #{sleep_time} sec.")
55
+ sleep(sleep_time)
56
+ retry
57
+ else
58
+ handle_error(e)
59
+ end
60
+ end
36
61
  end
37
62
 
38
63
  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)
64
+ sleep_times = ::RailsPgAdapter.configuration.reconnect_with_backoff.dup
65
+ begin
66
+ super(*args)
67
+ rescue ::ActiveRecord::StatementInvalid,
68
+ ::ActiveRecord::ConnectionNotEstablished,
69
+ ::ActiveRecord::NoDatabaseError => e
70
+ raise unless ::RailsPgAdapter::Patch.supported_errors?(e)
71
+
72
+ if try_reconnect?(e)
73
+ sleep_time = sleep_times.shift
74
+ handle_error(e) unless sleep_time
75
+ warn("Retry query failed, retrying again in #{sleep_time} sec.")
76
+ sleep(sleep_time)
77
+ retry
78
+ else
79
+ handle_error(e)
80
+ end
81
+ end
44
82
  end
45
83
 
46
84
  def try_reconnect?(e)
47
85
  return false if in_transaction?
48
- return false unless failover_error?(e.message)
49
- return false unless RailsPgAdapter.reconnect_with_backoff?
86
+ return false unless ::RailsPgAdapter::Patch.failover_error?(e.message)
87
+ return false unless ::RailsPgAdapter.reconnect_with_backoff?
50
88
 
51
89
  begin
52
- disconnect_and_remove_conn! if read_only_error?(e.message)
53
- reconnect!
90
+ disconnect_conn!
54
91
  true
55
92
  rescue ::ActiveRecord::ConnectionNotEstablished
56
93
  false
@@ -58,13 +95,13 @@ module RailsPgAdapter
58
95
  end
59
96
 
60
97
  def handle_error(e)
61
- if failover_error?(e.message) && RailsPgAdapter.failover_patch?
98
+ if ::RailsPgAdapter::Patch.failover_error?(e.message)
62
99
  warn("clearing connections due to #{e} - #{e.message}")
63
- disconnect_and_remove_conn!
100
+ disconnect_conn!
64
101
  raise(e)
65
102
  end
66
103
 
67
- return unless missing_column_error?(e.message) && RailsPgAdapter.reset_column_information_patch?
104
+ return unless ::RailsPgAdapter::Patch.missing_column_error?(e.message)
68
105
 
69
106
  warn("clearing column information due to #{e} - #{e.message}")
70
107
 
@@ -72,15 +109,7 @@ module RailsPgAdapter
72
109
  raise
73
110
  end
74
111
 
75
- def failover_error?(error_message)
76
- CONNECTION_ERROR_RE.match?(error_message)
77
- end
78
-
79
- def missing_column_error?(error_message)
80
- CONNECTION_SCHEMA_RE.match?(error_message)
81
- end
82
-
83
- def disconnect_and_remove_conn!
112
+ def disconnect_conn!
84
113
  disconnect!
85
114
  ::ActiveRecord::Base.connection_pool.remove(::ActiveRecord::Base.connection)
86
115
  end
@@ -93,19 +122,7 @@ module RailsPgAdapter
93
122
  def warn(msg)
94
123
  return unless defined?(Rails)
95
124
  return if Rails.logger.nil?
96
- ::Rails.logger.warn("[RailsPgAdapter::Patch] #{msg}")
97
- end
98
-
99
- def supported_errors?(e)
100
- return true if failover_error?(e.message) && RailsPgAdapter.failover_patch?
101
- if missing_column_error?(e.message) && RailsPgAdapter.reset_column_information_patch?
102
- return true
103
- end
104
- false
105
- end
106
-
107
- def read_only_error?(error_message)
108
- CONNECTION_READ_ONLY_ERROR_RE.match?(error_message)
125
+ ::Rails.logger.warn("[::RailsPgAdapter::Patch] #{msg}")
109
126
  end
110
127
  end
111
128
  end
@@ -120,15 +137,20 @@ module ActiveRecord
120
137
  old_new_client_method = instance_method(:new_client)
121
138
 
122
139
  define_method(:new_client) do |args|
123
- sleep_times = RailsPgAdapter.configuration.reconnect_with_backoff.dup
140
+ sleep_times = ::RailsPgAdapter.configuration.reconnect_with_backoff.dup
124
141
  begin
125
142
  old_new_client_method.bind(self).call(args)
126
- rescue ::ActiveRecord::ConnectionNotEstablished => e
127
- raise(e) unless RailsPgAdapter.failover_patch? && RailsPgAdapter.reconnect_with_backoff?
143
+ rescue ::ActiveRecord::ConnectionNotEstablished, ::ActiveRecord::NoDatabaseError => e
144
+ unless ::RailsPgAdapter::Patch.supported_errors?(e) &&
145
+ ::RailsPgAdapter.reconnect_with_backoff?
146
+ raise
147
+ end
128
148
 
129
149
  sleep_time = sleep_times.shift
130
150
  raise unless sleep_time
131
- warn( "Could not establish a connection from new_client, retrying again in #{sleep_time} sec.")
151
+ warn(
152
+ "Could not establish a connection from new_client, retrying again in #{sleep_time} sec.",
153
+ )
132
154
  sleep(sleep_time)
133
155
  retry
134
156
  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.8"
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.8
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-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails