Skip to content

fix: prevent excessive idle ping traffic from health monitor#620

Merged
datlechin merged 2 commits intomainfrom
fix/excessive-idle-ping-traffic
Apr 7, 2026
Merged

fix: prevent excessive idle ping traffic from health monitor#620
datlechin merged 2 commits intomainfrom
fix/excessive-idle-ping-traffic

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

Fixes #618 — ConnectionHealthMonitor was sending 40-50 SELECT 1 pings per second when idle instead of 1 per 30 seconds.

Two bugs fixed:

  1. AsyncStream iterator recreated every loop iteration (ConnectionHealthMonitor.swift:113): A new makeAsyncIterator() call each cycle drained any buffered yield() values instantly, causing back-to-back pings with zero delay. Fixed by creating the iterator once before the loop and reusing it.

  2. Orphaned monitor tasks from non-awaited stopMonitoring() (ConnectionHealthMonitor.swift:136): stopMonitoring() cancelled the task but didn't await its completion. When startHealthMonitor was called (on reconnect, SSH tunnel recovery), the old task could still be alive and pinging alongside the new one. Fixed by making stopMonitoring() async and awaiting task.value.

Test plan

  • Connect to a PostgreSQL database (especially cloud-hosted like PlanetScale/Neon with higher latency)
  • Leave idle for 2+ minutes, verify only ~1 ping per 30 seconds in server logs
  • Trigger manual reconnect, verify no duplicate ping streams
  • Kill SSH tunnel (if applicable), verify reconnect loop doesn't spawn multiple monitors
  • Verify checkNow() still triggers an immediate single ping when called

Reuse AsyncStream iterator across loop iterations instead of creating a
new one each cycle, which drained buffered yields instantly causing
back-to-back pings. Make stopMonitoring() await task completion to
prevent orphaned monitors from pinging concurrently.

Closes #618
@datlechin datlechin merged commit 826ccc4 into main Apr 7, 2026
2 checks passed
@datlechin datlechin deleted the fix/excessive-idle-ping-traffic branch April 7, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Excessive idle PostgreSQL ping traffic

1 participant