Skip to content

Commit 6ef4db2

Browse files
committed
Simplify the busy_handler_timeout
Don't use a modulo and pre-compute the timeout_deadline
1 parent ec687cf commit 6ef4db2

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

lib/sqlite3/database.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,19 @@ def readonly?
691691
@readonly
692692
end
693693

694+
# Sets a #busy_handler that releases the GVL between retries,
695+
# but only retries up to the indicated number of +milliseconds+.
696+
# This is an alternative to #busy_timeout, which holds the GVL
697+
# while SQLite sleeps and retries.
694698
def busy_handler_timeout=( milliseconds )
695699
timeout_seconds = milliseconds.fdiv(1000)
696700
retry_interval = 0.001 # 1 millisecond
701+
timeout_deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout_seconds
697702

698703
busy_handler do |count|
699-
@start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) if count == 0
704+
next false if Process.clock_gettime(Process::CLOCK_MONOTONIC) > timeout_deadline
700705

701-
next sleep(retry_interval) unless (count % 100) == 0
702-
703-
(Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_time) <= timeout_seconds
706+
sleep(retry_interval)
704707
end
705708
end
706709

0 commit comments

Comments
 (0)