appcore-gateway 1.0.3-rc

Multi-tenant Gateway capability for the AppCore Runtime.
Documentation
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
// =============================================================================
//        #######
//     ###       ###     F: runtime.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/20 00:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/20 00:00:00 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

//! Owned listener, task, health, and shutdown lifecycle for one Gateway.

use crate::{
    make_gateway_router, spawn_heartbeat_pruner, GatewayConfig, GatewayError, GatewayHaCoordinator,
    GatewayHaCoordinatorSnapshot, GatewayHaOwnershipSource, GatewayMetrics, GatewayResult,
    GatewayState, GatewayTelemetrySnapshot,
};
use appcore_peer_rpc::{BoundedReplayStore, PeerNonceStore, ReplayStoreConfig};
use appcore_security::HashTokenProvider;
use parking_lot::Mutex;
use std::future::IntoFuture;
use std::net::{SocketAddr, TcpListener};
use std::sync::Arc;
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use tokio::sync::watch;

const MAX_SHUTDOWN_JOIN_RESERVE: Duration = Duration::from_millis(100);

/// Concrete execution state of a Gateway runtime instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GatewayRuntimeState {
    /// No listener or task is active.
    Stopped,
    /// Listener preparation is in progress.
    Starting,
    /// The listener and owned tasks are running.
    Running,
    /// Cooperative shutdown is in progress.
    Stopping,
    /// The runtime terminated with a controlled failure.
    Failed,
    /// The runtime thread failed to honor even the forced shutdown deadline.
    Orphaned,
}

/// Safe point-in-time Gateway lifecycle and metric snapshot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GatewayRuntimeSnapshot {
    /// Current execution state.
    pub state: GatewayRuntimeState,
    /// Deployment-configured listener address.
    pub configured_bind_address: SocketAddr,
    /// Actual bound address while or after an instance has run.
    pub bound_address: Option<SocketAddr>,
    /// Active authenticated worker sockets.
    pub active_workers: u64,
    /// Active authenticated client sockets.
    pub active_clients: u64,
    /// Successfully routed messages since this instance started.
    pub messages_routed: u64,
    /// Failed routing attempts since this instance started.
    pub routing_failures: u64,
    /// Detailed bounded vendor-neutral route telemetry.
    pub telemetry: GatewayTelemetrySnapshot,
    /// Opt-in shared-registry ownership state, when HA is configured.
    pub ha: Option<GatewayHaCoordinatorSnapshot>,
    /// Sanitized lifecycle failure, when present.
    pub last_error: Option<String>,
}

struct RunningGateway {
    shutdown: watch::Sender<Option<Duration>>,
    handle: JoinHandle<GatewayResult<()>>,
}

struct RuntimeInner {
    state: GatewayRuntimeState,
    running: Option<RunningGateway>,
    gateway_state: Option<Arc<GatewayState>>,
    bound_address: Option<SocketAddr>,
    last_error: Option<String>,
}

/// Restartable owner of one Gateway listener and all work spawned beneath it.
pub struct GatewayRuntime {
    config: GatewayConfig,
    token_provider: HashTokenProvider,
    connection_replay: Arc<dyn PeerNonceStore>,
    ha_coordinator: Option<Arc<GatewayHaCoordinator>>,
    inner: Mutex<RuntimeInner>,
}

impl GatewayRuntime {
    /// Creates a stopped runtime after validating owner-defined configuration.
    pub fn new(config: GatewayConfig, token_provider: HashTokenProvider) -> GatewayResult<Self> {
        Self::with_replay_store(
            config,
            token_provider,
            Arc::new(BoundedReplayStore::new(ReplayStoreConfig::default())),
        )
    }

    /// Creates a stopped runtime using an explicit durable or shared replay
    /// store for one-use Gateway connection credentials.
    pub fn with_replay_store(
        config: GatewayConfig,
        token_provider: HashTokenProvider,
        connection_replay: Arc<dyn PeerNonceStore>,
    ) -> GatewayResult<Self> {
        config.validate()?;
        Ok(Self {
            config,
            token_provider,
            connection_replay,
            ha_coordinator: None,
            inner: Mutex::new(RuntimeInner {
                state: GatewayRuntimeState::Stopped,
                running: None,
                gateway_state: None,
                bound_address: None,
                last_error: None,
            }),
        })
    }

    /// Creates a stopped HA runtime with an explicit shared replay store and
    /// shared-registry coordinator.
    pub fn with_ha_coordinator(
        config: GatewayConfig,
        token_provider: HashTokenProvider,
        connection_replay: Arc<dyn PeerNonceStore>,
        ha_coordinator: Arc<GatewayHaCoordinator>,
    ) -> GatewayResult<Self> {
        config.validate()?;
        Ok(Self {
            config,
            token_provider,
            connection_replay,
            ha_coordinator: Some(ha_coordinator),
            inner: Mutex::new(RuntimeInner {
                state: GatewayRuntimeState::Stopped,
                running: None,
                gateway_state: None,
                bound_address: None,
                last_error: None,
            }),
        })
    }

    /// Binds synchronously and starts the listener on one owned runtime thread.
    ///
    /// Bind, runtime construction, and thread creation failures are returned
    /// before this method reports success.
    pub fn start(&self) -> GatewayResult<()> {
        let mut inner = self.inner.lock();
        refresh_runtime(&mut inner);
        match inner.state {
            GatewayRuntimeState::Running => return Ok(()),
            GatewayRuntimeState::Starting | GatewayRuntimeState::Stopping => {
                return Err(GatewayError::Transport(
                    "gateway lifecycle transition already in progress".to_string(),
                ));
            }
            GatewayRuntimeState::Orphaned => {
                return Err(GatewayError::Transport(
                    "orphaned gateway instance cannot be restarted".to_string(),
                ));
            }
            GatewayRuntimeState::Stopped | GatewayRuntimeState::Failed => {}
        }
        inner.state = GatewayRuntimeState::Starting;
        match self.prepare_instance() {
            Ok(prepared) => {
                inner.bound_address = Some(prepared.bound_address);
                inner.gateway_state = Some(prepared.state);
                inner.running = Some(prepared.running);
                inner.last_error = None;
                inner.state = GatewayRuntimeState::Running;
                Ok(())
            }
            Err(error) => {
                inner.state = GatewayRuntimeState::Failed;
                inner.last_error = Some(error.to_string());
                Err(error)
            }
        }
    }

    /// Requests graceful shutdown, force-cancels the server future before the
    /// deadline when needed, and joins all owned listener and task work.
    pub fn stop(&self, timeout: Duration) -> GatewayResult<()> {
        let running = {
            let mut inner = self.inner.lock();
            refresh_runtime(&mut inner);
            let Some(running) = inner.running.take() else {
                if inner.state != GatewayRuntimeState::Orphaned {
                    inner.state = GatewayRuntimeState::Stopped;
                }
                return Ok(());
            };
            inner.state = GatewayRuntimeState::Stopping;
            running
                .shutdown
                .send_replace(Some(graceful_shutdown_budget(timeout)));
            running
        };
        let deadline = Instant::now().checked_add(timeout);
        while !running.handle.is_finished()
            && deadline.is_none_or(|deadline| Instant::now() < deadline)
        {
            std::thread::sleep(Duration::from_millis(10));
        }
        if !running.handle.is_finished() {
            let mut inner = self.inner.lock();
            inner.running = Some(running);
            inner.state = GatewayRuntimeState::Orphaned;
            inner.last_error = Some("gateway shutdown timed out".to_string());
            return Err(GatewayError::Transport(
                "gateway shutdown timed out".to_string(),
            ));
        }
        let result = join_runtime(running);
        let mut inner = self.inner.lock();
        inner.state = if result.is_ok() {
            GatewayRuntimeState::Stopped
        } else {
            GatewayRuntimeState::Failed
        };
        inner.last_error = result.as_ref().err().map(ToString::to_string);
        result
    }

    /// Returns lifecycle, listener, and bounded metric state without exposing
    /// credentials or token material.
    pub fn snapshot(&self) -> GatewayRuntimeSnapshot {
        let mut inner = self.inner.lock();
        refresh_runtime(&mut inner);
        let metrics = inner
            .gateway_state
            .as_ref()
            .map(|state| Arc::clone(&state.metrics));
        snapshot_from_parts(&self.config, &inner, metrics.as_deref())
    }

    fn prepare_instance(&self) -> GatewayResult<PreparedGateway> {
        let state = Arc::new(match &self.ha_coordinator {
            Some(coordinator) => GatewayState::with_ha_coordinator(
                self.config.clone(),
                self.token_provider.clone(),
                Arc::clone(&self.connection_replay),
                Arc::clone(coordinator),
            )?,
            None => GatewayState::with_replay_store(
                self.config.clone(),
                self.token_provider.clone(),
                Arc::clone(&self.connection_replay),
            )?,
        });
        let listener = bind_listener(self.config.bind_address)?;
        let bound_address = listener.local_addr().map_err(transport_error)?;
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(transport_error)?;
        let listener = {
            let _entered = runtime.enter();
            tokio::net::TcpListener::from_std(listener).map_err(transport_error)?
        };
        let (shutdown, shutdown_request) = watch::channel(None);
        let thread_state = Arc::clone(&state);
        let handle = std::thread::Builder::new()
            .name("appcore-gateway".to_string())
            .spawn(move || run_gateway(runtime, listener, thread_state, shutdown_request))
            .map_err(transport_error)?;
        Ok(PreparedGateway {
            bound_address,
            state,
            running: RunningGateway { shutdown, handle },
        })
    }
}

struct PreparedGateway {
    bound_address: SocketAddr,
    state: Arc<GatewayState>,
    running: RunningGateway,
}

impl Drop for GatewayRuntime {
    fn drop(&mut self) {
        let _ = self.stop(Duration::from_secs(10));
    }
}

fn bind_listener(address: SocketAddr) -> GatewayResult<TcpListener> {
    let listener = TcpListener::bind(address).map_err(|error| {
        GatewayError::Transport(format!(
            "failed to bind gateway listener {address}: {error}"
        ))
    })?;
    listener.set_nonblocking(true).map_err(transport_error)?;
    Ok(listener)
}

fn run_gateway(
    runtime: tokio::runtime::Runtime,
    listener: tokio::net::TcpListener,
    state: Arc<GatewayState>,
    mut shutdown: watch::Receiver<Option<Duration>>,
) -> GatewayResult<()> {
    runtime.block_on(async move {
        let coordinator = state.ha_coordinator().map(|coordinator| {
            let source: Arc<dyn GatewayHaOwnershipSource> = state.clone();
            tokio::spawn(coordinator.run(source, state.subscribe_shutdown()))
        });
        let pruner = spawn_heartbeat_pruner(
            Arc::clone(&state),
            state.config().heartbeat_interval,
            state.config().heartbeat_timeout,
        );
        let router = make_gateway_router(Arc::clone(&state));
        let graceful_state = Arc::clone(&state);
        let mut server = Box::pin(
            axum::serve(listener, router)
                .with_graceful_shutdown(async move {
                    graceful_state.wait_for_shutdown().await;
                })
                .into_future(),
        );
        let exit = tokio::select! {
            result = server.as_mut() => GatewayServerExit::Completed(result),
            grace = wait_for_shutdown_request(&mut shutdown) => {
                state.request_shutdown();
                match tokio::time::timeout(grace, server.as_mut()).await {
                    Ok(result) => GatewayServerExit::Completed(result),
                    Err(_) => GatewayServerExit::Forced,
                }
            }
        };
        drop(server);
        state.request_shutdown();
        let pruner_result = pruner.await.map_err(|error| {
            GatewayError::Transport(format!("gateway heartbeat pruner failed: {error}"))
        });
        let coordinator_result = match coordinator {
            Some(coordinator) => coordinator.await.map_err(|error| {
                GatewayError::Transport(format!("gateway HA coordinator failed: {error}"))
            }),
            None => Ok(()),
        };
        let result = match exit {
            GatewayServerExit::Completed(result) => result.map_err(transport_error),
            GatewayServerExit::Forced => Err(GatewayError::Transport(
                "gateway graceful shutdown timed out; forced cancellation completed".to_string(),
            )),
        };
        result.and(pruner_result).and(coordinator_result)
    })
}

enum GatewayServerExit {
    Completed(std::io::Result<()>),
    Forced,
}

async fn wait_for_shutdown_request(shutdown: &mut watch::Receiver<Option<Duration>>) -> Duration {
    loop {
        if let Some(grace) = *shutdown.borrow() {
            return grace;
        }
        if shutdown.changed().await.is_err() {
            return Duration::ZERO;
        }
    }
}

fn graceful_shutdown_budget(timeout: Duration) -> Duration {
    timeout.saturating_sub(timeout.min(MAX_SHUTDOWN_JOIN_RESERVE))
}

fn refresh_runtime(inner: &mut RuntimeInner) {
    let finished = inner
        .running
        .as_ref()
        .is_some_and(|running| running.handle.is_finished());
    if !finished {
        return;
    }
    let Some(running) = inner.running.take() else {
        return;
    };
    let requested = running.shutdown.borrow().is_some();
    let result = join_runtime(running);
    inner.state = if requested && result.is_ok() {
        GatewayRuntimeState::Stopped
    } else {
        GatewayRuntimeState::Failed
    };
    inner.last_error = result.err().map(|error| error.to_string());
}

fn join_runtime(running: RunningGateway) -> GatewayResult<()> {
    running
        .handle
        .join()
        .map_err(|_| GatewayError::Transport("gateway runtime thread panicked".to_string()))?
}

fn snapshot_from_parts(
    config: &GatewayConfig,
    inner: &RuntimeInner,
    metrics: Option<&GatewayMetrics>,
) -> GatewayRuntimeSnapshot {
    GatewayRuntimeSnapshot {
        state: inner.state,
        configured_bind_address: config.bind_address,
        bound_address: inner.bound_address,
        active_workers: metrics.map_or(0, GatewayMetrics::active_workers),
        active_clients: metrics.map_or(0, GatewayMetrics::active_clients),
        messages_routed: metrics.map_or(0, GatewayMetrics::messages_routed),
        routing_failures: metrics.map_or(0, GatewayMetrics::routing_failures),
        telemetry: metrics.map_or_else(
            GatewayTelemetrySnapshot::default,
            GatewayMetrics::telemetry_snapshot,
        ),
        ha: inner
            .gateway_state
            .as_ref()
            .and_then(|state| state.ha_coordinator())
            .map(|coordinator| coordinator.snapshot()),
        last_error: inner.last_error.clone(),
    }
}

fn transport_error(error: impl std::fmt::Display) -> GatewayError {
    GatewayError::Transport(error.to_string())
}

#[cfg(test)]
#[path = "runtime_tests.rs"]
mod tests;