appcore-api 1.0.3-rc

HTTP API host and transport contracts for 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
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
// =============================================================================
//        #######
//     ###       ###     F: reload.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/30 05:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/30 05:00:00 by dnettoRaw
//      ###########      S: 1.0.2-rc
// =============================================================================

//! Atomic HTTP routing-generation reload with bounded drain and rollback.

use super::reload_generation::{HttpRoutingGenerationsSnapshot, RoutingGeneration, RoutingTable};
use super::RuntimeHttpHost;
use axum::body::Body;
use axum::extract::{Request, State};
use axum::http::{Response, StatusCode};
use axum::routing::any;
use axum::Router;
use std::fmt;
use std::io;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tower::ServiceExt;

const ROUTING_RETRY_DELAY: Duration = Duration::from_millis(1);
const MAX_RELOAD_TIMEOUT: Duration = Duration::from_secs(60);

/// Reload phase associated with a controlled failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpReloadPhase {
    /// Candidate validation and health before activation.
    Prepare,
    /// Atomic routing activation and health confirmation.
    Switch,
    /// Bounded completion of requests admitted by the old generation.
    Drain,
    /// Restoration and drain after a failed activation.
    Rollback,
}

/// Bounded health and drain policy for one routing reload.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HttpReloadPolicy {
    health_timeout: Duration,
    drain_timeout: Duration,
}

impl HttpReloadPolicy {
    /// Creates a policy with non-zero health and drain deadlines.
    pub fn new(
        health_timeout: Duration,
        drain_timeout: Duration,
    ) -> Result<Self, RuntimeHttpReloadError> {
        if health_timeout.is_zero()
            || drain_timeout.is_zero()
            || health_timeout > MAX_RELOAD_TIMEOUT
            || drain_timeout > MAX_RELOAD_TIMEOUT
        {
            return Err(RuntimeHttpReloadError::InvalidPolicy);
        }
        Ok(Self {
            health_timeout,
            drain_timeout,
        })
    }

    /// Returns the candidate health deadline.
    pub fn health_timeout(self) -> Duration {
        self.health_timeout
    }

    /// Returns the old-generation drain deadline.
    pub fn drain_timeout(self) -> Duration {
        self.drain_timeout
    }
}

/// Payload-free state and counters for a reloadable HTTP host.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HttpReloadSnapshot {
    /// Generation currently selected for new requests.
    pub active_generation: u64,
    /// Requests currently executing on the selected generation.
    pub active_inflight: usize,
    /// Whether one prepare/switch/drain transaction is active.
    pub reload_in_progress: bool,
    /// Reloads that switched and drained successfully.
    pub successful_reloads: u64,
    /// Failed reload attempts, including controlled rollbacks.
    pub failed_reloads: u64,
    /// Switches restored to the prior generation.
    pub rollbacks: u64,
}

/// Controlled, redacted reload failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeHttpReloadError {
    /// Reload requires an enabled listener.
    ListenerDisabled,
    /// A candidate attempted to change the listener address in-place.
    ListenerAddressChanged,
    /// Generation identifiers must increase monotonically.
    StaleGeneration,
    /// Another reload transaction already owns the coordinator.
    ReloadInProgress,
    /// A failed or cancelled generation still owns in-flight requests.
    RetiringGenerationBusy,
    /// Health or drain deadlines must be non-zero.
    InvalidPolicy,
    /// The candidate or active generation failed its health gate.
    HealthGateFailed(HttpReloadPhase),
    /// The outgoing generation did not drain before rollback.
    DrainTimedOut,
    /// The failed generation remained active past the rollback drain deadline.
    RollbackDrainTimedOut,
}

impl fmt::Display for RuntimeHttpReloadError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ListenerDisabled => formatter.write_str("HTTP reload listener is disabled"),
            Self::ListenerAddressChanged => {
                formatter.write_str("HTTP reload requires a prepared listener generation")
            }
            Self::StaleGeneration => formatter.write_str("HTTP routing generation must increase"),
            Self::ReloadInProgress => formatter.write_str("HTTP reload is already in progress"),
            Self::RetiringGenerationBusy => {
                formatter.write_str("HTTP routing generation is still draining")
            }
            Self::InvalidPolicy => formatter.write_str("HTTP reload policy is invalid"),
            Self::HealthGateFailed(phase) => {
                write!(formatter, "HTTP reload health gate failed during {phase:?}")
            }
            Self::DrainTimedOut => formatter.write_str("HTTP routing generation drain timed out"),
            Self::RollbackDrainTimedOut => {
                formatter.write_str("HTTP rollback generation drain timed out")
            }
        }
    }
}

impl std::error::Error for RuntimeHttpReloadError {}

/// Prepared, health-gated candidate that can be consumed by one reload.
pub struct PreparedRuntimeHttpGeneration {
    generation: Arc<RoutingGeneration>,
}

impl fmt::Debug for PreparedRuntimeHttpGeneration {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PreparedRuntimeHttpGeneration")
            .field("generation", &self.generation.id())
            .finish_non_exhaustive()
    }
}

/// HTTP host whose stable listener dispatches through one atomic generation.
pub struct ReloadableRuntimeHttpHost {
    config: super::HttpApiConfig,
    routing: Arc<RoutingTable>,
    reload_in_progress: AtomicBool,
    successful_reloads: AtomicU64,
    failed_reloads: AtomicU64,
    rollbacks: AtomicU64,
}

impl ReloadableRuntimeHttpHost {
    /// Creates a reloadable host from an already composed initial host.
    pub fn new(
        initial_generation: u64,
        host: RuntimeHttpHost,
    ) -> Result<Self, RuntimeHttpReloadError> {
        if !host.config().enabled {
            return Err(RuntimeHttpReloadError::ListenerDisabled);
        }
        if initial_generation == 0 {
            return Err(RuntimeHttpReloadError::StaleGeneration);
        }
        let config = host.config().clone();
        Ok(Self {
            config,
            routing: Arc::new(RoutingTable::new(initial_generation, host.router())),
            reload_in_progress: AtomicBool::new(false),
            successful_reloads: AtomicU64::new(0),
            failed_reloads: AtomicU64::new(0),
            rollbacks: AtomicU64::new(0),
        })
    }

    /// Validates and owns a candidate without changing live routing.
    pub fn prepare(
        &self,
        generation: u64,
        host: RuntimeHttpHost,
    ) -> Result<PreparedRuntimeHttpGeneration, RuntimeHttpReloadError> {
        let active = self.routing.active();
        if generation <= active.id() {
            return Err(RuntimeHttpReloadError::StaleGeneration);
        }
        if !host.config().enabled {
            return Err(RuntimeHttpReloadError::ListenerDisabled);
        }
        if host.config().host != self.config.host || host.config().port != self.config.port {
            return Err(RuntimeHttpReloadError::ListenerAddressChanged);
        }
        Ok(PreparedRuntimeHttpGeneration {
            generation: self.routing.generation(generation, host.router()),
        })
    }

    #[cfg(test)]
    pub(super) fn new_for_test(
        initial_generation: u64,
        config: super::HttpApiConfig,
        router: Router,
    ) -> Self {
        Self {
            config,
            routing: Arc::new(RoutingTable::new(initial_generation, router)),
            reload_in_progress: AtomicBool::new(false),
            successful_reloads: AtomicU64::new(0),
            failed_reloads: AtomicU64::new(0),
            rollbacks: AtomicU64::new(0),
        }
    }

    #[cfg(test)]
    pub(super) fn prepare_router_for_test(
        &self,
        generation: u64,
        router: Router,
    ) -> PreparedRuntimeHttpGeneration {
        PreparedRuntimeHttpGeneration {
            generation: self.routing.generation(generation, router),
        }
    }

    /// Returns a router that always dispatches through the active generation.
    pub fn router(&self) -> Router {
        dynamic_router(Arc::clone(&self.routing))
    }

    /// Runs the stable listener until cooperative shutdown is requested.
    pub fn run_until_shutdown(&self, shutdown: Arc<AtomicBool>) -> io::Result<()> {
        let address = format!("{}:{}", self.config.host, self.config.port);
        let runtime = super::build_http_runtime()?;
        runtime.block_on(async move {
            let listener = tokio::net::TcpListener::bind(address).await?;
            serve_listener(listener, self.router(), shutdown).await
        })
    }

    /// Runs on a listener that the composition root already bound and checked.
    pub fn run_on_listener_until_shutdown(
        &self,
        listener: std::net::TcpListener,
        shutdown: Arc<AtomicBool>,
    ) -> io::Result<()> {
        listener.set_nonblocking(true)?;
        let router = self.router();
        let runtime = super::build_http_runtime()?;
        runtime.block_on(async move {
            let listener = tokio::net::TcpListener::from_std(listener)?;
            serve_listener(listener, router, shutdown).await
        })
    }

    /// Health-checks, atomically activates, and drains one prepared generation.
    pub async fn reload(
        &self,
        prepared: PreparedRuntimeHttpGeneration,
        policy: HttpReloadPolicy,
    ) -> Result<(), RuntimeHttpReloadError> {
        let mut guard = match ReloadGuard::acquire(
            &self.reload_in_progress,
            &self.routing,
            &self.failed_reloads,
            &self.rollbacks,
        ) {
            Ok(guard) => guard,
            Err(error) => return self.fail(error),
        };
        self.routing.release_drained_retiring();
        if self.routing.generations_snapshot().retiring.is_some() {
            return self.fail(RuntimeHttpReloadError::RetiringGenerationBusy);
        }
        let previous = self.routing.active();
        if prepared.generation.id() <= previous.id() {
            return self.fail(RuntimeHttpReloadError::StaleGeneration);
        }
        if !probe_health(prepared.generation.router(), policy.health_timeout).await {
            return self.fail(RuntimeHttpReloadError::HealthGateFailed(
                HttpReloadPhase::Prepare,
            ));
        }

        previous.stop_accepting();
        if !self.routing.retire(Arc::clone(&previous)) {
            previous.start_accepting();
            return self.fail(RuntimeHttpReloadError::RetiringGenerationBusy);
        }
        guard.arm(Arc::clone(&previous), Arc::clone(&prepared.generation));
        self.routing.activate(Arc::clone(&prepared.generation));
        if !probe_health(prepared.generation.router(), policy.health_timeout).await {
            guard.disarm();
            return self
                .rollback(previous, prepared.generation, policy)
                .await
                .and(Err(RuntimeHttpReloadError::HealthGateFailed(
                    HttpReloadPhase::Switch,
                )));
        }
        if !wait_for_drain(&previous, policy.drain_timeout).await {
            guard.disarm();
            return self
                .rollback(previous, prepared.generation, policy)
                .await
                .and(Err(RuntimeHttpReloadError::DrainTimedOut));
        }
        self.routing.release_retiring(previous.id());
        guard.disarm();
        increment(&self.successful_reloads);
        Ok(())
    }

    /// Returns the active generation and bounded operational counters.
    pub fn snapshot(&self) -> HttpReloadSnapshot {
        let active = self.routing.active();
        HttpReloadSnapshot {
            active_generation: active.id(),
            active_inflight: active.inflight(),
            reload_in_progress: self.reload_in_progress.load(Ordering::Acquire),
            successful_reloads: self.successful_reloads.load(Ordering::Relaxed),
            failed_reloads: self.failed_reloads.load(Ordering::Relaxed),
            rollbacks: self.rollbacks.load(Ordering::Relaxed),
        }
    }

    /// Returns the active and optional retiring generation without request data.
    pub fn generation_snapshot(&self) -> HttpRoutingGenerationsSnapshot {
        self.routing.generations_snapshot()
    }

    async fn rollback(
        &self,
        previous: Arc<RoutingGeneration>,
        failed: Arc<RoutingGeneration>,
        policy: HttpReloadPolicy,
    ) -> Result<(), RuntimeHttpReloadError> {
        failed.stop_accepting();
        previous.start_accepting();
        self.routing.activate(Arc::clone(&previous));
        self.routing.release_retiring(previous.id());
        if !self.routing.retire(Arc::clone(&failed)) {
            return Err(RuntimeHttpReloadError::RetiringGenerationBusy);
        }
        increment(&self.rollbacks);
        increment(&self.failed_reloads);
        if wait_for_drain(&failed, policy.drain_timeout).await {
            self.routing.release_retiring(failed.id());
            Ok(())
        } else {
            Err(RuntimeHttpReloadError::RollbackDrainTimedOut)
        }
    }

    fn fail<T>(&self, error: RuntimeHttpReloadError) -> Result<T, RuntimeHttpReloadError> {
        increment(&self.failed_reloads);
        Err(error)
    }
}

struct ReloadGuard<'a> {
    flag: &'a AtomicBool,
    routing: &'a RoutingTable,
    failed_reloads: &'a AtomicU64,
    rollbacks: &'a AtomicU64,
    rollback: Option<(Arc<RoutingGeneration>, Arc<RoutingGeneration>)>,
}

impl<'a> ReloadGuard<'a> {
    fn acquire(
        flag: &'a AtomicBool,
        routing: &'a RoutingTable,
        failed_reloads: &'a AtomicU64,
        rollbacks: &'a AtomicU64,
    ) -> Result<Self, RuntimeHttpReloadError> {
        flag.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .map_err(|_| RuntimeHttpReloadError::ReloadInProgress)?;
        Ok(Self {
            flag,
            routing,
            failed_reloads,
            rollbacks,
            rollback: None,
        })
    }

    fn arm(&mut self, previous: Arc<RoutingGeneration>, candidate: Arc<RoutingGeneration>) {
        self.rollback = Some((previous, candidate));
    }

    fn disarm(&mut self) {
        self.rollback = None;
    }
}

impl Drop for ReloadGuard<'_> {
    fn drop(&mut self) {
        if let Some((previous, failed)) = self.rollback.take() {
            failed.stop_accepting();
            previous.start_accepting();
            self.routing.activate(Arc::clone(&previous));
            self.routing.release_retiring(previous.id());
            if self.routing.retire(failed) {
                increment(self.rollbacks);
                increment(self.failed_reloads);
            }
        }
        self.routing.release_drained_retiring();
        self.flag.store(false, Ordering::Release);
    }
}

fn dynamic_router(routing: Arc<RoutingTable>) -> Router {
    Router::new()
        .fallback(any(dispatch_active_generation))
        .with_state(routing)
}

async fn dispatch_active_generation(
    State(routing): State<Arc<RoutingTable>>,
    request: Request,
) -> Response<Body> {
    loop {
        let generation = routing.active();
        if let Some(_permit) = generation.try_admit() {
            return generation
                .router()
                .clone()
                .oneshot(request)
                .await
                .unwrap_or_else(|never| match never {});
        }
        tokio::time::sleep(ROUTING_RETRY_DELAY).await;
    }
}

async fn probe_health(router: &Router, timeout: Duration) -> bool {
    let request = Request::get("/v1/health").body(Body::empty());
    let Ok(request) = request else {
        return false;
    };
    tokio::time::timeout(timeout, router.clone().oneshot(request))
        .await
        .ok()
        .and_then(Result::ok)
        .is_some_and(|response| response.status() == StatusCode::OK)
}

async fn wait_for_drain(generation: &RoutingGeneration, timeout: Duration) -> bool {
    let deadline = Instant::now() + timeout;
    loop {
        if generation.inflight() == 0 {
            return true;
        }
        if Instant::now() >= deadline {
            return false;
        }
        tokio::time::sleep(ROUTING_RETRY_DELAY).await;
    }
}

async fn serve_listener(
    listener: tokio::net::TcpListener,
    router: Router,
    shutdown: Arc<AtomicBool>,
) -> io::Result<()> {
    axum::serve(super::connection::with_read_timeout(listener), router)
        .with_graceful_shutdown(super::wait_for_shutdown(shutdown))
        .await
}

fn increment(counter: &AtomicU64) {
    let _ = counter.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
        Some(current.saturating_add(1))
    });
}