appcore-gateway 1.0.0

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
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
// =============================================================================
//        #######
//     ###       ###     F: router.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/07/26 08:53:09 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/02 12:48:56 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

//! Multiplexing router for Peer RPC envelopes.

use crate::error::{GatewayError, GatewayResult};
use crate::mesh::{MeshPeerRequest, MeshPeerResponse};
use crate::state::GatewayState;
use appcore_distributed_contracts::{PeerRpcEnvelope, PeerRpcResponse};
use appcore_peer_rpc::payload_hash;
use appcore_types::{ProtocolVersion, TenantId};
use axum::extract::ws::Message;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::oneshot;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PendingKey {
    state_id: usize,
    tenant_id: String,
    request_id: String,
    mesh: bool,
}

#[derive(Debug, Clone, Copy)]
struct PendingMetadata {
    worker_generation: u64,
    response_limit: Option<usize>,
}

// appcore-norm: allow(global-state) reason: gateway callbacks require process-wide pending request correlation
static PENDING_METADATA: LazyLock<Mutex<HashMap<PendingKey, PendingMetadata>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

/// Handles routing of envelopes between clients and workers.
pub struct EnvelopeRouter;

impl EnvelopeRouter {
    /// Routes an inbound client request envelope to a worker.
    ///
    /// Resolves the target worker, registers a pending request channel,
    /// forwards the envelope, and awaits the response from the worker.
    pub async fn route_request(
        state: Arc<GatewayState>,
        envelope: PeerRpcEnvelope,
        timeout: Duration,
    ) -> PeerRpcResponse {
        let request_id = envelope.request_id.clone();
        let tenant_id = envelope.tenant_id.clone();
        let capability = envelope.capability.clone();
        if let Some(error) = validate_routing_envelope(&envelope) {
            state.metrics.routing_failure();
            return PeerRpcResponse::rejected(request_id, error);
        }
        let timeout = timeout.min(crate::config::MAX_GATEWAY_REQUEST_TIMEOUT);

        // 1. Resolve target worker and register pending channel
        let (rx, worker_conn) = {
            let mut tenants = state.tenants.write();
            let tenant_state = tenants
                .entry(tenant_id.clone())
                .or_insert_with(|| crate::tenant::TenantState::new(tenant_id.clone()));

            let Some(worker_conn) = tenant_state
                .get_worker_by_core(&envelope.target_core_id)
                .filter(|worker| {
                    worker.cluster_id() == Some(&envelope.cluster_id)
                        && tenant_state
                            .registry
                            .resolve(&capability)
                            .is_some_and(|workers| workers.contains(&worker.key))
                })
                .cloned()
            else {
                state.metrics.routing_failure();
                return PeerRpcResponse::rejected(
                    request_id,
                    format!("compatible_worker_unavailable: {}", capability.as_str()),
                );
            };
            if !tenant_state.can_register_pending(&request_id, false) {
                state.metrics.routing_failure();
                return PeerRpcResponse::rejected(request_id, "pending_request_rejected");
            }

            let (tx, rx) = oneshot::channel();
            tenant_state.pending_requests.insert(request_id.clone(), tx);
            register_pending_metadata(
                &state,
                &tenant_id,
                &request_id,
                false,
                worker_conn.generation(),
                None,
            );
            (rx, worker_conn)
        };
        let _cleanup = PendingCleanup::new(
            Arc::clone(&state),
            tenant_id.clone(),
            request_id.clone(),
            false,
        );

        // 2. Forward the envelope to the worker
        let payload = match serde_json::to_string(&envelope) {
            Ok(json) => json,
            Err(err) => {
                state.metrics.routing_failure();
                cleanup_pending_request(&state, &tenant_id, &request_id);
                return PeerRpcResponse::rejected(
                    request_id,
                    format!("serialization_failed: {err}"),
                );
            }
        };

        if let Err(err) = worker_conn.send(Message::Text(payload.into())) {
            state.metrics.routing_failure();
            cleanup_pending_request(&state, &tenant_id, &request_id);
            return PeerRpcResponse::rejected(request_id, format!("forward_failed: {err:?}"));
        }

        // 3. Await worker response or timeout
        tokio::select! {
            biased;
            _ = state.wait_for_shutdown() => {
                state.metrics.routing_failure();
                PeerRpcResponse::rejected(request_id, "gateway_shutting_down")
            }
            result = tokio::time::timeout(timeout, rx) => match result {
                Ok(Ok(response)) => {
                    state.metrics.message_routed();
                    response
                }
                Ok(Err(_)) => {
                    state.metrics.routing_failure();
                    PeerRpcResponse::rejected(request_id, "worker_connection_lost")
                }
                Err(_) => {
                    state.metrics.routing_failure();
                    PeerRpcResponse::rejected(request_id, "worker_response_timeout")
                }
            }
        }
    }

    /// Dispatches a response received from a worker to the waiting client's task.
    pub fn handle_worker_response(
        state: Arc<GatewayState>,
        tenant_id: &TenantId,
        response: PeerRpcResponse,
    ) -> GatewayResult<()> {
        dispatch_worker_response(state, tenant_id, None, response)
    }

    /// Dispatches a response only when it came from the selected worker connection.
    pub fn handle_worker_response_from(
        state: Arc<GatewayState>,
        tenant_id: &TenantId,
        worker: &crate::WorkerConnection,
        response: PeerRpcResponse,
    ) -> GatewayResult<()> {
        dispatch_worker_response(state, tenant_id, Some(worker.generation()), response)
    }

    /// Routes a mesh relay request to the target worker socket.
    pub async fn route_mesh_request(
        state: Arc<GatewayState>,
        request: MeshPeerRequest,
        timeout: Duration,
    ) -> MeshPeerResponse {
        if let Err(error) = request.validate_schema() {
            state.metrics.routing_failure();
            return MeshPeerResponse::rejected(request.request_id, error.to_string());
        }
        let request_id = request.request_id.clone();
        let tenant_id = request.target_tenant_id.clone();
        let timeout = timeout.min(crate::config::MAX_GATEWAY_REQUEST_TIMEOUT);
        let peer_envelope = request.peer_envelope().ok();

        let (rx, worker_conn) = {
            let mut tenants = state.tenants.write();
            let Some(tenant_state) = tenants.get_mut(&tenant_id) else {
                state.metrics.routing_failure();
                return MeshPeerResponse::rejected(request_id, "tenant_unavailable");
            };
            let Some(worker_conn) = tenant_state
                .get_worker_by_core(&request.target_core_id)
                .filter(|worker| {
                    peer_envelope.as_ref().is_none_or(|envelope| {
                        worker.cluster_id() == Some(&envelope.cluster_id)
                            && tenant_state
                                .registry
                                .resolve(&envelope.capability)
                                .is_some_and(|workers| workers.contains(&worker.key))
                    })
                })
                .cloned()
            else {
                state.metrics.routing_failure();
                return MeshPeerResponse::rejected(request_id, "worker_offline");
            };
            if !tenant_state.can_register_pending(&request_id, true) {
                state.metrics.routing_failure();
                return MeshPeerResponse::rejected(request_id, "pending_request_rejected");
            }
            let (tx, rx) = oneshot::channel();
            tenant_state
                .pending_mesh_requests
                .insert(request_id.clone(), tx);
            register_pending_metadata(
                &state,
                &tenant_id,
                &request_id,
                true,
                worker_conn.generation(),
                Some(request.max_response_bytes),
            );
            (rx, worker_conn)
        };
        let _cleanup = PendingCleanup::new(
            Arc::clone(&state),
            tenant_id.clone(),
            request_id.clone(),
            true,
        );

        let payload = match serde_json::to_string(&request) {
            Ok(json) => json,
            Err(error) => {
                state.metrics.routing_failure();
                cleanup_pending_mesh_request(&state, &tenant_id, &request_id);
                return MeshPeerResponse::rejected(
                    request_id,
                    format!("serialization_failed: {error}"),
                );
            }
        };
        if let Err(error) = worker_conn.send(Message::Text(payload.into())) {
            state.metrics.routing_failure();
            cleanup_pending_mesh_request(&state, &tenant_id, &request_id);
            return MeshPeerResponse::rejected(request_id, format!("forward_failed: {error:?}"));
        }

        tokio::select! {
            biased;
            _ = state.wait_for_shutdown() => {
                state.metrics.routing_failure();
                MeshPeerResponse::rejected(request_id, "gateway_shutting_down")
            }
            result = tokio::time::timeout(timeout, rx) => match result {
                Ok(Ok(response)) => {
                    state.metrics.message_routed();
                    response
                }
                Ok(Err(_)) => {
                    state.metrics.routing_failure();
                    MeshPeerResponse::rejected(request_id, "worker_connection_lost")
                }
                Err(_) => {
                    state.metrics.routing_failure();
                    MeshPeerResponse::rejected(request_id, "worker_response_timeout")
                }
            }
        }
    }

    /// Dispatches a mesh response received from a worker to the waiting relay task.
    pub fn handle_worker_mesh_response(
        state: Arc<GatewayState>,
        tenant_id: &TenantId,
        response: MeshPeerResponse,
    ) -> GatewayResult<()> {
        dispatch_worker_mesh_response(state, tenant_id, None, response)
    }

    /// Dispatches a mesh response only when it came from the selected worker connection.
    pub fn handle_worker_mesh_response_from(
        state: Arc<GatewayState>,
        tenant_id: &TenantId,
        worker: &crate::WorkerConnection,
        response: MeshPeerResponse,
    ) -> GatewayResult<()> {
        dispatch_worker_mesh_response(state, tenant_id, Some(worker.generation()), response)
    }
}

struct PendingCleanup {
    state: Arc<GatewayState>,
    tenant_id: TenantId,
    request_id: String,
    mesh: bool,
}

impl PendingCleanup {
    fn new(state: Arc<GatewayState>, tenant_id: TenantId, request_id: String, mesh: bool) -> Self {
        Self {
            state,
            tenant_id,
            request_id,
            mesh,
        }
    }
}

impl Drop for PendingCleanup {
    fn drop(&mut self) {
        if self.mesh {
            cleanup_pending_mesh_request(&self.state, &self.tenant_id, &self.request_id);
        } else {
            cleanup_pending_request(&self.state, &self.tenant_id, &self.request_id);
        }
    }
}

fn cleanup_pending_request(state: &GatewayState, tenant_id: &TenantId, request_id: &str) {
    let mut tenants = state.tenants.write();
    if let Some(tenant_state) = tenants.get_mut(tenant_id) {
        tenant_state.pending_requests.remove(request_id);
    }
    remove_pending_metadata(state, tenant_id, request_id, false);
}

fn cleanup_pending_mesh_request(state: &GatewayState, tenant_id: &TenantId, request_id: &str) {
    let mut tenants = state.tenants.write();
    if let Some(tenant_state) = tenants.get_mut(tenant_id) {
        tenant_state.pending_mesh_requests.remove(request_id);
    }
    remove_pending_metadata(state, tenant_id, request_id, true);
}

fn dispatch_worker_response(
    state: Arc<GatewayState>,
    tenant_id: &TenantId,
    generation: Option<u64>,
    response: PeerRpcResponse,
) -> GatewayResult<()> {
    let request_id = response.request_id.clone();
    let mut tenants = state.tenants.write();
    if let Some(tenant_state) = tenants.get_mut(tenant_id) {
        let expected = pending_metadata(&state, tenant_id, &request_id, false);
        if expected.is_some_and(|metadata| {
            generation.is_none_or(|value| value == metadata.worker_generation)
        }) {
            remove_pending_metadata(&state, tenant_id, &request_id, false);
            if let Some(tx) = tenant_state.pending_requests.remove(&request_id) {
                let _ = tx.send(response);
                return Ok(());
            }
        }
    }
    Err(orphaned_response(tenant_id, &request_id, false))
}

fn dispatch_worker_mesh_response(
    state: Arc<GatewayState>,
    tenant_id: &TenantId,
    generation: Option<u64>,
    response: MeshPeerResponse,
) -> GatewayResult<()> {
    let request_id = response.request_id.clone();
    let mut tenants = state.tenants.write();
    if let Some(tenant_state) = tenants.get_mut(tenant_id) {
        let expected = pending_metadata(&state, tenant_id, &request_id, true);
        if expected.is_some_and(|metadata| {
            generation.is_none_or(|value| value == metadata.worker_generation)
                && metadata
                    .response_limit
                    .is_some_and(|limit| response.validate_for_request(&request_id, limit).is_ok())
        }) {
            remove_pending_metadata(&state, tenant_id, &request_id, true);
            if let Some(tx) = tenant_state.pending_mesh_requests.remove(&request_id) {
                let _ = tx.send(response);
                return Ok(());
            }
        }
    }
    Err(orphaned_response(tenant_id, &request_id, true))
}

fn register_pending_metadata(
    state: &GatewayState,
    tenant_id: &TenantId,
    request_id: &str,
    mesh: bool,
    worker_generation: u64,
    response_limit: Option<usize>,
) {
    pending_metadata_map().insert(
        pending_key(state, tenant_id, request_id, mesh),
        PendingMetadata {
            worker_generation,
            response_limit,
        },
    );
}

fn pending_metadata(
    state: &GatewayState,
    tenant_id: &TenantId,
    request_id: &str,
    mesh: bool,
) -> Option<PendingMetadata> {
    pending_metadata_map()
        .get(&pending_key(state, tenant_id, request_id, mesh))
        .copied()
}

fn remove_pending_metadata(
    state: &GatewayState,
    tenant_id: &TenantId,
    request_id: &str,
    mesh: bool,
) {
    pending_metadata_map().remove(&pending_key(state, tenant_id, request_id, mesh));
}

fn pending_metadata_map() -> std::sync::MutexGuard<'static, HashMap<PendingKey, PendingMetadata>> {
    PENDING_METADATA
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

fn pending_key(
    state: &GatewayState,
    tenant_id: &TenantId,
    request_id: &str,
    mesh: bool,
) -> PendingKey {
    PendingKey {
        state_id: std::ptr::from_ref(state) as usize,
        tenant_id: tenant_id.as_str().to_string(),
        request_id: request_id.to_string(),
        mesh,
    }
}

fn orphaned_response(tenant_id: &TenantId, request_id: &str, mesh: bool) -> GatewayError {
    GatewayError::Protocol(format!(
        "orphaned {}response or timeout for tenant {} request: {}",
        if mesh { "worker mesh " } else { "worker " },
        tenant_id.as_str(),
        request_id
    ))
}

fn validate_routing_envelope(envelope: &PeerRpcEnvelope) -> Option<&'static str> {
    if envelope.protocol_version != ProtocolVersion::default() {
        return Some("protocol_version_unsupported");
    }
    if envelope.expires_at_ms <= envelope.timestamp_ms {
        return Some("envelope_expiry_invalid");
    }
    if envelope.expires_at_ms <= now_ms() {
        return Some("envelope_expired");
    }
    if envelope.body_hash != payload_hash(&envelope.payload) {
        return Some("body_hash_invalid");
    }
    if envelope.payload.len() > crate::config::MAX_GATEWAY_MESSAGE_BYTES {
        return Some("payload_too_large");
    }
    None
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_millis() as u64)
        .unwrap_or(0)
}