Skip to content

Implement Graceful Shutdown with Connection Draining - #145

Open
veemakama wants to merge 2 commits into
StellarConduit:mainfrom
veemakama:main
Open

Implement Graceful Shutdown with Connection Draining#145
veemakama wants to merge 2 commits into
StellarConduit:mainfrom
veemakama:main

Conversation

@veemakama

Copy link
Copy Markdown

Closes #119

- Add tokio-util dep for CancellationToken
- Add shutdown: CancellationToken param to run_gossip_loop; cancelled()
  branch is the first arm in select! so it is always checked
- Add disconnect_all() to TransportManager; drains active_connections
  and clears pending_messages; shutdown() delegates to it
- Add connected_peers() helper to TransportManager
- Add close() to MeshDatabase; issues PRAGMA wal_checkpoint(TRUNCATE)
  before returning
- Implement full shutdown sequence in stellarconduitd.rs:
  CancellationToken root + child token for gossip, JoinHandle stored,
  SIGTERM handled alongside ctrl_c, 2 s pending-message flush,
  3 s gossip join timeout, disconnect_all, db.close()

Tests added:
- test_gossip_loop_exits_on_cancellation (protocol.rs)
- test_disconnect_all_clears_connections (unified.rs)
- test_database_close_checkpoints_wal (persistence_test.rs)
- test_daemon_exits_within_5_seconds (daemon_shutdown_test.rs)

Closes StellarConduit#47
…n-draining

feat(daemon): implement graceful shutdown with connection draining
Changes
Cargo.toml
Add tokio-util = { version = "0.7", features = ["rt"] } for CancellationToken
src/gossip/protocol.rs
Add shutdown: CancellationToken parameter to run_gossip_loop
Add shutdown.cancelled() as the first select! arm so it is always polled
src/transport/unified.rs
Add disconnect_all(): drains active_connections, clears pending_messages, logs completion
Add connected_peers(): returns pubkeys of all live connections
shutdown() now delegates to disconnect_all() (backwards compatible)
src/persistence/db.rs
Add close(): issues PRAGMA wal_checkpoint(TRUNCATE) via conn.call, then logs
src/bin/stellarconduitd.rs
Full shutdown sequence:

CancellationToken root; child token passed to gossip loop
Handles both ctrl_c and SIGTERM
Calls shutdown.cancel()
2 s deadline to flush GossipState::active_queue to connected peers
3 s timeout to join the gossip JoinHandle
disconnect_all() on the transport manager
db.close() for WAL checkpoint
Tests added
Test	Location
test_gossip_loop_exits_on_cancellation	src/gossip/protocol.rs
test_disconnect_all_clears_connections	src/transport/unified.rs
test_database_close_checkpoints_wal	tests/persistence_test.rs
test_daemon_exits_within_5_seconds	tests/daemon_shutdown_test.rs
Acceptance criteria
 run_gossip_loop exits cleanly when its CancellationToken is cancelled
 TransportManager::disconnect_all() exists and drains active_connections
 MeshDatabase::close() issues WAL checkpoint before returning
 Daemon handles SIGTERM and exits within 5 s
 Pending queue flushed to connected peers before shutdown (2 s deadline)
 All existing tests updated to pass the new shutdown arg
Closes StellarConduit#119
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.

Implement Graceful Shutdown with Connection Draining

1 participant