Skip to content

perf(connections): reconnect when the network returns and ping less while inactive #4546

Done

Description

@datlechin

Problem

  • After a Wi-Fi drop or a VPN reconnect, a session can wait up to 120 s after the network is back before it reconnects. Attempts made while the network is down count against the 10-attempt budget, so an outage of about 8 minutes or more leaves the session unreachable even though nothing is wrong with the server. Outages under about 30 s recover within a few seconds.
  • On wake, only tunneled sessions are checked; direct sessions wait for first use.
  • The default health check pings every connection every 30 s, also while the app sits in the background: about 20 pings a minute with 10 connections. It keeps serverless databases awake and wakes the network.
  • The ping runs outside the session driver gate, so a table open or query that starts during a ping waits up to one RTT behind it.

Cause

  • runRecovery sleeps ExponentialBackoff.delay(for:maxDelay:) between attempts, capped by maxRecoveryDelay = 120 (apple/TablePro/Core/Database/DatabaseManager+Recovery.swift:44, :162-184). The delays are 2, 4, 8, 16, 32, 64, then 120 s (apple/Packages/TableProMacCore/Sources/TableProUtilities/Connection/ExponentialBackoff.swift:3-14), and the budget is maxAttempts = 10 (apple/TablePro/Models/Connection/ConnectionWindowPhase.swift:105). Nothing wakes the sleep when the path comes back.
  • The only NWPathMonitor is SyncNetworkMonitor, used for iCloud sync (apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncNetworkMonitor.swift:10-33). DatabaseManager has no reachability input.
  • handleSystemDidWake validates tunneled sessions only (apple/TablePro/Core/Database/DatabaseManager+SystemEvents.swift:22-40).
  • The default is .every30Seconds (apple/TablePro/Models/Settings/GeneralSettings.swift:79). The monitor sleeps a fixed interval and reads the interval from settings on the main actor twice per pass (apple/TablePro/Core/Database/ConnectionHealthMonitor.swift:69-90). Nothing depends on whether the app is active, though verifyBeforeUse already checks a connection older than 300 s when the user reaches for it (apple/TablePro/Core/Database/DatabaseManager+Verification.swift:38-56, apple/TablePro/Models/Settings/ConnectionHealthCheck.swift:29).
  • The ping handler skips only while queriesInFlight is set at that instant, then calls mainDriver.ping() directly (apple/TablePro/Core/Database/DatabaseManager+Health.swift:41-60, :54). The driver runs it on the same queue as user work (apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift:286-291).

Overlaps #4462: WI13 already covers counting an answered statement as a connection check, so a busy connection skips the ping. This issue adds the network wake-up, the inactive interval and the gate.

Change

  • Generalize SyncNetworkMonitor into one path monitor that DatabaseManager also observes (Network framework, the documented reachability API). While the path is unsatisfied, recovery loops wait without spending attempts. When it becomes satisfied, or its interface changes, cancel every waiting loop's sleep so each retries at once. Backoff still applies to real server failures.
  • On a path change and on wake, check direct sessions as well as tunneled ones.
  • While NSApp is inactive, stretch the health-check interval to ConnectionHealthCheck.freshness and let verifyBeforeUse cover the return; restore the configured interval on didBecomeActive. Read the interval once per pass.
  • Send the ping as a turn on the session driver gate. When the gate is held, skip the ping: the work holding it reports a dead connection itself.
  • SSH keepalive stays in LibSSH2KeepAlive.

Measure

  • 5 connections open, Wi-Fi off for 3 minutes, then on: time from path satisfied to every session live. Today up to 120 s; target about one connect time. Repeat with a 10-minute outage: no session ends unreachable.
  • 10 connections, app in the background for 15 minutes: pings per minute from signposts or the server log, and idle wake-ups in Activity Monitor. Today about 20 pings a minute; target at most one per connection per 300 s.
  • A query started during a ping: no wait behind it (signpost on the gate turn).

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #4582 ships the network wake-up, the direct-session checks on path change and wake, the inactive interval, the single interval read and the gated ping. Notes:

    • The path monitor is a new app-side ConnectionNetworkMonitor rather than a generalized SyncNetworkMonitor: that one lives in TableProSyncTransport, which the iOS app links too, and reports only a return to a usable path, where a reconnect also needs an interface change. Folding the two into one monitor is left for a follow-up.
    • Only a connection that provably stays on this Mac (a local socket, or a network engine whose Host field is loopback) redials with no path; a tunnel, an empty Host or an engine that reads its address from its own fields waits for the path.
    • The Wi-Fi-off timing runs and the background ping count from signposts were not run here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions