confers 0.4.0

Production-ready Rust configuration library with zero boilerplate
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Redis-based ConfigBus implementation.

use std::pin::Pin;

use async_trait::async_trait;
use futures_util::{Stream, StreamExt};
use redis::AsyncCommands;

use super::{ConfigBus, ConfigChangeEvent};
use crate::error::{ConfigConfigError, ConfigError, ConfigResult};
use crate::lifecycle::Lifecycle;

/// Default retry wait time when no message is available (100ms).
const DEFAULT_RETRY_WAIT_MS: u64 = 100;

/// Default error retry wait time (1 second).
const DEFAULT_ERROR_RETRY_WAIT_SECS: u64 = 1;

/// Redis default port.
const DEFAULT_REDIS_PORT: u16 = 6379;

pub struct RedisConfigBus {
    client: redis::Client,
    channel: String,
    /// Retry wait time in milliseconds when no message available.
    ///
    /// Historical field retained for builder API compatibility. The new
    /// `subscribe()` implementation uses a dedicated PubSub connection with
    /// `on_message()` push delivery, so this value is no longer read.
    #[allow(dead_code)]
    retry_wait_ms: u64,
    /// Error retry wait time in seconds.
    ///
    /// Historical field retained for builder API compatibility. See
    /// `retry_wait_ms` for context.
    #[allow(dead_code)]
    error_retry_wait_secs: u64,
}

impl RedisConfigBus {
    fn sanitize_url(url: &str) -> String {
        if let Ok(parsed) = url::Url::parse(url) {
            let host = parsed.host_str().unwrap_or("unknown");
            let port = parsed.port().unwrap_or(DEFAULT_REDIS_PORT);
            format!("{}:{}", host, port)
        } else {
            "invalid_url".to_string()
        }
    }

    pub async fn connect(url: &str, channel: impl Into<String>) -> ConfigResult<Self> {
        Self::connect_with_config(
            url,
            channel,
            DEFAULT_RETRY_WAIT_MS,
            DEFAULT_ERROR_RETRY_WAIT_SECS,
        )
        .await
    }

    pub async fn connect_with_pool(
        url: &str,
        channel: impl Into<String>,
        _pool_size: usize,
    ) -> ConfigResult<Self> {
        Self::connect(url, channel).await
    }

    /// Connect with custom retry wait times.
    pub async fn connect_with_config(
        url: &str,
        channel: impl Into<String>,
        retry_wait_ms: u64,
        error_retry_wait_secs: u64,
    ) -> ConfigResult<Self> {
        let safe_host = Self::sanitize_url(url);

        let client = redis::Client::open(url).map_err(|e| ConfigError::RemoteUnavailable {
            error_type: format!("redis_connection_failed: host={}, error={}", safe_host, e),
            retryable: true,
        })?;

        Ok(Self {
            client,
            channel: channel.into(),
            retry_wait_ms,
            error_retry_wait_secs,
        })
    }
}

#[async_trait]
impl Lifecycle for RedisConfigBus {
    async fn start(&self) -> Result<(), ConfigConfigError> {
        Ok(())
    }

    async fn stop(&self) -> ConfigResult<()> {
        Ok(())
    }
}

#[async_trait]
impl ConfigBus for RedisConfigBus {
    async fn publish(&self, event: ConfigChangeEvent) -> ConfigResult<()> {
        let mut conn = self
            .client
            .get_multiplexed_async_connection()
            .await
            .map_err(|e| ConfigError::RemoteUnavailable {
                error_type: format!("redis_connection: {}", e),
                retryable: true,
            })?;

        let payload = serde_json::to_vec(&event).map_err(|e| ConfigError::SourceChainError {
            message: format!("serialize event: {}", e),
            source_index: 0,
        })?;

        conn.publish::<_, _, ()>(&self.channel, payload)
            .await
            .map_err(|e| ConfigError::RemoteUnavailable {
                error_type: format!("redis_publish: {}", e),
                retryable: true,
            })?;

        Ok(())
    }

    async fn subscribe(
        &self,
    ) -> ConfigResult<Pin<Box<dyn Stream<Item = ConfigChangeEvent> + Send>>> {
        // Use a dedicated PubSub connection (not multiplexed). Redis' SUBSCRIBE
        // command transitions the connection into subscription mode, which is
        // incompatible with multiplexed-connection query semantics. The previous
        // implementation used `redis::cmd("SUBSCRIBE").query_async()` on a
        // multiplexed connection, which never delivers real published messages.
        let mut pubsub =
            self.client
                .get_async_pubsub()
                .await
                .map_err(|e| ConfigError::RemoteUnavailable {
                    error_type: format!("redis_pubsub: {}", e),
                    retryable: true,
                })?;

        pubsub
            .subscribe(&self.channel)
            .await
            .map_err(|e| ConfigError::RemoteUnavailable {
                error_type: format!("redis_subscribe: {}", e),
                retryable: true,
            })?;

        // on_message(&mut self) borrows pubsub and returns a Stream<Item = Msg>.
        // Wrap the polling loop in async_stream::stream! so pubsub is owned by
        // the stream future itself (avoids E0515: cannot return reference to
        // local). Payload decode errors and JSON deserialization errors are
        // skipped (Rule 12: errors that cannot be meaningfully surfaced to the
        // stream consumer are skipped via continue, not silently swallowed).
        let stream = async_stream::stream! {
            let mut msg_stream = pubsub.on_message();
            while let Some(msg) = msg_stream.next().await {
                let payload: Vec<u8> = match msg.get_payload() {
                    Ok(p) => p,
                    Err(_) => continue,
                };
                match serde_json::from_slice::<ConfigChangeEvent>(&payload) {
                    Ok(event) => yield event,
                    Err(_) => continue,
                }
            }
        };

        Ok(Box::pin(stream))
    }
}

pub struct RedisBusBuilder {
    url: Option<String>,
    channel: Option<String>,
    pool_size: Option<usize>,
    /// Retry wait time in milliseconds when no message available.
    retry_wait_ms: u64,
    /// Error retry wait time in seconds.
    error_retry_wait_secs: u64,
}

impl RedisBusBuilder {
    pub fn new() -> Self {
        Self {
            url: None,
            channel: None,
            pool_size: None,
            retry_wait_ms: DEFAULT_RETRY_WAIT_MS,
            error_retry_wait_secs: DEFAULT_ERROR_RETRY_WAIT_SECS,
        }
    }

    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self
    }

    pub fn channel(mut self, channel: impl Into<String>) -> Self {
        self.channel = Some(channel.into());
        self
    }

    pub fn pool_size(mut self, size: usize) -> Self {
        self.pool_size = Some(size);
        self
    }

    /// Set retry wait time in milliseconds when no message is available.
    ///
    /// Default: 100ms.
    pub fn retry_wait_ms(mut self, ms: u64) -> Self {
        self.retry_wait_ms = ms;
        self
    }

    /// Set error retry wait time in seconds.
    ///
    /// Default: 1 second.
    pub fn error_retry_wait_secs(mut self, secs: u64) -> Self {
        self.error_retry_wait_secs = secs;
        self
    }

    pub async fn build(self) -> ConfigResult<RedisConfigBus> {
        let url = self.url.ok_or(ConfigError::InvalidValue {
            key: "redis_url".to_string(),
            expected_type: "string".to_string(),
            message: "Redis URL is required".to_string(),
        })?;

        let channel = self.channel.unwrap_or_else(|| "config:events".to_string());

        RedisConfigBus::connect_with_config(
            &url,
            channel,
            self.retry_wait_ms,
            self.error_retry_wait_secs,
        )
        .await
    }
}

impl Default for RedisBusBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures_util::StreamExt;
    use tokio::time::{timeout, Duration};

    /// Probe whether the local Redis test service is accepting connections.
    fn redis_ready() -> bool {
        std::net::TcpStream::connect(("127.0.0.1", 16379)).is_ok()
    }

    /// Process-unique channel name to keep parallel tests isolated.
    fn unique(suffix: &str) -> String {
        use std::sync::atomic::{AtomicU64, Ordering};
        static N: AtomicU64 = AtomicU64::new(0);
        format!(
            "confers.unit.{}.{}",
            N.fetch_add(1, Ordering::SeqCst),
            suffix
        )
    }

    /// Obtain a port that is (almost certainly) closed to trigger connection
    /// errors without relying on a hard-coded port number.
    fn closed_port() -> u16 {
        let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap();
        let port = listener.local_addr().unwrap().port();
        drop(listener);
        port
    }

    fn event(checksum: &str) -> ConfigChangeEvent {
        ConfigChangeEvent::new(
            "test-instance",
            "unit-test",
            vec!["key.alpha".to_string()],
            checksum,
        )
    }

    // ==================== sanitize_url (private helper) ====================

    #[test]
    fn test_sanitize_url_valid_host_and_port() {
        assert_eq!(
            RedisConfigBus::sanitize_url("redis://1.2.3.4:7000"),
            "1.2.3.4:7000"
        );
    }

    #[test]
    fn test_sanitize_url_uses_default_port_when_absent() {
        assert_eq!(
            RedisConfigBus::sanitize_url("redis://1.2.3.4"),
            "1.2.3.4:6379"
        );
    }

    #[test]
    fn test_sanitize_url_invalid_returns_sentinel() {
        assert_eq!(RedisConfigBus::sanitize_url("not-a-url"), "invalid_url");
    }

    // ==================== RedisBusBuilder ====================

    #[test]
    fn test_builder_new_defaults() {
        let b = RedisBusBuilder::new();
        assert!(b.url.is_none());
        assert!(b.channel.is_none());
        assert!(b.pool_size.is_none());
        assert_eq!(b.retry_wait_ms, DEFAULT_RETRY_WAIT_MS);
        assert_eq!(b.error_retry_wait_secs, DEFAULT_ERROR_RETRY_WAIT_SECS);
    }

    #[test]
    fn test_builder_default_impl_matches_new() {
        let d = RedisBusBuilder::default();
        assert!(d.url.is_none());
        assert!(d.channel.is_none());
        assert_eq!(d.retry_wait_ms, DEFAULT_RETRY_WAIT_MS);
        assert_eq!(d.error_retry_wait_secs, DEFAULT_ERROR_RETRY_WAIT_SECS);
    }

    #[test]
    fn test_builder_setters_chain_and_store() {
        let b = RedisBusBuilder::new()
            .url("redis://127.0.0.1:16379")
            .channel("unit-chan")
            .pool_size(8)
            .retry_wait_ms(42)
            .error_retry_wait_secs(3);
        assert_eq!(b.url.as_deref(), Some("redis://127.0.0.1:16379"));
        assert_eq!(b.channel.as_deref(), Some("unit-chan"));
        assert_eq!(b.pool_size, Some(8));
        assert_eq!(b.retry_wait_ms, 42);
        assert_eq!(b.error_retry_wait_secs, 3);
    }

    #[tokio::test]
    async fn test_build_without_url_returns_invalid_value() {
        let err = RedisBusBuilder::new()
            .channel("c")
            .build()
            .await
            .err()
            .expect("build should error");
        match err {
            ConfigError::InvalidValue {
                key,
                expected_type,
                message,
            } => {
                assert_eq!(key, "redis_url");
                assert_eq!(expected_type, "string");
                assert!(message.contains("required"), "message={}", message);
            }
            other => panic!("expected InvalidValue, got {:?}", other),
        }
    }

    // ==================== connect error paths (no service needed) ====================

    #[tokio::test]
    async fn test_connect_invalid_url_is_retryable() {
        let err = RedisConfigBus::connect("not-a-valid-url", "chan")
            .await
            .err()
            .expect("connect should error");
        match err {
            ConfigError::RemoteUnavailable {
                retryable,
                error_type,
            } => {
                assert!(retryable, "should be retryable");
                // sanitize_url returns "invalid_url" for unparseable input, and
                // the error_type embeds the sanitized host.
                assert!(
                    error_type.contains("invalid_url")
                        || error_type.contains("redis_connection_failed"),
                    "error_type={}",
                    error_type
                );
            }
            other => panic!("expected RemoteUnavailable, got {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_connect_with_pool_invalid_url_is_retryable() {
        let err = RedisConfigBus::connect_with_pool("not-a-valid-url", "chan", 4)
            .await
            .err()
            .expect("connect_with_pool should error");
        assert!(matches!(
            err,
            ConfigError::RemoteUnavailable {
                retryable: true,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_connect_with_config_dead_port_opens_client() {
        // Client::open succeeds for a syntactically valid URL pointing at a
        // closed port; failure surfaces later at publish/subscribe time.
        let port = closed_port();
        let bus = RedisConfigBus::connect_with_config(
            &format!("redis://127.0.0.1:{}", port),
            "chan",
            10,
            1,
        )
        .await
        .expect("client open should succeed for valid URL");
        assert_eq!(bus.channel, "chan");
        assert_eq!(bus.retry_wait_ms, 10);
        assert_eq!(bus.error_retry_wait_secs, 1);
    }

    #[tokio::test]
    async fn test_publish_dead_port_returns_retryable_error() {
        let port = closed_port();
        let bus = RedisConfigBus::connect(&format!("redis://127.0.0.1:{}", port), "chan")
            .await
            .expect("client open succeeds");
        let result = timeout(Duration::from_secs(3), bus.publish(event("dead"))).await;
        let err = result.expect("publish should not time out").unwrap_err();
        assert!(matches!(
            err,
            ConfigError::RemoteUnavailable {
                retryable: true,
                ..
            }
        ));
    }

    #[tokio::test]
    async fn test_subscribe_dead_port_returns_retryable_error() {
        let port = closed_port();
        let bus = RedisConfigBus::connect(&format!("redis://127.0.0.1:{}", port), "chan")
            .await
            .expect("client open succeeds");
        let result = timeout(Duration::from_secs(3), bus.subscribe()).await;
        let err = result
            .expect("subscribe should not time out")
            .err()
            .expect("subscribe should have errored");
        assert!(matches!(
            err,
            ConfigError::RemoteUnavailable {
                retryable: true,
                ..
            }
        ));
    }

    // ==================== Lifecycle (start/stop are no-ops for Redis) ====================

    #[tokio::test]
    async fn test_lifecycle_start_stop_are_noops() {
        // start/stop do not require a live connection for Redis.
        let port = closed_port();
        let bus = RedisConfigBus::connect(&format!("redis://127.0.0.1:{}", port), "chan")
            .await
            .expect("client open succeeds");
        bus.start().await.expect("start is always Ok");
        bus.stop().await.expect("stop is always Ok");
    }

    // ==================== Service-required tests ====================

    #[tokio::test]
    async fn test_build_success_uses_default_channel() {
        if !redis_ready() {
            eprintln!("Skipping test: Redis not available");
            return;
        }
        let bus = RedisBusBuilder::new()
            .url("redis://127.0.0.1:16379")
            .build()
            .await
            .expect("build should succeed with live Redis");
        assert_eq!(bus.channel, "config:events");
    }

    #[tokio::test]
    async fn test_connect_with_pool_live_service() {
        if !redis_ready() {
            eprintln!("Skipping test: Redis not available");
            return;
        }
        let bus = RedisConfigBus::connect_with_pool("redis://127.0.0.1:16379", "p", 4)
            .await
            .expect("connect_with_pool should succeed");
        assert_eq!(bus.channel, "p");
    }

    #[tokio::test]
    async fn test_publish_subscribe_roundtrip() {
        if !redis_ready() {
            eprintln!("Skipping test: Redis not available");
            return;
        }
        let channel = unique("roundtrip");
        let bus = RedisConfigBus::connect("redis://127.0.0.1:16379", &channel)
            .await
            .expect("connect");
        let mut rx = bus.subscribe().await.expect("subscribe");

        // Give Redis time to register the pubsub subscription before publishing
        // (at-most-once delivery race).
        tokio::time::sleep(Duration::from_millis(150)).await;

        let ev = event("rt-ck");
        bus.publish(ev.clone()).await.expect("publish");

        let received = timeout(Duration::from_secs(2), rx.next())
            .await
            .expect("timed out waiting for message")
            .expect("stream ended");

        assert_eq!(received.instance_id, ev.instance_id);
        assert_eq!(received.source, ev.source);
        assert_eq!(received.changed_keys, ev.changed_keys);
        assert_eq!(received.checksum, ev.checksum);
    }

    #[tokio::test]
    async fn test_subscribe_skips_invalid_payload() {
        // Publish raw non-JSON bytes directly to the channel via a separate
        // client. The subscribe stream's decode-error continue branch must
        // skip them and still deliver a subsequently published valid event.
        if !redis_ready() {
            eprintln!("Skipping test: Redis not available");
            return;
        }
        let channel = unique("badpayload");
        let bus = RedisConfigBus::connect("redis://127.0.0.1:16379", &channel)
            .await
            .expect("connect");
        let mut rx = bus.subscribe().await.expect("subscribe");
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Inject an invalid payload directly via a standalone client.
        let pub_client = redis::Client::open("redis://127.0.0.1:16379").unwrap();
        let mut conn = pub_client
            .get_multiplexed_async_connection()
            .await
            .expect("pub connection");
        conn.publish::<_, _, ()>(&channel, b"not-json".to_vec())
            .await
            .expect("raw publish");

        // Now publish a valid event through the bus.
        let ev = event("good-ck");
        bus.publish(ev.clone()).await.expect("publish");

        let received = timeout(Duration::from_secs(2), rx.next())
            .await
            .expect("timed out waiting for valid message")
            .expect("stream ended");

        // The invalid payload must have been skipped, leaving the valid event.
        assert_eq!(received.checksum, "good-ck");
        assert_eq!(received.instance_id, ev.instance_id);
    }
}