grpc_graphql_gateway 1.2.4

A Rust implementation of gRPC-GraphQL gateway - generates GraphQL execution code from gRPC services
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Graceful shutdown support for the GraphQL gateway.
//!
//! This module provides utilities for gracefully shutting down the gateway,
//! ensuring that in-flight requests complete and resources are properly cleaned up.
//!
//! # Features
//!
//! - Signal handling (SIGTERM, SIGINT, Ctrl+C)
//! - Configurable shutdown timeout
//! - In-flight request draining
//! - Subscription cleanup
//!
//! # Example
//!
//! ```rust,no_run
//! use grpc_graphql_gateway::{Gateway, ShutdownConfig};
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! Gateway::builder()
//!     // ... configuration ...
//!     .with_graceful_shutdown(ShutdownConfig {
//!         timeout: Duration::from_secs(30),
//!         ..Default::default()
//!     })
//!     .serve("0.0.0.0:8080")
//!     .await?;
//! # Ok(())
//! # }
//! ```

use std::future::Future;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{broadcast, watch};
use tracing::{debug, info, warn};

/// Configuration for graceful shutdown behavior.
#[derive(Debug, Clone)]
pub struct ShutdownConfig {
    /// Maximum time to wait for in-flight requests to complete (default: 30 seconds)
    pub timeout: Duration,
    /// Whether to handle OS signals (SIGTERM, SIGINT) automatically (default: true)
    pub handle_signals: bool,
    /// Grace period before forceful shutdown after timeout (default: 5 seconds)
    pub force_shutdown_delay: Duration,
}

impl Default for ShutdownConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            handle_signals: true,
            force_shutdown_delay: Duration::from_secs(5),
        }
    }
}

/// Shutdown coordinator that manages the graceful shutdown process.
#[derive(Clone)]
pub struct ShutdownCoordinator {
    /// Signal to stop accepting new connections
    shutdown_tx: broadcast::Sender<()>,
    /// Watch channel for shutdown state
    state_tx: Arc<watch::Sender<ShutdownState>>,
    state_rx: watch::Receiver<ShutdownState>,
    /// Count of active connections/requests
    active_count: Arc<AtomicUsize>,
    /// Whether shutdown has been initiated
    is_shutting_down: Arc<AtomicBool>,
    /// Configuration
    config: ShutdownConfig,
}

/// Current state of the shutdown process.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShutdownState {
    /// Server is running normally
    Running,
    /// Shutdown initiated, draining requests
    Draining,
    /// Shutdown complete
    Shutdown,
}

impl ShutdownCoordinator {
    /// Create a new shutdown coordinator with the given configuration.
    pub fn new(config: ShutdownConfig) -> Self {
        let (shutdown_tx, _) = broadcast::channel(1);
        let (state_tx, state_rx) = watch::channel(ShutdownState::Running);

        Self {
            shutdown_tx,
            state_tx: Arc::new(state_tx),
            state_rx,
            active_count: Arc::new(AtomicUsize::new(0)),
            is_shutting_down: Arc::new(AtomicBool::new(false)),
            config,
        }
    }

    /// Create a shutdown coordinator with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(ShutdownConfig::default())
    }

    /// Check if shutdown has been initiated.
    pub fn is_shutting_down(&self) -> bool {
        self.is_shutting_down.load(Ordering::SeqCst)
    }

    /// Get the current number of active requests/connections.
    pub fn active_count(&self) -> usize {
        self.active_count.load(Ordering::SeqCst)
    }

    /// Subscribe to shutdown signals.
    pub fn subscribe(&self) -> broadcast::Receiver<()> {
        self.shutdown_tx.subscribe()
    }

    /// Watch the shutdown state.
    pub fn watch_state(&self) -> watch::Receiver<ShutdownState> {
        self.state_rx.clone()
    }

    /// Increment the active request count (call when a request starts).
    pub fn request_started(&self) {
        self.active_count.fetch_add(1, Ordering::SeqCst);
    }

    /// Decrement the active request count (call when a request completes).
    pub fn request_completed(&self) {
        let prev = self.active_count.fetch_sub(1, Ordering::SeqCst);
        debug!(active_count = prev - 1, "Request completed");
    }

    /// Create a guard that automatically tracks request lifecycle.
    pub fn request_guard(&self) -> RequestGuard {
        self.request_started();
        RequestGuard {
            coordinator: self.clone(),
        }
    }

    /// Initiate graceful shutdown.
    ///
    /// This will:
    /// 1. Stop accepting new connections
    /// 2. Wait for in-flight requests to complete (up to timeout)
    /// 3. Force shutdown if timeout is exceeded
    pub async fn shutdown(&self) {
        if self.is_shutting_down.swap(true, Ordering::SeqCst) {
            debug!("Shutdown already in progress");
            return;
        }

        info!("Initiating graceful shutdown...");

        // Update state to draining
        let _ = self.state_tx.send(ShutdownState::Draining);

        // Notify all subscribers
        let _ = self.shutdown_tx.send(());

        // Wait for active requests to complete
        let start = std::time::Instant::now();
        let timeout = self.config.timeout;

        loop {
            let active = self.active_count();

            if active == 0 {
                info!("All requests completed, shutting down");
                break;
            }

            if start.elapsed() >= timeout {
                warn!(
                    active_count = active,
                    "Shutdown timeout reached, {} requests still active", active
                );
                break;
            }

            debug!(
                active_count = active,
                elapsed_secs = start.elapsed().as_secs(),
                "Waiting for {} active request(s) to complete",
                active
            );

            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        // Force shutdown delay
        if self.active_count() > 0 {
            warn!(
                "Waiting {} seconds before forcing shutdown...",
                self.config.force_shutdown_delay.as_secs()
            );
            tokio::time::sleep(self.config.force_shutdown_delay).await;
        }

        // Update state to shutdown
        let _ = self.state_tx.send(ShutdownState::Shutdown);

        info!("Graceful shutdown complete");
    }

    /// Create a future that completes when shutdown is signaled.
    ///
    /// This is useful for passing to `axum::serve().with_graceful_shutdown()`.
    pub fn shutdown_signal(&self) -> impl Future<Output = ()> + Send + 'static {
        let mut rx = self.subscribe();
        async move {
            let _ = rx.recv().await;
        }
    }
}

/// RAII guard that tracks request lifecycle.
///
/// Automatically decrements the active count when dropped.
pub struct RequestGuard {
    coordinator: ShutdownCoordinator,
}

impl Drop for RequestGuard {
    fn drop(&mut self) {
        self.coordinator.request_completed();
    }
}

/// Create a future that completes on SIGTERM or SIGINT (Ctrl+C).
///
/// This is useful for Unix-like platforms where you want to handle
/// OS shutdown signals.
#[cfg(unix)]
pub async fn signal_shutdown() {
    use tokio::signal::unix::{signal, SignalKind};

    let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler");
    let mut sigint = signal(SignalKind::interrupt()).expect("failed to install SIGINT handler");

    tokio::select! {
        _ = sigterm.recv() => {
            info!("Received SIGTERM");
        }
        _ = sigint.recv() => {
            info!("Received SIGINT (Ctrl+C)");
        }
    }
}

/// Create a future that completes on Ctrl+C.
///
/// This works on all platforms.
#[cfg(not(unix))]
pub async fn signal_shutdown() {
    tokio::signal::ctrl_c()
        .await
        .expect("failed to install Ctrl+C handler");
    info!("Received Ctrl+C");
}

/// Create a combined shutdown signal that triggers on OS signals.
pub async fn os_signal_shutdown() {
    signal_shutdown().await;
}

/// Helper to run a server with graceful shutdown.
///
/// # Example
///
/// ```rust,no_run
/// use grpc_graphql_gateway::shutdown::{run_with_graceful_shutdown, ShutdownConfig};
/// use axum::Router;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let app = Router::new();
/// let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
///
/// run_with_graceful_shutdown(
///     listener,
///     app,
///     ShutdownConfig::default(),
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn run_with_graceful_shutdown(
    listener: tokio::net::TcpListener,
    app: axum::Router,
    config: ShutdownConfig,
) -> std::io::Result<()> {
    let coordinator = ShutdownCoordinator::new(config.clone());
    let coordinator_clone = coordinator.clone();

    // Spawn a task to handle OS signals
    if config.handle_signals {
        let coordinator_for_signal = coordinator.clone();
        tokio::spawn(async move {
            signal_shutdown().await;
            coordinator_for_signal.shutdown().await;
        });
    }

    // Run the server with graceful shutdown
    axum::serve(listener, app)
        .with_graceful_shutdown(coordinator_clone.shutdown_signal())
        .await?;

    // Ensure shutdown completes
    coordinator.shutdown().await;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Instant;

    #[tokio::test]
    async fn test_shutdown_resilience() {
        let config = ShutdownConfig {
            timeout: Duration::from_millis(50),
            handle_signals: false,
            force_shutdown_delay: Duration::from_millis(10),
        };
        let coordinator = ShutdownCoordinator::new(config);

        // Start a 'stuck' request
        coordinator.request_started();

        let start = Instant::now();
        coordinator.shutdown().await;
        let duration = start.elapsed();

        // Should have waited at least timeout
        assert!(duration >= Duration::from_millis(50));
        // But shouldn't be stuck forever
        assert!(duration < Duration::from_secs(5));

        // Should still show active count (it was forced)
        assert_eq!(coordinator.active_count(), 1);
        assert_eq!(*coordinator.watch_state().borrow(), ShutdownState::Shutdown);
    }

    #[tokio::test]
    async fn test_shutdown_coordinator_creation() {
        let coordinator = ShutdownCoordinator::with_defaults();
        assert!(!coordinator.is_shutting_down());
        assert_eq!(coordinator.active_count(), 0);
    }

    #[tokio::test]
    async fn test_request_tracking() {
        let coordinator = ShutdownCoordinator::with_defaults();

        coordinator.request_started();
        assert_eq!(coordinator.active_count(), 1);

        coordinator.request_started();
        assert_eq!(coordinator.active_count(), 2);

        coordinator.request_completed();
        assert_eq!(coordinator.active_count(), 1);

        coordinator.request_completed();
        assert_eq!(coordinator.active_count(), 0);
    }

    #[tokio::test]
    async fn test_request_guard() {
        let coordinator = ShutdownCoordinator::with_defaults();
        assert_eq!(coordinator.active_count(), 0);

        {
            let _guard = coordinator.request_guard();
            assert_eq!(coordinator.active_count(), 1);
        }

        // Guard dropped, count should be back to 0
        assert_eq!(coordinator.active_count(), 0);
    }

    #[tokio::test]
    async fn test_shutdown_idempotency() {
        let coordinator = ShutdownCoordinator::with_defaults();

        let c1 = coordinator.clone();
        let task1 = tokio::spawn(async move {
            c1.shutdown().await;
        });

        let c2 = coordinator.clone();
        let task2 = tokio::spawn(async move {
            c2.shutdown().await;
        });

        let _ = tokio::join!(task1, task2);

        assert!(coordinator.is_shutting_down());
    }

    #[tokio::test]
    async fn test_shutdown_with_no_active_requests() {
        let config = ShutdownConfig {
            timeout: Duration::from_millis(100),
            handle_signals: false,
            force_shutdown_delay: Duration::from_millis(10),
        };
        let coordinator = ShutdownCoordinator::new(config);

        // Shutdown should complete immediately with no active requests
        let start = std::time::Instant::now();
        coordinator.shutdown().await;
        assert!(start.elapsed() < Duration::from_millis(50));
        assert!(coordinator.is_shutting_down());
    }

    #[tokio::test]
    async fn test_shutdown_waits_for_active_requests() {
        let config = ShutdownConfig {
            timeout: Duration::from_secs(5),
            handle_signals: false,
            force_shutdown_delay: Duration::from_millis(10),
        };
        let coordinator = ShutdownCoordinator::new(config);

        // Simulate an active request
        let coordinator_clone = coordinator.clone();
        tokio::spawn(async move {
            let _guard = coordinator_clone.request_guard();
            tokio::time::sleep(Duration::from_millis(200)).await;
        });

        // Give time for the guard to be created
        tokio::time::sleep(Duration::from_millis(10)).await;
        assert_eq!(coordinator.active_count(), 1);

        // Shutdown should wait for the request to complete
        coordinator.shutdown().await;
        assert_eq!(coordinator.active_count(), 0);
    }

    #[tokio::test]
    async fn test_shutdown_signal_propagation() {
        let coordinator = ShutdownCoordinator::with_defaults();
        let mut rx = coordinator.subscribe();

        let handle = tokio::spawn(async move { rx.recv().await });

        coordinator.shutdown().await;

        assert!(handle.await.is_ok());
    }

    #[tokio::test]
    async fn test_shutdown_state_transitions() {
        let config = ShutdownConfig {
            timeout: Duration::from_millis(100),
            handle_signals: false,
            force_shutdown_delay: Duration::from_millis(10),
        };
        let coordinator = ShutdownCoordinator::new(config);

        let mut state_rx = coordinator.watch_state();
        assert_eq!(*state_rx.borrow(), ShutdownState::Running);

        // Trigger shutdown in bg
        let c_clone = coordinator.clone();
        tokio::spawn(async move {
            c_clone.shutdown().await;
        });

        // It should eventually reach Shutdown
        // It might pass through Draining
        while state_rx.changed().await.is_ok() {
            let state = *state_rx.borrow();
            if state == ShutdownState::Shutdown {
                break;
            }
        }

        assert_eq!(*state_rx.borrow(), ShutdownState::Shutdown);
    }
}