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
//! Hooks for customizing codec error recovery behaviour.
use Duration;
use ;
use crateCodecError;
/// Hook trait for customizing codec error recovery behaviour.
///
/// Implementations can override default recovery policies based on
/// application-specific requirements or connection state.
///
/// # Default Implementation
///
/// The default implementation ([`DefaultRecoveryPolicy`]) delegates to
/// [`CodecError::default_recovery_policy`] for all errors.
///
/// # Installation
///
/// Install an implementation with
/// [`WireframeApp::with_recovery_hook`](crate::app::WireframeApp::with_recovery_hook).
/// The inbound connection loop then consults the hook for every decoder
/// failure on a live connection before it drops the frame, quarantines the
/// connection, or disconnects. Without an installed hook every decoder failure
/// disconnects the connection and the hook is never consulted.
///
/// # Consultation order
///
/// For each decoder failure the loop:
///
/// 1. Calls [`recovery_policy`](Self::recovery_policy) to select a policy.
/// 2. Escalates the selection to [`RecoveryPolicy::Disconnect`] when the failure is an end-of-file
/// error, when a non-disconnecting policy would discard a frame the decoder did not consume, or
/// when the selected [`RecoveryPolicy::Drop`] exceeds
/// [`RecoveryConfig::max_consecutive_drops`](crate::codec::RecoveryConfig::max_consecutive_drops).
/// 3. Resolves the pause through [`quarantine_duration`](Self::quarantine_duration) when the
/// applied policy is [`RecoveryPolicy::Quarantine`].
/// 4. Calls [`on_error`](Self::on_error) with the *applied* policy.
/// 5. Records `wireframe_codec_errors_total` once for the failure.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use wireframe::codec::{
/// CodecError,
/// CodecErrorContext,
/// FramingError,
/// RecoveryPolicy,
/// RecoveryPolicyHook,
/// };
///
/// /// Pause after a protocol violation, but block peers that oversize a frame.
/// struct PauseOnProtocolErrors;
///
/// impl RecoveryPolicyHook for PauseOnProtocolErrors {
/// fn recovery_policy(&self, error: &CodecError, _ctx: &CodecErrorContext) -> RecoveryPolicy {
/// match error {
/// // A single malformed message may be a glitch worth resynchronizing after.
/// CodecError::Protocol(_) => RecoveryPolicy::Quarantine,
/// // No honest peer declares a frame beyond the configured maximum.
/// CodecError::Framing(FramingError::OversizedFrame { .. }) => {
/// RecoveryPolicy::Disconnect
/// }
/// other => other.default_recovery_policy(),
/// }
/// }
///
/// fn quarantine_duration(&self, _error: &CodecError, _ctx: &CodecErrorContext) -> Duration {
/// Duration::from_secs(5)
/// }
/// }
/// ```
/// Default recovery policy implementation.
///
/// This implementation uses the built-in default policies from
/// [`CodecError::default_recovery_policy`] without any customization.
;