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
// Copyright 2022 Jeff Kim <hiking90@gmail.com>
// SPDX-License-Identifier: Apache-2.0
//! Dead Letter Tracking Module
//!
//! This module provides infrastructure for tracking and recording dead letters—
//! messages that could not be delivered to their intended recipients.
//!
//! # Background
//!
//! Dead letters occur when a message cannot be delivered to an actor — or,
//! for `ask`-family operations, when its **reply** is not observed — such as:
//! - The actor's mailbox channel has closed (actor stopped)
//! - A send operation times out before admission, or an ask round-trip times
//! out before the reply arrives (the message itself may have been delivered
//! and processed; see [`DeadLetterReason::Timeout`])
//! - The reply channel was dropped before responding
//! - The message was accepted but the actor terminated before processing it
//! (see [`DeadLetterReason::DiscardedAtShutdown`])
//!
//! # Observability
//!
//! Dead letters are always logged with structured fields via `tracing`:
//!
//! ```text
//! WARN dead_letter: Dead letter: message could not be delivered
//! actor.id=42
//! actor.type_name="MyActor"
//! message.type_name="PingMessage"
//! dead_letter.reason="actor stopped"
//! dead_letter.operation="tell"
//! ```
//!
//! # Performance Characteristics
//!
//! Dead letter recording is designed for minimal overhead:
//!
//! | Scenario | Overhead |
//! |----------|----------|
//! | Successful message delivery (hot path) | **Zero** - no code executes |
//! | Dead letter, no tracing subscriber | ~5-50 ns (fast check + early return) |
//! | Dead letter, subscriber active | ~1-10 μs (logging + serialization) |
//!
//! Key optimizations:
//! - `#[cold]` attribute hints compiler to optimize hot path
//! - `Ordering::Relaxed` for atomic counter (no memory barriers)
//! - Static string references for operation names (no allocation)
//! - `std::any::type_name::<M>()` is compile-time computed (zero runtime cost)
//!
//! # Testing Support
//!
//! When the `test-utils` feature is enabled (or in unit tests), a counter tracks
//! the number of dead letters for verification purposes. Use `dead_letter_count()`
//! and `reset_dead_letter_count()` to inspect and reset this counter.
//!
//! ```toml
//! [dev-dependencies]
//! rsactor = { version = "...", features = ["test-utils"] }
//! ```
//!
//! # Example: Observing Dead Letters
//!
//! ```rust,ignore
//! use rsactor::{spawn, Actor, ActorRef};
//!
//! #[tokio::main]
//! async fn main() {
//! // Initialize tracing to see dead letter logs
//! tracing_subscriber::fmt()
//! .with_env_filter("rsactor=debug")
//! .init();
//!
//! // Dead letters are automatically logged when message delivery fails
//! let (actor_ref, handle) = spawn::<MyActor>(MyActor);
//! actor_ref.stop().await;
//! handle.await.unwrap();
//!
//! // This will log a dead letter warning
//! let _ = actor_ref.tell(MyMessage).await;
//! }
//! ```
//!
//! # Security Warning
//!
//! **Never enable `test-utils` in production builds!** This feature exposes
//! internal metrics that could be used to:
//! - Monitor message delivery failure rates
//! - Reset monitoring metrics to evade detection
//!
//! For production observability, rely on the structured `tracing::warn!` logs instead.
use crateIdentity;
use ;
static DEAD_LETTER_COUNT: AtomicU64 = new;
/// Reason why a message became a dead letter.
///
/// This enum is marked `#[non_exhaustive]` to allow adding new variants
/// in future versions without breaking existing code.
/// Records a dead letter event with structured logging.
///
/// This function is called automatically by `ActorRef` methods when a message
/// cannot be delivered. It serves two purposes:
///
/// 1. **Observability**: Logs a warning-level event with structured fields
/// for debugging and monitoring (always available via `tracing`).
///
/// 2. **Testing**: When `test-utils` feature is enabled, increments an atomic counter
/// that can be queried via [`dead_letter_count()`] to verify dead letter behavior.
///
/// # Arguments
///
/// * `identity` - The identity of the actor that failed to receive the message
/// * `reason` - Why the message became a dead letter
/// * `operation` - The logical operation that failed ("tell", "ask",
/// "tell_priority", "ask_priority"). Blocking variants record the same
/// label as their async counterparts so the same call site aggregates to
/// one operation regardless of which execution path it took.
///
/// # Type Parameters
///
/// * `M` - The message type (used for logging the type name)
///
/// # Why `#[cold]`?
///
/// Dead letters are exceptional paths - they occur when something goes wrong.
/// The `#[cold]` attribute hints to the compiler that this function is rarely
/// called, allowing better optimization of the hot path (successful message delivery).
pub
/// Type-erased variant of [`record`] for call sites that no longer have the
/// concrete message type — e.g. the shutdown drain, which only holds a
/// `Box<dyn PayloadHandler>` and obtains the type name through
/// `PayloadHandler::message_type_name`.
pub
/// Returns the total number of dead letters recorded.
///
/// This function is only available when the `test-utils` feature is enabled.
/// Atomically resets the dead letter counter and returns the value it held.
///
/// The read-and-reset is a single atomic `swap`, so no increment can be lost
/// *between* reading and resetting. Dead letters recorded concurrently with
/// the call still race with it (they land in either the returned value or the
/// fresh count) — serialize generators against this call when exact
/// accounting matters.
///
/// This function is only available when the `test-utils` feature is enabled.