Sqlite: add a scoped per-call busy-timeout override (VL-SQLITE-04)
This commit is contained in:
1 parent
797ea630a1
commit
7e5cf7379a
3 files changed
+159
No files matched your search
@@ -403,6 +403,9 @@ namespace Void::Sqlite
|
||||
std::uint64_t active_transaction_id{0};
|
||||
std::uint64_t next_transaction_id{0};
|
||||
std::size_t open_savepoints{0};
|
||||
/// Current busy timeout, tracked so scoped overrides restore the
|
||||
/// exact prior value instead of the configured default.
|
||||
std::chrono::milliseconds busy_timeout{0};
|
||||
|
||||
std::vector<std::unique_ptr<ScalarFunctionBox>> scalar_functions{};
|
||||
std::unique_ptr<AuthorizerBox> authorizer{};
|
||||
@@ -1694,6 +1697,7 @@ namespace Void::Sqlite
|
||||
const auto busy_ms = state->options.limits.busy_timeout.count();
|
||||
const int clamped_busy = busy_ms > INT_MAX ? INT_MAX : static_cast<int>(busy_ms);
|
||||
const int busy_rc = sqlite3_busy_timeout(raw_db, clamped_busy);
|
||||
state->busy_timeout = std::chrono::milliseconds{clamped_busy};
|
||||
if (busy_rc != SQLITE_OK) {
|
||||
return Void::Result::fail(sqlite_error_from_db(raw_db, busy_rc, "busy_timeout"));
|
||||
}
|
||||
@@ -1974,6 +1978,79 @@ namespace Void::Sqlite
|
||||
return rows;
|
||||
}
|
||||
|
||||
BusyTimeoutOverride::BusyTimeoutOverride(
|
||||
std::shared_ptr<detail::ConnectionState> conn,
|
||||
std::chrono::milliseconds previous) noexcept
|
||||
: m_conn{std::move(conn)}
|
||||
, m_previous{previous}
|
||||
, m_active{true}
|
||||
{
|
||||
}
|
||||
|
||||
BusyTimeoutOverride::~BusyTimeoutOverride()
|
||||
{
|
||||
restore();
|
||||
}
|
||||
|
||||
BusyTimeoutOverride::BusyTimeoutOverride(BusyTimeoutOverride&& other) noexcept
|
||||
: m_conn{std::move(other.m_conn)}
|
||||
, m_previous{other.m_previous}
|
||||
, m_active{other.m_active}
|
||||
{
|
||||
other.m_active = false;
|
||||
}
|
||||
|
||||
BusyTimeoutOverride&
|
||||
BusyTimeoutOverride::operator=(BusyTimeoutOverride&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
restore();
|
||||
m_conn = std::move(other.m_conn);
|
||||
m_previous = other.m_previous;
|
||||
m_active = other.m_active;
|
||||
other.m_active = false;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void BusyTimeoutOverride::restore() noexcept
|
||||
{
|
||||
if (!m_active) {
|
||||
return;
|
||||
}
|
||||
m_active = false;
|
||||
if (!m_conn || m_conn->db == nullptr || m_conn->closed) {
|
||||
return;
|
||||
}
|
||||
const int clamped = static_cast<int>(std::clamp<std::int64_t>(
|
||||
m_previous.count(), 0, std::numeric_limits<int>::max()));
|
||||
(void)sqlite3_busy_timeout(m_conn->db, clamped);
|
||||
m_conn->busy_timeout = std::chrono::milliseconds{clamped};
|
||||
}
|
||||
|
||||
Result<BusyTimeoutOverride>
|
||||
Database::override_busy_timeout(std::chrono::milliseconds timeout)
|
||||
{
|
||||
if (timeout.count() < 0) {
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::invalid_argument,
|
||||
"Sqlite: busy timeout must be non-negative"));
|
||||
}
|
||||
if (!m_conn || m_conn->db == nullptr || m_conn->closed) {
|
||||
return Void::Result::fail(make_sqlite_error(
|
||||
SqliteErrorCode::closed, "Sqlite: database is closed"));
|
||||
}
|
||||
const int clamped = static_cast<int>(std::clamp<std::int64_t>(
|
||||
timeout.count(), 0, std::numeric_limits<int>::max()));
|
||||
if (sqlite3_busy_timeout(m_conn->db, clamped) != SQLITE_OK) {
|
||||
return Void::Result::fail(
|
||||
sqlite_error_from_db(m_conn->db, SQLITE_ERROR, "busy_timeout"));
|
||||
}
|
||||
const auto previous = m_conn->busy_timeout;
|
||||
m_conn->busy_timeout = std::chrono::milliseconds{clamped};
|
||||
return BusyTimeoutOverride{m_conn, previous};
|
||||
}
|
||||
|
||||
Result<void> Database::close()
|
||||
{
|
||||
if (!m_conn || m_conn->db == nullptr || m_conn->closed) {
|
||||
|
||||
Reference in new issue
Block a user