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
use super::{
ClientId, ConnectionId, DeliveryEvidence, Message, MessageLane, OperationId, OperationPhase,
Request, SessionId, TaskSuccess,
};
use crate::{error::ErrorKind, NetError, Result};
use std::sync::Arc;
/// Terminal event emitted for an admitted message or request operation.
#[derive(Clone, Debug)]
pub struct TaskEvent {
/// Client that owns the operation.
pub client_id: ClientId,
/// Logical session that owns the operation.
pub session_id: SessionId,
/// Identifier of this send or request operation.
pub operation_id: OperationId,
/// Physical connection associated with the operation, if one was bound.
pub connection_id: Option<ConnectionId>,
/// Original message or request shared with observers.
pub source: TaskSource,
/// Send lane used by the operation.
pub lane: MessageLane,
/// Processing phase immediately before the terminal result became effective.
pub phase: OperationPhase,
/// Strongest delivery evidence known when the operation ended.
pub delivery: DeliveryEvidence,
/// Cause that ended the operation.
pub cause: TaskEndCause,
/// The single effective success or error result; success does not imply business acceptance by the peer.
pub result: Result<TaskSuccess>,
}
/// Original task input shared by cloned events without copying the payload.
#[derive(Clone)]
pub enum TaskSource {
/// Metadata for a logical request whose wire payload is generated per attempt.
/// The factory and idempotency key are deliberately excluded.
RequestOperation(Arc<crate::Metadata>),
/// A message send that has no response tracking.
Message(
/// Original message shared by the task and observers.
Arc<Message>,
),
/// A request whose business response is tracked.
Request(
/// Original request and metadata shared by the task and observers.
Arc<Request>,
),
}
impl std::fmt::Debug for TaskSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Message(message) => f.debug_tuple("Message").field(message).finish(),
Self::Request(request) => f.debug_tuple("Request").field(request).finish(),
Self::RequestOperation(metadata) => f
.debug_struct("RequestOperation")
.field("metadata_entries", &metadata.len())
.finish(),
}
}
}
impl TaskSource {
/// Counts the owned variable-size input once for the shared payload budget.
pub(crate) fn payload_bytes(&self) -> Result<usize> {
match self {
Self::RequestOperation(metadata) => {
metadata.iter().try_fold(0usize, |bytes, (key, value)| {
bytes
.checked_add(key.len())
.and_then(|size| size.checked_add(value.len()))
.ok_or_else(|| NetError::from(ErrorKind::ItemTooLarge))
})
}
Self::Message(message) => Ok(message.len()),
Self::Request(request) => {
let mut bytes = request
.message()
.len()
.checked_add(request.id().as_str().len())
.ok_or_else(|| NetError::from(ErrorKind::ItemTooLarge))?;
for (key, value) in request.metadata() {
bytes = bytes
.checked_add(key.len())
.and_then(|size| size.checked_add(value.len()))
.ok_or_else(|| NetError::from(ErrorKind::ItemTooLarge))?;
}
Ok(bytes)
}
}
}
}
/// Reason why a message or request reached its terminal state.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TaskEndCause {
/// Local write completed, or a matching final response was received.
Completed,
/// Explicit cancellation, cancellation-group cancellation, or owner drop.
Cancelled,
/// The operation deadline or processing timeout elapsed.
Expired,
/// The connection ended before the operation could continue.
Disconnected,
/// Client or engine shutdown terminated the operation.
Shutdown,
/// Another processing error caused failure.
Failed,
}
/// Owning subscription receiver for terminal task events.
pub type TaskEvents = crate::subscription::EventReceiver<TaskEvent>;