Skip to content

Commit bb6df44

Browse files
Setup the config setter for lock timeout options + update README.md
1 parent df907d5 commit bb6df44

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ SafePgMigrations.config.safe_timeout = 5.seconds # Statement timeout used for al
329329

330330
SafePgMigrations.config.lock_timeout = nil # Lock timeout used for all DDL operations except from CREATE / DROP INDEX. If not set, safe_timeout will be used with a deduction of 1% to ensure that the lock timeout is raised in priority
331331

332+
SafePgMigrations.config.max_lock_timeout_for_retry = 1.second # Max lock timeout for the retries for all DDL operations except from CREATE / DROP INDEX. Each retry will increase the lock_timeout by (max_lock_timeout_for_retry - lock_timeout) / max_tries
333+
332334
SafePgMigrations.config.blocking_activity_logger_verbose = true # Outputs the raw blocking queries on timeout. When false, outputs information about the lock instead
333335

334336
SafePgMigrations.config.sensitive_logger = nil # When given, sensitive data will be sent to this logger instead of the standard output. Must implement method `info`.

lib/safe-pg-migrations/configuration.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ class Configuration
1010
blocking_activity_logger_margin
1111
blocking_activity_logger_verbose
1212
default_value_backfill_threshold
13-
lock_timeout
1413
max_tries
15-
max_lock_timeout
1614
retry_delay
1715
sensitive_logger
1816
])
19-
attr_reader :safe_timeout
17+
attr_reader :lock_timeout, :safe_timeout, :max_lock_timeout_for_retry
2018

2119
def initialize
2220
self.default_value_backfill_threshold = nil
@@ -28,30 +26,38 @@ def initialize
2826
self.backfill_pause = 0.5.second
2927
self.retry_delay = 1.minute
3028
self.max_tries = 5
31-
self.max_lock_timeout = 1.second
29+
self.max_lock_timeout_for_retry = 1.second
3230
self.sensitive_logger = nil
3331
end
3432

3533
def lock_timeout=(value)
3634
raise 'Setting lock timeout to 0 disables the lock timeout and is dangerous' if value == 0.seconds
3735

38-
unless value.nil? || value < safe_timeout
39-
raise ArgumentError, "Lock timeout (#{value}) cannot be greater than safe timeout (#{safe_timeout})"
36+
unless value.nil? || (value < safe_timeout && value < max_lock_timeout_on_retry)
37+
raise ArgumentError, "Lock timeout (#{value}) cannot be greater than the safe timeout (#{safe_timeout}) or the max lock timeout for retry (#{max_lock_timeout_for_retry})"
4038
end
4139

4240
@lock_timeout = value
4341
end
4442

4543
def safe_timeout=(value)
46-
raise 'Setting safe timeout to 0 disables the safe timeout and is dangerous' unless value
44+
raise 'Setting safe timeout to 0 or nil disables the safe timeout and is dangerous' unless value && value > 0.seconds
4745

48-
unless lock_timeout.nil? || value > lock_timeout || value > max_lock_timeout
49-
raise ArgumentError, "Safe timeout (#{value}) cannot be less than lock timeout (#{lock_timeout})"
46+
unless lock_timeout.nil? || (value > lock_timeout && value > max_lock_timeout_for_retry)
47+
raise ArgumentError, "Safe timeout (#{value}) cannot be lower than the lock timeout (#{lock_timeout}) or the max lock timeout for retry (#{max_lock_timeout_for_retry})"
5048
end
5149

5250
@safe_timeout = value
5351
end
5452

53+
def max_lock_timeout_for_retry(value)
54+
unless lock_timeout.nil? || (value > lock_timeout && value < safe_timeout)
55+
raise ArgumentError, "Max lock timeout for retry (#{value}) cannot be lower than the lock timeout (#{lock_timeout}) and greater than the safe timeout (#{safe_timeout})"
56+
end
57+
58+
@max_lock_timeout_for_retry = value
59+
end
60+
5561
def pg_statement_timeout
5662
pg_duration safe_timeout
5763
end

0 commit comments

Comments
 (0)