ctb_workspace_ipc/types.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Common types, identifiers, and payload definitions for IPC.
21
22#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use serde::{Deserialize, Serialize};
30use std::fmt;
31use std::time::Duration;
32use uuid::Uuid;
33
34/// Monotonic request identifier for RPC calls on a connection.
35pub type RequestId = u64;
36
37/// Identifier for a logical stream.
38pub type StreamId = u64;
39
40/// Opaque identifier for a process managed by the workspace.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub struct ProcessId(pub Uuid);
43
44impl Default for ProcessId {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl ProcessId {
51 /// Generate a new random `ProcessId`.
52 pub fn new() -> Self {
53 Self(Uuid::new_v4())
54 }
55
56 /// Parse a `ProcessId` from a UUID string.
57 pub fn parse_str(s: &str) -> Result<Self, uuid::Error> {
58 Ok(Self(Uuid::parse_str(s)?))
59 }
60}
61
62impl fmt::Display for ProcessId {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 self.0.fmt(f)
65 }
66}
67
68/// Identifier for a connection (control plane) bound to a process/capability set.
69#[derive(
70 Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default,
71)]
72pub struct ConnectionId(pub Uuid);
73
74impl ConnectionId {
75 /// Generate a new random `ConnectionId`.
76 pub fn new() -> Self {
77 Self(Uuid::new_v4())
78 }
79}
80
81impl fmt::Display for ConnectionId {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 self.0.fmt(f)
84 }
85}
86
87/// Create a span that identifies a workspace-managed process.
88///
89/// This is intended for attaching `process_id` to log lines emitted while
90/// handling process-scoped work.
91#[must_use]
92pub fn ipc_process_span(process_id: ProcessId) -> tracing::Span {
93 tracing::info_span!("ipc.process", process_id = %process_id)
94}
95
96/// Create a span that identifies an IPC connection.
97///
98/// This is intended for attaching `conn_id` to log lines emitted while
99/// servicing a connection.
100#[must_use]
101pub fn ipc_connection_span(connection_id: ConnectionId) -> tracing::Span {
102 tracing::info_span!("ipc.connection", conn_id = %connection_id)
103}
104
105/// Create a span that identifies a single RPC request.
106///
107/// This is intentionally independent of protocol types (only uses `RequestId`).
108#[must_use]
109pub fn ipc_request_span(request_id: RequestId) -> tracing::Span {
110 tracing::info_span!("ipc.request", request_id)
111}
112
113/// Create a span that identifies a logical stream.
114#[must_use]
115pub fn ipc_stream_span(stream_id: StreamId) -> tracing::Span {
116 tracing::info_span!("ipc.stream", stream_id)
117}
118
119/// Platform-neutral timeout settings.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct Timeouts {
122 pub handshake: Duration,
123 pub request: Duration,
124 pub graceful_shutdown: Duration,
125 pub heartbeat_interval: Duration,
126 pub heartbeat_miss_tolerance: u32,
127}