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
//! Recovery hook and configuration for `WireframeApp`.
use super::WireframeApp;
use crate::{
app::Packet,
codec::{FrameCodec, RecoveryConfig, RecoveryPolicyHook},
serializer::Serializer,
};
impl<S, C, E, F> WireframeApp<S, C, E, F>
where
S: Serializer + Send + Sync,
C: Send + 'static,
E: Packet,
F: FrameCodec,
{
/// Install a [`RecoveryPolicyHook`] consulted when a decoder fails.
///
/// Without a hook, a decoder failure on a live connection is terminal: the
/// connection stops reading and the original error reaches the teardown
/// path unchanged. Installing a hook lets the application choose between
/// [`Drop`](crate::codec::RecoveryPolicy::Drop),
/// [`Quarantine`](crate::codec::RecoveryPolicy::Quarantine), and
/// [`Disconnect`](crate::codec::RecoveryPolicy::Disconnect) per error.
///
/// The hook is shared by every connection built from this application and
/// is wrapped in an [`Arc`](std::sync::Arc), so all its methods take
/// `&self`. Per-connection state belongs in a connection setup hook, not in
/// the recovery hook.
///
/// # Callback order
///
/// For each decoder failure the connection loop performs these steps in
/// order:
///
/// 1. Call [`recovery_policy`](RecoveryPolicyHook::recovery_policy) to select a policy.
/// 2. Apply the fallbacks described below, which may replace the selection.
/// 3. Call [`quarantine_duration`](RecoveryPolicyHook::quarantine_duration) when the applied
/// policy is `Quarantine`, and pause for that duration.
/// 4. Call [`on_error`](RecoveryPolicyHook::on_error) with the applied policy, immediately
/// before the loop acts on it.
/// 5. Increment `wireframe_codec_errors_total` once, labelled with the error type and the
/// applied policy.
///
/// # Fallbacks
///
/// The connection loop overrides the hook's selection in three cases,
/// because continuing would either repeat forever or cannot help:
///
/// - A failure from `decode_eof` always disconnects: the peer has stopped sending, so no later
/// frame can arrive on this connection.
/// - `Drop` or `Quarantine` selected for a failure that consumed no bytes disconnects: the same
/// malformed prefix would be decoded again.
/// - A `Drop` beyond
/// [`RecoveryConfig::max_consecutive_drops`](crate::codec::RecoveryConfig::max_consecutive_drops)
/// disconnects. The counter restarts whenever a frame decodes successfully.
///
/// `on_error` and the metric report the applied policy, so they never claim
/// a drop or quarantine that was escalated to a disconnect.
///
/// # Decoder contract
///
/// Recovery only works if the decoder consumes the malformed frame and
/// leaves its framing state valid before returning the error. A decoder
/// that returns an error without consuming any bytes would be offered the
/// same input again, so the connection loop escalates that case to a
/// disconnect.
///
/// # Examples
///
/// ```
/// use wireframe::{
/// app::WireframeApp,
/// codec::{CodecError, CodecErrorContext, RecoveryPolicy, RecoveryPolicyHook},
/// };
///
/// struct DisconnectOnEveryError;
///
/// impl RecoveryPolicyHook for DisconnectOnEveryError {
/// fn recovery_policy(&self, _error: &CodecError, _ctx: &CodecErrorContext) -> RecoveryPolicy {
/// RecoveryPolicy::Disconnect
/// }
/// }
///
/// let app: WireframeApp = WireframeApp::new()
/// .expect("builder")
/// .with_recovery_hook(DisconnectOnEveryError);
/// let _ = app;
/// ```
#[must_use]
pub fn with_recovery_hook<H>(self, hook: H) -> Self
where
H: RecoveryPolicyHook + 'static,
{
Self {
recovery_hook: Some(std::sync::Arc::new(hook)),
..self
}
}
/// Replace the global recovery settings.
///
/// These settings are independent of the installed hook: the drop limit
/// bounds how many consecutive frames a hook's `Drop` may discard, and
/// [`RecoveryConfig::log_dropped_frames`](crate::codec::RecoveryConfig::log_dropped_frames)
/// controls the log line emitted for an applied drop.
///
/// [`RecoveryConfig::quarantine_duration`](crate::codec::RecoveryConfig::quarantine_duration)
/// is only a fallback. When a hook is installed, its
/// [`quarantine_duration`](RecoveryPolicyHook::quarantine_duration) method
/// decides how long a quarantine lasts, because the hook may vary the pause
/// per error.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use wireframe::{app::WireframeApp, codec::RecoveryConfig};
///
/// let app: WireframeApp = WireframeApp::new().expect("builder").recovery_config(
/// RecoveryConfig::default()
/// .max_consecutive_drops(3)
/// .quarantine_duration(Duration::from_millis(250)),
/// );
/// let _ = app;
/// ```
#[must_use]
pub const fn recovery_config(mut self, config: RecoveryConfig) -> Self {
self.recovery_config = config;
self
}
}
#[cfg(test)]
mod tests {
//! Coverage for recovery hook installation and propagation.
use std::{error::Error, io, time::Duration};
use rstest::{fixture, rstest};
use super::*;
use crate::{
app::Envelope,
codec::{
CodecError,
CodecErrorContext,
LengthDelimitedFrameCodec,
RecoveryConfig,
RecoveryPolicy,
RecoveryPolicyHook,
},
serializer::BincodeSerializer,
};
type TestApp = WireframeApp<BincodeSerializer, (), Envelope, LengthDelimitedFrameCodec>;
/// Recovery hook whose only observable property is that it is installed.
struct FixedPolicyHook;
impl RecoveryPolicyHook for FixedPolicyHook {
fn recovery_policy(&self, _error: &CodecError, _ctx: &CodecErrorContext) -> RecoveryPolicy {
RecoveryPolicy::Drop
}
}
#[fixture]
fn app_builder() -> Result<TestApp, Box<dyn Error>> {
TestApp::new().map_err(|error| {
io::Error::other(format!("failed to construct test app: {error}")).into()
})
}
#[fixture]
fn custom_config() -> RecoveryConfig {
RecoveryConfig::default()
.max_consecutive_drops(3)
.quarantine_duration(Duration::from_millis(25))
.log_dropped_frames(false)
}
fn assert_custom_config_preserved(config: &RecoveryConfig, expected: &RecoveryConfig) {
assert_eq!(
config, expected,
"a type-changing builder method should preserve the recovery config"
);
}
/// Assert that `app` still carries the hook a builder step was given.
///
/// The assertion lives outside the tests because
/// `clippy::panic_in_result_fn` rejects panicking assertions in
/// `Result`-returning functions, and the tests return `Result` so fixture
/// failures can propagate with `?`.
fn assert_hook_installed(app: &TestApp, step: &str) {
assert!(
app.recovery_hook.is_some(),
"{step} should preserve the recovery hook"
);
}
/// Assert that `app` is in the state a freshly built application has.
fn assert_default_recovery_state(app: &TestApp) {
assert!(
app.recovery_hook.is_none(),
"no recovery hook should be installed by default"
);
assert_eq!(app.recovery_config, RecoveryConfig::default());
}
#[rstest]
fn builder_defaults_to_no_hook_and_default_config(
app_builder: Result<TestApp, Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
let app = app_builder?;
assert_default_recovery_state(&app);
Ok(())
}
#[rstest]
fn with_recovery_hook_installs_hook(
app_builder: Result<TestApp, Box<dyn Error>>,
) -> Result<(), Box<dyn Error>> {
let app = app_builder?.with_recovery_hook(FixedPolicyHook);
assert_hook_installed(&app, "with_recovery_hook");
Ok(())
}
#[rstest]
fn recovery_config_replaces_settings(
app_builder: Result<TestApp, Box<dyn Error>>,
custom_config: RecoveryConfig,
) -> Result<(), Box<dyn Error>> {
let app = app_builder?.recovery_config(custom_config.clone());
assert_custom_config_preserved(&app.recovery_config, &custom_config);
Ok(())
}
/// A builder step that changes one of the application's type parameters.
///
/// Every variant ends at [`TestApp`] again, which lets one parameterized
/// test cover the transitions instead of repeating the same body per step.
#[derive(Clone, Copy)]
enum TypeTransition {
/// Replace the frame codec.
Codec,
/// Replace the serializer.
Serializer,
/// Register a setup hook, which changes the connection-state type.
ConnectionSetup,
}
/// Apply `transition` and return the application with its original types.
fn apply_transition(
app: TestApp,
transition: TypeTransition,
) -> Result<TestApp, Box<dyn Error>> {
match transition {
TypeTransition::Codec => Ok(app.with_codec(LengthDelimitedFrameCodec::new(4096))),
TypeTransition::Serializer => Ok(app.serializer(BincodeSerializer)),
TypeTransition::ConnectionSetup => Ok(app.on_connection_setup(|| async {})?),
}
}
#[rstest]
#[case::with_codec(TypeTransition::Codec, "with_codec")]
#[case::serializer(TypeTransition::Serializer, "serializer")]
#[case::connection_setup(TypeTransition::ConnectionSetup, "on_connection_setup")]
fn transition_preserves_recovery_state(
app_builder: Result<TestApp, Box<dyn Error>>,
custom_config: RecoveryConfig,
#[case] transition: TypeTransition,
#[case] step: &str,
) -> Result<(), Box<dyn Error>> {
let app = apply_transition(
app_builder?
.with_recovery_hook(FixedPolicyHook)
.recovery_config(custom_config.clone()),
transition,
)?;
assert_hook_installed(&app, step);
assert_custom_config_preserved(&app.recovery_config, &custom_config);
Ok(())
}
#[rstest]
#[case::with_codec(TypeTransition::Codec, "with_codec")]
#[case::serializer(TypeTransition::Serializer, "serializer")]
#[case::connection_setup(TypeTransition::ConnectionSetup, "on_connection_setup")]
fn transition_then_recovery_hook_installs_hook(
app_builder: Result<TestApp, Box<dyn Error>>,
#[case] transition: TypeTransition,
#[case] step: &str,
) -> Result<(), Box<dyn Error>> {
let app = apply_transition(app_builder?, transition)?.with_recovery_hook(FixedPolicyHook);
assert_hook_installed(&app, step);
Ok(())
}
}