hexcfg 1.1.4

A hexagonal architecture configuration loading crate with multi-source support
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Redis configuration source adapter.
//!
//! This module provides an adapter that reads configuration values from Redis.

use crate::domain::{ConfigError, ConfigKey, ConfigValue, Result};
use crate::ports::ConfigSource;
use once_cell::sync::Lazy;
use redis::aio::MultiplexedConnection;
use redis::{AsyncCommands, Client};
use std::collections::HashMap;
use std::sync::Arc;

/// Shared runtime for reload operations to avoid expensive runtime creation on every reload
static RELOAD_RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
    tokio::runtime::Runtime::new().expect("Failed to create reload runtime for Redis adapter")
});

/// Storage mode for Redis configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RedisStorageMode {
    /// Store each configuration key as a separate Redis key with a prefix.
    /// Example: prefix:database.host, prefix:database.port
    StringKeys,
    /// Store all configuration in a single Redis hash.
    /// Example: HGETALL config_hash
    Hash,
}

/// Configuration source adapter for Redis.
///
/// This adapter reads configuration values from a Redis instance. It supports
/// both string key storage (with prefix) and hash storage modes.
///
/// # Priority
///
/// Redis has a default priority of 1, but this can be customized.
///
/// # Examples
///
/// ```rust,no_run
/// use hexcfg::adapters::{RedisAdapter, RedisStorageMode};
/// use hexcfg::ports::ConfigSource;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Connect to Redis using string keys with prefix
/// let adapter = RedisAdapter::new(
///     "redis://localhost:6379",
///     "myapp:",
///     RedisStorageMode::StringKeys
/// ).await?;
///
/// // Or use hash storage
/// let adapter = RedisAdapter::new(
///     "redis://localhost:6379",
///     "myapp:config",
///     RedisStorageMode::Hash
/// ).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct RedisAdapter {
    /// Redis client
    client: Arc<Client>,
    /// Key prefix or hash key name
    namespace: String,
    /// Storage mode (string keys or hash)
    storage_mode: RedisStorageMode,
    /// Priority for this source
    priority: u8,
    /// Cached configuration values
    cache: HashMap<String, String>,
}

impl RedisAdapter {
    /// Validates namespace to prevent injection attacks
    fn validate_namespace(namespace: &str) -> Result<()> {
        // Disallow wildcard characters and other special Redis pattern characters
        if namespace.contains(['*', '?', '[', ']', '\\']) {
            return Err(ConfigError::SourceError {
                source_name: "redis".to_string(),
                message: "Namespace contains invalid characters (* ? [ ] \\)".to_string(),
                source: None,
            });
        }
        Ok(())
    }

    /// Creates a new Redis adapter with the given connection URL.
    ///
    /// # Arguments
    ///
    /// * `url` - Redis connection URL (e.g., `"redis://localhost:6379"`)
    /// * `namespace` - Key prefix (for StringKeys mode) or hash key name (for Hash mode)
    /// * `storage_mode` - Whether to use string keys or hash storage
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use hexcfg::adapters::{RedisAdapter, RedisStorageMode};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let adapter = RedisAdapter::new(
    ///     "redis://localhost:6379",
    ///     "myapp:",
    ///     RedisStorageMode::StringKeys
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(url: &str, namespace: &str, storage_mode: RedisStorageMode) -> Result<Self> {
        // Validate namespace to prevent injection attacks
        Self::validate_namespace(namespace)?;

        let client = Client::open(url).map_err(|e| ConfigError::SourceError {
            source_name: "redis".to_string(),
            message: format!("Failed to create Redis client: {}", e),
            source: Some(Box::new(e)),
        })?;

        let mut adapter = Self {
            client: Arc::new(client),
            namespace: namespace.to_string(),
            storage_mode,
            priority: 1,
            cache: HashMap::new(),
        };

        // Initial load of all keys
        adapter.load_all_keys().await?;

        Ok(adapter)
    }

    /// Creates a new Redis adapter with a custom priority.
    ///
    /// # Arguments
    ///
    /// * `url` - Redis connection URL
    /// * `namespace` - Key prefix or hash key name
    /// * `storage_mode` - Whether to use string keys or hash storage
    /// * `priority` - Priority for this source (higher values override lower values)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use hexcfg::adapters::{RedisAdapter, RedisStorageMode};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let adapter = RedisAdapter::with_priority(
    ///     "redis://localhost:6379",
    ///     "myapp:",
    ///     RedisStorageMode::StringKeys,
    ///     2
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn with_priority(
        url: &str,
        namespace: &str,
        storage_mode: RedisStorageMode,
        priority: u8,
    ) -> Result<Self> {
        let mut adapter = Self::new(url, namespace, storage_mode).await?;
        adapter.priority = priority;
        Ok(adapter)
    }

    /// Gets a multiplexed connection to Redis.
    async fn get_connection(&self) -> Result<MultiplexedConnection> {
        self.client
            .get_multiplexed_async_connection()
            .await
            .map_err(|e| ConfigError::SourceError {
                source_name: "redis".to_string(),
                message: format!("Failed to connect to Redis: {}", e),
                source: Some(Box::new(e)),
            })
    }

    /// Loads all keys from Redis into the cache.
    async fn load_all_keys(&mut self) -> Result<()> {
        let mut conn = self.get_connection().await?;

        self.cache.clear();

        match self.storage_mode {
            RedisStorageMode::Hash => {
                // Load all fields from hash
                let hash: HashMap<String, String> =
                    conn.hgetall(&self.namespace)
                        .await
                        .map_err(|e| ConfigError::SourceError {
                            source_name: "redis".to_string(),
                            message: format!("Failed to fetch hash from Redis: {}", e),
                            source: Some(Box::new(e)),
                        })?;

                self.cache = hash;
            }
            RedisStorageMode::StringKeys => {
                // Use SCAN instead of KEYS to avoid blocking the Redis server
                let pattern = format!("{}*", self.namespace);
                let mut cursor: u64 = 0;
                let mut all_keys = Vec::new();

                loop {
                    let (new_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                        .arg(cursor)
                        .arg("MATCH")
                        .arg(&pattern)
                        .arg("COUNT")
                        .arg(100)
                        .query_async(&mut conn)
                        .await
                        .map_err(|e| ConfigError::SourceError {
                            source_name: "redis".to_string(),
                            message: format!("Failed to scan keys from Redis: {}", e),
                            source: Some(Box::new(e)),
                        })?;

                    all_keys.extend(keys);
                    cursor = new_cursor;
                    if cursor == 0 {
                        break;
                    }
                }

                // Fetch all values
                for key in all_keys {
                    let value: String =
                        conn.get(&key).await.map_err(|e| ConfigError::SourceError {
                            source_name: "redis".to_string(),
                            message: format!("Failed to fetch value from Redis: {}", e),
                            source: Some(Box::new(e)),
                        })?;

                    // Strip prefix from key
                    let key = if key.starts_with(&self.namespace) {
                        &key[self.namespace.len()..]
                    } else {
                        &key
                    };

                    self.cache.insert(key.to_string(), value);
                }
            }
        }

        Ok(())
    }

    /// Reloads all keys from Redis synchronously.
    ///
    /// Note: This method uses a shared runtime to perform async operations efficiently.
    /// If called from an async context, it will spawn a separate thread to avoid blocking.
    fn reload_sync(&mut self) -> Result<()> {
        let client = Arc::clone(&self.client);
        let namespace = self.namespace.clone();
        let storage_mode = self.storage_mode;

        // Try to use the current runtime if available, otherwise use the shared runtime
        let new_cache = if tokio::runtime::Handle::try_current().is_ok() {
            // We're in an async context, need to spawn a separate thread with the shared runtime
            // to avoid blocking the current runtime's executor
            let handle = std::thread::spawn(move || {
                RELOAD_RUNTIME.block_on(async move {
                    let mut conn =
                        client
                            .get_multiplexed_async_connection()
                            .await
                            .map_err(|e| ConfigError::SourceError {
                                source_name: "redis".to_string(),
                                message: format!("Failed to connect to Redis: {}", e),
                                source: Some(Box::new(e)),
                            })?;

                    let mut new_cache = HashMap::new();

                    match storage_mode {
                        RedisStorageMode::Hash => {
                            // Load all fields from hash
                            let hash: HashMap<String, String> = conn
                                .hgetall(&namespace)
                                .await
                                .map_err(|e| ConfigError::SourceError {
                                    source_name: "redis".to_string(),
                                    message: format!("Failed to fetch hash from Redis: {}", e),
                                    source: Some(Box::new(e)),
                                })?;

                            new_cache = hash;
                        }
                        RedisStorageMode::StringKeys => {
                            // Use SCAN instead of KEYS to avoid blocking the Redis server
                            let pattern = format!("{}*", namespace);
                            let mut cursor: u64 = 0;
                            let mut all_keys = Vec::new();

                            loop {
                                let (new_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                                    .arg(cursor)
                                    .arg("MATCH")
                                    .arg(&pattern)
                                    .arg("COUNT")
                                    .arg(100)
                                    .query_async(&mut conn)
                                    .await
                                    .map_err(|e| ConfigError::SourceError {
                                        source_name: "redis".to_string(),
                                        message: format!("Failed to scan keys from Redis: {}", e),
                                        source: Some(Box::new(e)),
                                    })?;

                                all_keys.extend(keys);
                                cursor = new_cursor;
                                if cursor == 0 {
                                    break;
                                }
                            }

                            // Fetch all values
                            for key in all_keys {
                                let value: String =
                                    conn.get(&key).await.map_err(|e| ConfigError::SourceError {
                                        source_name: "redis".to_string(),
                                        message: format!("Failed to fetch value from Redis: {}", e),
                                        source: Some(Box::new(e)),
                                    })?;

                                // Strip prefix from key
                                let key = if key.starts_with(&namespace) {
                                    &key[namespace.len()..]
                                } else {
                                    &key
                                };

                                new_cache.insert(key.to_string(), value);
                            }
                        }
                    }

                    Ok::<HashMap<String, String>, ConfigError>(new_cache)
                })
            });

            handle.join().map_err(|_| ConfigError::SourceError {
                source_name: "redis".to_string(),
                message: "Failed to join reload thread".to_string(),
                source: None,
            })?
        } else {
            // No runtime available, use the shared runtime
            RELOAD_RUNTIME.block_on(async move {
                let mut conn = client
                    .get_multiplexed_async_connection()
                    .await
                    .map_err(|e| ConfigError::SourceError {
                        source_name: "redis".to_string(),
                        message: format!("Failed to connect to Redis: {}", e),
                        source: Some(Box::new(e)),
                    })?;

                let mut new_cache = HashMap::new();

                match storage_mode {
                    RedisStorageMode::Hash => {
                        // Load all fields from hash
                        let hash: HashMap<String, String> = conn
                            .hgetall(&namespace)
                            .await
                            .map_err(|e| ConfigError::SourceError {
                                source_name: "redis".to_string(),
                                message: format!("Failed to fetch hash from Redis: {}", e),
                                source: Some(Box::new(e)),
                            })?;

                        new_cache = hash;
                    }
                    RedisStorageMode::StringKeys => {
                        // Use SCAN instead of KEYS to avoid blocking the Redis server
                        let pattern = format!("{}*", namespace);
                        let mut cursor: u64 = 0;
                        let mut all_keys = Vec::new();

                        loop {
                            let (new_cursor, keys): (u64, Vec<String>) = redis::cmd("SCAN")
                                .arg(cursor)
                                .arg("MATCH")
                                .arg(&pattern)
                                .arg("COUNT")
                                .arg(100)
                                .query_async(&mut conn)
                                .await
                                .map_err(|e| ConfigError::SourceError {
                                    source_name: "redis".to_string(),
                                    message: format!("Failed to scan keys from Redis: {}", e),
                                    source: Some(Box::new(e)),
                                })?;

                            all_keys.extend(keys);
                            cursor = new_cursor;
                            if cursor == 0 {
                                break;
                            }
                        }

                        // Fetch all values
                        for key in all_keys {
                            let value: String =
                                conn.get(&key).await.map_err(|e| ConfigError::SourceError {
                                    source_name: "redis".to_string(),
                                    message: format!("Failed to fetch value from Redis: {}", e),
                                    source: Some(Box::new(e)),
                                })?;

                            // Strip prefix from key
                            let key = if key.starts_with(&namespace) {
                                &key[namespace.len()..]
                            } else {
                                &key
                            };

                            new_cache.insert(key.to_string(), value);
                        }
                    }
                }

                Ok::<HashMap<String, String>, ConfigError>(new_cache)
            })
        }?;

        self.cache = new_cache;
        Ok(())
    }
}

impl ConfigSource for RedisAdapter {
    fn name(&self) -> &str {
        "redis"
    }

    fn priority(&self) -> u8 {
        self.priority
    }

    fn get(&self, key: &ConfigKey) -> Result<Option<ConfigValue>> {
        Ok(self
            .cache
            .get(key.as_str())
            .map(|v| ConfigValue::from(v.as_str())))
    }

    fn all_keys(&self) -> Result<Vec<ConfigKey>> {
        Ok(self
            .cache
            .keys()
            .map(|k| ConfigKey::from(k.as_str()))
            .collect())
    }

    fn reload(&mut self) -> Result<()> {
        self.reload_sync()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_redis_storage_modes() {
        // Test that both storage modes are available
        assert_eq!(RedisStorageMode::StringKeys, RedisStorageMode::StringKeys);
        assert_eq!(RedisStorageMode::Hash, RedisStorageMode::Hash);
        assert_ne!(RedisStorageMode::StringKeys, RedisStorageMode::Hash);
    }
}