This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.rs
More file actions
183 lines (156 loc) · 5.57 KB
/
Copy pathentry.rs
File metadata and controls
183 lines (156 loc) · 5.57 KB
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
// Project: hyperi-rustlib
// File: src/dlq/entry.rs
// Purpose: Shared DLQ entry envelope format
// Language: Rust
//
// License: BUSL-1.1
// Copyright: (c) 2026 HYPERI PTY LIMITED
//! Shared DLQ entry types used by all backends.
use base64::Engine;
use serde::{Deserialize, Serialize};
/// A failed message routed to the dead letter queue.
///
/// This envelope is backend-agnostic -- it carries the original payload
/// plus metadata about why and where the failure occurred.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DlqEntry {
/// When the failure occurred (epoch milliseconds).
pub timestamp: i64,
/// Service that generated this entry (e.g. "loader", "receiver").
pub service: String,
/// Why the message failed processing.
pub reason: String,
/// How many times the message was retried before being sent to DLQ.
pub attempts: u32,
/// Original message payload (base64-encoded in JSON).
#[serde(
serialize_with = "serialize_payload",
deserialize_with = "deserialize_payload"
)]
pub payload: Vec<u8>,
/// Intended destination (e.g. "db.table" or topic name).
#[serde(skip_serializing_if = "Option::is_none")]
pub destination: Option<String>,
/// Where the message originated.
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<DlqSource>,
}
/// Source metadata for a DLQ entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DlqSource {
/// Original Kafka topic.
#[serde(skip_serializing_if = "Option::is_none")]
pub topic: Option<String>,
/// Original Kafka partition.
#[serde(skip_serializing_if = "Option::is_none")]
pub partition: Option<i32>,
/// Original Kafka offset.
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<i64>,
}
impl DlqEntry {
/// Create a new DLQ entry with the current timestamp.
pub fn new(service: impl Into<String>, reason: impl Into<String>, payload: Vec<u8>) -> Self {
Self {
timestamp: chrono::Utc::now().timestamp_millis(),
service: service.into(),
reason: reason.into(),
attempts: 0,
payload,
destination: None,
source: None,
}
}
/// Set the intended destination.
#[must_use]
pub fn with_destination(mut self, dest: impl Into<String>) -> Self {
self.destination = Some(dest.into());
self
}
/// Set the source metadata.
#[must_use]
pub fn with_source(mut self, source: DlqSource) -> Self {
self.source = Some(source);
self
}
/// Set the retry attempt count.
#[must_use]
pub fn with_attempts(mut self, attempts: u32) -> Self {
self.attempts = attempts;
self
}
}
impl DlqSource {
/// Create source metadata from Kafka coordinates.
pub fn kafka(topic: impl Into<String>, partition: i32, offset: i64) -> Self {
Self {
topic: Some(topic.into()),
partition: Some(partition),
offset: Some(offset),
}
}
}
fn serialize_payload<S>(payload: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let encoded = base64::engine::general_purpose::STANDARD.encode(payload);
serializer.serialize_str(&encoded)
}
fn deserialize_payload<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
base64::engine::general_purpose::STANDARD
.decode(&s)
.map_err(serde::de::Error::custom)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_entry_new() {
let entry = DlqEntry::new("loader", "schema_mismatch", b"test payload".to_vec());
assert_eq!(entry.service, "loader");
assert_eq!(entry.reason, "schema_mismatch");
assert_eq!(entry.payload, b"test payload");
assert_eq!(entry.attempts, 0);
assert!(entry.destination.is_none());
assert!(entry.source.is_none());
assert!(entry.timestamp > 0);
}
#[test]
fn test_entry_builders() {
let entry = DlqEntry::new("receiver", "parse_error", b"bad json".to_vec())
.with_destination("acme.auth")
.with_source(DlqSource::kafka("events", 3, 12345))
.with_attempts(2);
assert_eq!(entry.destination.as_deref(), Some("acme.auth"));
assert_eq!(entry.attempts, 2);
let src = entry.source.as_ref().expect("source should be set");
assert_eq!(src.topic.as_deref(), Some("events"));
assert_eq!(src.partition, Some(3));
assert_eq!(src.offset, Some(12345));
}
#[test]
fn test_serde_roundtrip() {
let entry = DlqEntry::new("loader", "type_error", b"\x00\x01\x02\xff".to_vec())
.with_destination("db.table")
.with_source(DlqSource::kafka("topic", 0, 999));
let json = serde_json::to_string(&entry).expect("serialise");
let parsed: DlqEntry = serde_json::from_str(&json).expect("deserialise");
assert_eq!(parsed.service, entry.service);
assert_eq!(parsed.reason, entry.reason);
assert_eq!(parsed.payload, entry.payload);
assert_eq!(parsed.destination, entry.destination);
assert_eq!(parsed.attempts, entry.attempts);
}
#[test]
fn test_payload_base64_encoding() {
let entry = DlqEntry::new("test", "reason", b"hello world".to_vec());
let json = serde_json::to_string(&entry).expect("serialise");
// base64 of "hello world" is "aGVsbG8gd29ybGQ="
assert!(json.contains("aGVsbG8gd29ybGQ="));
}
}