Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:2giosangmitom Sqmeow nvim Connection Retry Logic

From Leeroopedia
Knowledge Sources
Domains Optimization, Connection_Management, Reliability
Last Updated 2026-10-16 14:00 GMT

Overview

Tribal knowledge for the connect_retrying_with pattern that retries transient connection-establishment failures for up to 10 seconds, using driver-specific error classification — but never retries queries or writes.

Description

When opening a database connection, the engine retries transient failures (e.g. the server momentarily dropped the connection) for up to CONNECT_TIMEOUT (10 seconds). Between retries, it sleeps 250ms. Whether an error is retryable is determined by a driver-specific retryable function — what PostgreSQL considers transient may differ from MySQL or ScyllaDB.

The critical constraint is that this pattern is used only for connection establishment, never for query or write replay. A sent write whose outcome is unknown (e.g. commit succeeded but the response was lost) cannot be safely retried blindly — the data may already be committed. The engine warns users to verify data state before retrying in these cases.

Usage

Use this heuristic when:

  • Diagnosing connection timeouts: If a connection takes up to 10 seconds to establish, it may be retrying transient failures. Check SQMEOW_LOG=debug for "the server cut the connection off; trying again" messages.
  • Understanding write failure messages: Messages like "verify before retrying" indicate a write with unknown outcome — do not blindly re-run.
  • Adding a new adapter: Implement a retryable function that classifies the driver's transient vs. permanent errors, and use connect_retrying_with for connection opening.

The Insight (Rule of Thumb)

  • Action: Use connect_retrying_with for connection establishment only. Never use it to replay queries or writes.
  • Value: CONNECT_TIMEOUT = 10s, retry interval = 250ms, deadline-bounded loop.
  • Trade-off: Retrying improves reliability against transient network blips but adds up to 10s of latency. Non-retryable errors fail immediately.
  • Compatibility: Each driver defines its own retryable predicate. PostgreSQL, MySQL, and others have dialect-specific classifications. The await_sent pattern complements this for non-transactional writes.

Reasoning

Database servers may momentarily refuse or drop connections during startup, failover, or network instability. Retrying with a short deadline (10s) covers these transient cases without blocking the editor indefinitely. The 250ms sleep between retries prevents hammering the server.

The never retry writes rule is critical: a commit may have succeeded on the server but the response was lost in transit. Replaying the write would duplicate it (e.g. double-insert). Instead, the engine reports "outcome may be unknown; verify before retrying" and expects the user to check the data state.

This is why connect_retrying_with is named connect — it scopes the retry to the connection phase, not the query/write phase. The retryable function further filters to only truly transient errors (e.g. connection reset, timeout) and not permanent ones (e.g. auth failure).

Code Evidence

connect_retrying_with from core/sqmeow-adapters/src/lib.rs:72-90:

/// Retry transient connection-establishment failures, using the driver's error classification.
/// Never use this to replay queries or writes whose outcome might already be committed.
pub(crate) async fn connect_retrying_with<T, E, F, Fut>(
    mut open: F,
    retryable: impl Fn(&E) -> bool,
) -> std::result::Result<T, E>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = std::result::Result<T, E>>,
{
    let deadline = tokio::time::Instant::now() + CONNECT_TIMEOUT;
    loop {
        match open().await {
            Err(error) if retryable(&error) && tokio::time::Instant::now() < deadline => {
                tracing::debug!("the server cut the connection off; trying again");
                tokio::time::sleep(std::time::Duration::from_millis(250)).await;
            }
            outcome => return outcome,
        }
    }
}

CONNECT_TIMEOUT constant from core/sqmeow-adapters/src/lib.rs:43:

/// How long to keep trying to open a connection before giving up.
pub const CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

PostgreSQL retryable classification from core/sqmeow-adapters/src/postgres/native.rs:366:

fn retryable(error: &tokio_postgres::Error) -> bool {
    // ... classifies transient vs permanent errors
}

MySQL retryable classification from core/sqmeow-adapters/src/mysql/native.rs:449:

pub(super) fn retryable(error: &mysql_async::Error) -> bool {
    // ... classifies transient vs permanent errors
}

Write outcome warning from core/sqmeow-adapters/src/postgres/native.rs:400:

"PostgreSQL commit outcome unknown; verify edits before retrying: {error}"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment