autopush_common/db/redis/
mod.rs1mod redis_client;
15
16pub use redis_client::RedisClientImpl;
17
18use std::collections::HashMap;
19use std::time::Duration;
20
21use crate::db::error::DbError;
22use crate::notification::{Notification, default_ttl};
23use crate::util::deserialize_opt_u32_to_duration;
24
25use serde_derive::{Deserialize, Serialize};
26use uuid::Uuid;
27
28#[derive(Clone, Debug, Deserialize)]
30#[serde(default)]
31pub struct RedisDbSettings {
32 #[serde(deserialize_with = "deserialize_opt_u32_to_duration")]
33 pub create_timeout: Option<Duration>,
34 #[serde(deserialize_with = "deserialize_opt_u32_to_duration")]
35 pub router_ttl: Option<Duration>,
37 #[serde(deserialize_with = "deserialize_opt_u32_to_duration")]
38 pub notification_ttl: Option<Duration>,
40}
41
42#[allow(clippy::derivable_impls)]
43impl Default for RedisDbSettings {
44 fn default() -> Self {
45 Self {
46 create_timeout: Default::default(),
47 router_ttl: Some(Duration::from_secs(crate::MAX_ROUTER_TTL_SECS)),
48 notification_ttl: Some(Duration::from_secs(crate::MAX_NOTIFICATION_TTL_SECS)),
49 }
50 }
51}
52
53impl TryFrom<&str> for RedisDbSettings {
54 type Error = DbError;
55 fn try_from(setting_string: &str) -> Result<Self, Self::Error> {
56 let me: Self = match serde_json::from_str(setting_string) {
57 Ok(me) => me,
58 Err(e) if e.is_eof() => Self::default(),
59 Err(e) => Err(DbError::General(format!(
60 "Could not parse RedisDbSettings: {:?}",
61 e
62 )))?,
63 };
64 if let Some(router_ttl) = me.router_ttl
65 && router_ttl.as_secs() == 0
66 {
67 return Err(DbError::General(
68 "router_ttl must be greater than 0".to_string(),
69 ));
70 }
71 if let Some(notification_ttl) = me.notification_ttl
72 && notification_ttl.as_secs() == 0
73 {
74 return Err(DbError::General(
75 "notification_ttl must be greater than 0".to_string(),
76 ));
77 }
78 let me = Self {
81 router_ttl: me
82 .router_ttl
83 .or(Some(Duration::from_secs(crate::MAX_ROUTER_TTL_SECS))),
84 notification_ttl: me
85 .notification_ttl
86 .or(Some(Duration::from_secs(crate::MAX_NOTIFICATION_TTL_SECS))),
87 ..me
88 };
89 Ok(me)
90 }
91}
92
93#[derive(Serialize, Default, Deserialize, Clone, Debug)]
94pub struct StorableNotification {
98 #[serde(rename = "channelID")]
100 pub channel_id: Uuid,
101 pub version: String,
102 pub timestamp: u64,
103 #[serde(default = "default_ttl")]
108 pub ttl: u64,
109 #[serde(skip_serializing_if = "Option::is_none")]
111 pub topic: Option<String>,
112 #[serde(skip_serializing_if = "Option::is_none")]
113 pub data: Option<String>,
114 #[serde(skip_serializing_if = "Option::is_none")]
115 pub sortkey_timestamp: Option<u64>,
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub headers: Option<HashMap<String, String>>,
118 #[cfg(feature = "reliable_report")]
119 #[serde(skip_serializing_if = "Option::is_none")]
120 pub reliability_id: Option<String>,
121 #[cfg(feature = "reliable_report")]
122 #[serde(skip_serializing_if = "Option::is_none")]
123 pub reliable_state: Option<crate::reliability::ReliabilityState>,
124}
125
126impl From<Notification> for StorableNotification {
127 fn from(notification: Notification) -> Self {
128 Self {
129 channel_id: notification.channel_id,
130 version: notification.version,
131 timestamp: notification.timestamp,
132 ttl: notification.ttl,
133 topic: notification.topic,
134 data: notification.data,
135 sortkey_timestamp: notification.sortkey_timestamp,
136 headers: notification.headers,
137 #[cfg(feature = "reliable_report")]
138 reliability_id: notification.reliability_id,
139 #[cfg(feature = "reliable_report")]
140 reliable_state: notification.reliable_state,
141 }
142 }
143}
144
145impl From<StorableNotification> for Notification {
146 fn from(storable: StorableNotification) -> Self {
147 Self {
148 channel_id: storable.channel_id,
149 version: storable.version,
150 timestamp: storable.timestamp,
151 ttl: storable.ttl,
152 topic: storable.topic,
153 data: storable.data,
154 sortkey_timestamp: storable.sortkey_timestamp,
155 headers: storable.headers,
156 received_ms: None,
157 #[cfg(feature = "reliable_report")]
158 reliability_id: storable.reliability_id,
159 #[cfg(feature = "reliable_report")]
160 reliable_state: storable.reliable_state,
161 }
162 }
163}
164
165#[cfg(test)]
166mod tests {
167
168 use std::time::Duration;
169
170 #[test]
174 fn test_storable_notification_roundtrip() {
175 use super::StorableNotification;
176 use crate::notification::Notification;
177 use uuid::Uuid;
178
179 let notif = Notification {
181 channel_id: Uuid::parse_str("DECAFBAD-0000-0000-0000-0123456789AB").unwrap(),
182 version: "gAAAAAdeadbeef".to_owned(),
183 ttl: 300,
184 timestamp: 1_700_000_000,
185 data: Some("encrypted".to_owned()),
186 sortkey_timestamp: Some(1_700_000_000_123),
187 ..Default::default()
188 };
189 let expected_id = notif.chidmessageid();
190
191 let stored: StorableNotification = notif.into();
192 let json = serde_json::to_string(&stored).unwrap();
193 let back: Notification = serde_json::from_str::<StorableNotification>(&json)
194 .unwrap()
195 .into();
196
197 assert_eq!(back.ttl, 300);
198 assert_eq!(back.sortkey_timestamp, Some(1_700_000_000_123));
199 assert_eq!(back.topic, None);
200 assert_eq!(back.chidmessageid(), expected_id);
203 assert!(back.chidmessageid().starts_with("02:"));
204
205 let topic_notif = Notification {
207 channel_id: Uuid::parse_str("DECAFBAD-1111-0000-0000-0123456789AB").unwrap(),
208 version: "gAAAAAtopic".to_owned(),
209 ttl: 60,
210 timestamp: 1_700_000_000,
211 topic: Some("mytopic".to_owned()),
212 data: Some("encrypted".to_owned()),
213 ..Default::default()
214 };
215 let expected_topic_id = topic_notif.chidmessageid();
216 let stored: StorableNotification = topic_notif.into();
217 let json = serde_json::to_string(&stored).unwrap();
218 let back: Notification = serde_json::from_str::<StorableNotification>(&json)
219 .unwrap()
220 .into();
221 assert_eq!(back.topic, Some("mytopic".to_owned()));
222 assert_eq!(back.chidmessageid(), expected_topic_id);
223 assert!(back.chidmessageid().starts_with("01:"));
224 }
225
226 #[test]
227 fn test_settings_parse() -> Result<(), crate::db::error::DbError> {
228 let settings = super::RedisDbSettings::try_from("{\"create_timeout\": 123}")?;
229 assert_eq!(
230 settings.create_timeout,
231 Some(std::time::Duration::from_secs(123))
232 );
233 let settings = super::RedisDbSettings::try_from("{}")?;
234 assert_ne!(settings.router_ttl, Some(Duration::from_secs(0)));
235 assert_ne!(settings.notification_ttl, Some(Duration::from_secs(0)));
236 let settings = super::RedisDbSettings::try_from("{\"router_ttl\":0}");
237 assert!(settings.is_err());
238 let settings =
239 super::RedisDbSettings::try_from("{\"notification_ttl\": null, \"router_ttl\": null}")?;
240 assert_eq!(
241 settings.notification_ttl,
242 Some(std::time::Duration::from_secs(
243 crate::MAX_NOTIFICATION_TTL_SECS
244 ))
245 );
246 assert_eq!(
247 settings.router_ttl,
248 Some(std::time::Duration::from_secs(crate::MAX_ROUTER_TTL_SECS))
249 );
250 Ok(())
251 }
252}