Skip to content

Commit

Permalink
Use the same busy_handler function as will be in the sqlite3-ruby gem
Browse files Browse the repository at this point in the history
  • Loading branch information
fractaledmind committed Apr 10, 2024
1 parent 9f993d1 commit 17ea7f0
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/enhanced_sqlite3/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,17 @@ def configure_connection
def configure_busy_handler_timeout
return unless @config.key?(:timeout)

timeout = self.class.type_cast_config_to_integer(@config[:timeout])
timeout_seconds = timeout.fdiv(1000)
retry_interval = 6e-5 # 60 microseconds
timeout_milliseconds = self.class.type_cast_config_to_integer(@config[:timeout])
timeout_seconds = timeout_milliseconds.fdiv(1000)

@raw_connection.busy_handler do |count|
timed_out = false
# keep track of elapsed time every 100 iterations (to lower load)
if (count % 100).zero?
# fail if we exceed the timeout value (captured from the timeout config option, converted to seconds)
timed_out = (count * retry_interval) > timeout_seconds
end
if timed_out
false # this will cause the BusyException to be raised
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
if count.zero?
@timeout_deadline = now + timeout_seconds
elsif now > @timeout_deadline
next false
else
sleep(retry_interval)
true
sleep(0.001)
end
end
end
Expand Down

0 comments on commit 17ea7f0

Please sign in to comment.