Skip to content

Commit ee90036

Browse files
committed
Add back busy_handler_timeout
1 parent 30969c8 commit ee90036

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

lib/sqlite3/database.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,25 @@ def readonly?
698698
@readonly
699699
end
700700

701+
# Sets a #busy_handler that releases the GVL between retries,
702+
# but only retries up to the indicated number of +milliseconds+.
703+
# This is an alternative to #busy_timeout, which holds the GVL
704+
# while SQLite sleeps and retries.
705+
def busy_handler_timeout=( milliseconds )
706+
timeout_seconds = milliseconds.fdiv(1000)
707+
708+
busy_handler do |count|
709+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
710+
if count.zero?
711+
@timeout_deadline = now + timeout_seconds
712+
elsif now > @timeout_deadline
713+
next false
714+
else
715+
sleep(0.001)
716+
end
717+
end
718+
end
719+
701720
# A helper class for dealing with custom functions (see #create_function,
702721
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
703722
# opaque function object that represents the current invocation. It also

0 commit comments

Comments
 (0)