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
@@ -242,6 +242,40 @@ namespace Void::Sqlite
|
||||
bool m_active{false};
|
||||
};
|
||||
|
||||
/// Scoped per-call override of the connection busy timeout.
|
||||
///
|
||||
/// The previous timeout is restored when this object is destroyed (or
|
||||
/// explicitly via restore()), so a single statement or transaction can use
|
||||
/// a tighter or looser bound without permanently changing the connection.
|
||||
class BusyTimeoutOverride
|
||||
{
|
||||
public:
|
||||
BusyTimeoutOverride() noexcept = default;
|
||||
~BusyTimeoutOverride();
|
||||
BusyTimeoutOverride(BusyTimeoutOverride&&) noexcept;
|
||||
BusyTimeoutOverride& operator=(BusyTimeoutOverride&&) noexcept;
|
||||
BusyTimeoutOverride(const BusyTimeoutOverride&) = delete;
|
||||
BusyTimeoutOverride& operator=(const BusyTimeoutOverride&) = delete;
|
||||
|
||||
[[nodiscard]] bool is_active() const noexcept { return m_active; }
|
||||
[[nodiscard]] std::chrono::milliseconds previous() const noexcept
|
||||
{
|
||||
return m_previous;
|
||||
}
|
||||
|
||||
/// Restore the previous timeout early. Idempotent.
|
||||
void restore() noexcept;
|
||||
|
||||
private:
|
||||
friend class Database;
|
||||
BusyTimeoutOverride(std::shared_ptr<detail::ConnectionState> conn,
|
||||
std::chrono::milliseconds previous) noexcept;
|
||||
|
||||
std::shared_ptr<detail::ConnectionState> m_conn{};
|
||||
std::chrono::milliseconds m_previous{0};
|
||||
bool m_active{false};
|
||||
};
|
||||
|
||||
/// A single forward schema migration applied when the database's
|
||||
/// user_version is below `version`.
|
||||
struct Migration {
|
||||
@@ -300,6 +334,12 @@ namespace Void::Sqlite
|
||||
std::size_t max_rows = 0,
|
||||
std::size_t max_bytes = 0);
|
||||
|
||||
/// Temporarily override the connection busy timeout for the next
|
||||
/// calls. The previous value is restored when the returned scope is
|
||||
/// destroyed. A negative timeout is rejected.
|
||||
[[nodiscard]] Result<BusyTimeoutOverride>
|
||||
override_busy_timeout(std::chrono::milliseconds timeout);
|
||||
|
||||
/// Explicitly close the connection. Stops admission and returns
|
||||
/// SqliteErrorCode::busy while live statements/transactions/savepoints
|
||||
/// remain open.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -674,3 +674,45 @@ Void::Testing::Test sqlite_native_result_codes_are_structural()
|
||||
Void::Testing::require(!invalid.has_value());
|
||||
Void::Testing::expect(!sqlite_native_code(invalid.error()).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test sqlite_busy_timeout_override_is_scoped()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TempSqliteDir dir;
|
||||
auto db_res = open_database(dir.path / "busy.db");
|
||||
Void::Testing::require(db_res.has_value());
|
||||
auto db = std::move(*db_res);
|
||||
|
||||
// Nested overrides restore the exact previous value, not the default.
|
||||
{
|
||||
auto outer = db.override_busy_timeout(200ms);
|
||||
Void::Testing::require(outer.has_value());
|
||||
Void::Testing::expect(outer->is_active());
|
||||
Void::Testing::expect_eq(outer->previous(), 5000ms);
|
||||
|
||||
{
|
||||
auto inner = db.override_busy_timeout(10ms);
|
||||
Void::Testing::require(inner.has_value());
|
||||
Void::Testing::expect_eq(inner->previous(), 200ms);
|
||||
}
|
||||
|
||||
auto after_inner = db.override_busy_timeout(1ms);
|
||||
Void::Testing::require(after_inner.has_value());
|
||||
Void::Testing::expect_eq(after_inner->previous(), 200ms);
|
||||
after_inner->restore();
|
||||
Void::Testing::expect(!after_inner->is_active());
|
||||
after_inner->restore(); // idempotent
|
||||
}
|
||||
|
||||
auto restored = db.override_busy_timeout(2ms);
|
||||
Void::Testing::require(restored.has_value());
|
||||
Void::Testing::expect_eq(restored->previous(), 5000ms);
|
||||
|
||||
// A negative timeout is rejected without changing the connection.
|
||||
auto negative = db.override_busy_timeout(-1ms);
|
||||
Void::Testing::require(!negative.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
negative.error().code().value,
|
||||
static_cast<std::int32_t>(SqliteErrorCode::invalid_argument));
|
||||
}
|
||||
Reference in new issue
Block a user