Skip to content

Commit 21b607f

Browse files
Merge pull request #6 from fractaledmind/simpler-busy-handler
Implement a simpler busy_handler
2 parents 1137be4 + ef19ad0 commit 21b607f

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

lib/enhanced_sqlite3/adapter.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,21 @@ def configure_busy_handler_timeout
5656
return unless @config.key?(:timeout)
5757

5858
timeout = self.class.type_cast_config_to_integer(@config[:timeout])
59+
timeout_seconds = timeout.fdiv(1000)
60+
retry_interval = 6e-5 # 60 microseconds
61+
5962
@raw_connection.busy_handler do |count|
6063
timed_out = false
61-
# capture the start time of this blocked write
62-
@start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) if count == 0
6364
# keep track of elapsed time every 100 iterations (to lower load)
64-
if count % 100 == 0
65-
@elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start_time
65+
if (count % 100).zero?
6666
# fail if we exceed the timeout value (captured from the timeout config option, converted to seconds)
67-
timed_out = @elapsed_time > timeout
67+
timed_out = (count * retry_interval) > timeout_seconds
6868
end
6969
if timed_out
7070
false # this will cause the BusyException to be raised
7171
else
72-
sleep 0.001 # sleep 1 millisecond (or whatever)
72+
sleep(retry_interval)
73+
true
7374
end
7475
end
7576
end

0 commit comments

Comments
 (0)