litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Concurrent-safe Map implementation based on DashMap
//!
//! Provides a thread-safe HashMap with fine-grained locking for high-performance
//! concurrent access. This is a thin wrapper around DashMap with additional
//! convenience methods.

use dashmap::DashMap;
use std::hash::Hash;
use std::sync::Arc;

/// A concurrent-safe HashMap based on DashMap.
///
/// This container provides thread-safe access to a key-value store with
/// fine-grained locking, allowing multiple readers and writers to access
/// different keys simultaneously.
///
/// # Type Parameters
///
/// * `K` - The key type, must implement `Eq + Hash`
/// * `V` - The value type, must implement `Clone`
///
/// # Example
///
/// ```rust
/// use litellm_rs::utils::sync::ConcurrentMap;
///
/// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
/// map.insert("key".to_string(), 42);
/// assert_eq!(map.get(&"key".to_string()), Some(42));
/// ```
#[derive(Debug)]
pub struct ConcurrentMap<K, V>
where
    K: Eq + Hash,
{
    inner: Arc<DashMap<K, V>>,
}

impl<K, V> Default for ConcurrentMap<K, V>
where
    K: Eq + Hash,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<K, V> Clone for ConcurrentMap<K, V>
where
    K: Eq + Hash,
{
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<K, V> ConcurrentMap<K, V>
where
    K: Eq + Hash,
{
    /// Creates a new empty `ConcurrentMap`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// assert!(map.is_empty());
    /// ```
    pub fn new() -> Self {
        Self {
            inner: Arc::new(DashMap::new()),
        }
    }

    /// Creates a new `ConcurrentMap` with the specified capacity.
    ///
    /// # Arguments
    ///
    /// * `capacity` - The initial capacity of the map
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::with_capacity(100);
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: Arc::new(DashMap::with_capacity(capacity)),
        }
    }

    /// Inserts a key-value pair into the map.
    ///
    /// If the map already had this key present, the old value is returned.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to insert
    /// * `value` - The value to insert
    ///
    /// # Returns
    ///
    /// The old value if the key was present, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// assert_eq!(map.insert("key".to_string(), 1), None);
    /// assert_eq!(map.insert("key".to_string(), 2), Some(1));
    /// ```
    pub fn insert(&self, key: K, value: V) -> Option<V> {
        self.inner.insert(key, value)
    }

    /// Returns the number of elements in the map.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// map.insert("b".to_string(), 2);
    /// assert_eq!(map.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if the map contains no elements.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// assert!(map.is_empty());
    /// map.insert("key".to_string(), 1);
    /// assert!(!map.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Returns `true` if the map contains the specified key.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to check
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("key".to_string(), 1);
    /// assert!(map.contains_key(&"key".to_string()));
    /// assert!(!map.contains_key(&"other".to_string()));
    /// ```
    pub fn contains_key(&self, key: &K) -> bool {
        self.inner.contains_key(key)
    }

    /// Removes a key from the map, returning the value if present.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to remove
    ///
    /// # Returns
    ///
    /// The value if the key was present, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("key".to_string(), 42);
    /// assert_eq!(map.remove(&"key".to_string()), Some(42));
    /// assert_eq!(map.remove(&"key".to_string()), None);
    /// ```
    pub fn remove(&self, key: &K) -> Option<V> {
        self.inner.remove(key).map(|(_, v)| v)
    }

    /// Clears the map, removing all key-value pairs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// map.insert("b".to_string(), 2);
    /// map.clear();
    /// assert!(map.is_empty());
    /// ```
    pub fn clear(&self) {
        self.inner.clear();
    }

    /// Returns all keys in the map.
    ///
    /// Note: This creates a snapshot of the keys at the time of the call.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// map.insert("b".to_string(), 2);
    /// let keys = map.keys();
    /// assert_eq!(keys.len(), 2);
    /// ```
    pub fn keys(&self) -> Vec<K>
    where
        K: Clone,
    {
        self.inner.iter().map(|r| r.key().clone()).collect()
    }
}

impl<K, V> ConcurrentMap<K, V>
where
    K: Eq + Hash,
    V: Clone,
{
    /// Gets a clone of the value for the specified key.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up
    ///
    /// # Returns
    ///
    /// A clone of the value if present, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("key".to_string(), 42);
    /// assert_eq!(map.get(&"key".to_string()), Some(42));
    /// assert_eq!(map.get(&"other".to_string()), None);
    /// ```
    pub fn get(&self, key: &K) -> Option<V> {
        self.inner.get(key).map(|r| r.value().clone())
    }

    /// Gets a clone of the value or inserts a default value.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up or insert
    /// * `default` - The default value to insert if key is not present
    ///
    /// # Returns
    ///
    /// A clone of the existing or newly inserted value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// assert_eq!(map.get_or_insert("key".to_string(), 42), 42);
    /// assert_eq!(map.get_or_insert("key".to_string(), 100), 42);
    /// ```
    pub fn get_or_insert(&self, key: K, default: V) -> V {
        self.inner.entry(key).or_insert(default).value().clone()
    }

    /// Gets a clone of the value or inserts a value computed by a closure.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up or insert
    /// * `f` - A closure that computes the default value
    ///
    /// # Returns
    ///
    /// A clone of the existing or newly inserted value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// let value = map.get_or_insert_with("key".to_string(), || 42);
    /// assert_eq!(value, 42);
    /// ```
    pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
    where
        F: FnOnce() -> V,
    {
        self.inner.entry(key).or_insert_with(f).value().clone()
    }

    /// Updates a value in place using a closure.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to update
    /// * `f` - A closure that takes the current value and returns the new value
    ///
    /// # Returns
    ///
    /// `true` if the key was found and updated, `false` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("counter".to_string(), 0);
    /// map.update(&"counter".to_string(), |v| v + 1);
    /// assert_eq!(map.get(&"counter".to_string()), Some(1));
    /// ```
    pub fn update<F>(&self, key: &K, f: F) -> bool
    where
        F: FnOnce(V) -> V,
    {
        if let Some(mut entry) = self.inner.get_mut(key) {
            let new_value = f(entry.value().clone());
            *entry.value_mut() = new_value;
            true
        } else {
            false
        }
    }

    /// Returns all values in the map.
    ///
    /// Note: This creates a snapshot of the values at the time of the call.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// map.insert("b".to_string(), 2);
    /// let values = map.values();
    /// assert_eq!(values.len(), 2);
    /// ```
    pub fn values(&self) -> Vec<V> {
        self.inner.iter().map(|r| r.value().clone()).collect()
    }

    /// Returns all key-value pairs in the map.
    ///
    /// Note: This creates a snapshot at the time of the call.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// let entries = map.entries();
    /// assert_eq!(entries.len(), 1);
    /// ```
    pub fn entries(&self) -> Vec<(K, V)>
    where
        K: Clone,
    {
        self.inner
            .iter()
            .map(|r| (r.key().clone(), r.value().clone()))
            .collect()
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// # Arguments
    ///
    /// * `f` - A closure that returns `true` for elements to keep
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::ConcurrentMap;
    ///
    /// let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
    /// map.insert("a".to_string(), 1);
    /// map.insert("b".to_string(), 2);
    /// map.insert("c".to_string(), 3);
    /// map.retain(|_, v| *v > 1);
    /// assert_eq!(map.len(), 2);
    /// ```
    pub fn retain<F>(&self, f: F)
    where
        F: FnMut(&K, &mut V) -> bool,
    {
        self.inner.retain(f);
    }
}

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

    #[test]
    fn test_new_and_default() {
        let map1: ConcurrentMap<String, i32> = ConcurrentMap::new();
        let map2: ConcurrentMap<String, i32> = ConcurrentMap::default();
        assert!(map1.is_empty());
        assert!(map2.is_empty());
    }

    #[test]
    fn test_with_capacity() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::with_capacity(100);
        assert!(map.is_empty());
    }

    #[test]
    fn test_insert_and_get() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        assert_eq!(map.insert("key".to_string(), 42), None);
        assert_eq!(map.get(&"key".to_string()), Some(42));
        assert_eq!(map.insert("key".to_string(), 100), Some(42));
        assert_eq!(map.get(&"key".to_string()), Some(100));
    }

    #[test]
    fn test_remove() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("key".to_string(), 42);
        assert_eq!(map.remove(&"key".to_string()), Some(42));
        assert_eq!(map.remove(&"key".to_string()), None);
        assert!(map.is_empty());
    }

    #[test]
    fn test_contains_key() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("key".to_string(), 42);
        assert!(map.contains_key(&"key".to_string()));
        assert!(!map.contains_key(&"other".to_string()));
    }

    #[test]
    fn test_len_and_is_empty() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        assert!(map.is_empty());
        assert_eq!(map.len(), 0);

        map.insert("a".to_string(), 1);
        assert!(!map.is_empty());
        assert_eq!(map.len(), 1);

        map.insert("b".to_string(), 2);
        assert_eq!(map.len(), 2);
    }

    #[test]
    fn test_clear() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("a".to_string(), 1);
        map.insert("b".to_string(), 2);
        map.clear();
        assert!(map.is_empty());
    }

    #[test]
    fn test_get_or_insert() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        assert_eq!(map.get_or_insert("key".to_string(), 42), 42);
        assert_eq!(map.get_or_insert("key".to_string(), 100), 42);
    }

    #[test]
    fn test_get_or_insert_with() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        let value = map.get_or_insert_with("key".to_string(), || 42);
        assert_eq!(value, 42);
        let value = map.get_or_insert_with("key".to_string(), || 100);
        assert_eq!(value, 42);
    }

    #[test]
    fn test_update() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("counter".to_string(), 0);
        assert!(map.update(&"counter".to_string(), |v| v + 1));
        assert_eq!(map.get(&"counter".to_string()), Some(1));
        assert!(!map.update(&"nonexistent".to_string(), |v| v + 1));
    }

    #[test]
    fn test_keys_and_values() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("a".to_string(), 1);
        map.insert("b".to_string(), 2);

        let keys = map.keys();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"a".to_string()));
        assert!(keys.contains(&"b".to_string()));

        let values = map.values();
        assert_eq!(values.len(), 2);
        assert!(values.contains(&1));
        assert!(values.contains(&2));
    }

    #[test]
    fn test_entries() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("a".to_string(), 1);
        let entries = map.entries();
        assert_eq!(entries.len(), 1);
        assert!(entries.contains(&("a".to_string(), 1)));
    }

    #[test]
    fn test_retain() {
        let map: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map.insert("a".to_string(), 1);
        map.insert("b".to_string(), 2);
        map.insert("c".to_string(), 3);
        map.retain(|_, v| *v > 1);
        assert_eq!(map.len(), 2);
        assert!(!map.contains_key(&"a".to_string()));
    }

    #[test]
    fn test_clone() {
        let map1: ConcurrentMap<String, i32> = ConcurrentMap::new();
        map1.insert("key".to_string(), 42);
        let map2 = map1.clone();
        // Both maps share the same underlying data
        assert_eq!(map2.get(&"key".to_string()), Some(42));
        map2.insert("new".to_string(), 100);
        assert_eq!(map1.get(&"new".to_string()), Some(100));
    }

    #[test]
    fn test_concurrent_access() {
        let map: Arc<ConcurrentMap<i32, i32>> = Arc::new(ConcurrentMap::new());
        let handles: Vec<_> = (0..10)
            .map(|i| {
                let map = Arc::clone(&map);
                thread::spawn(move || {
                    for j in 0..100 {
                        map.insert(i * 100 + j, j);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(map.len(), 1000);
    }

    #[test]
    fn test_concurrent_update() {
        let map: Arc<ConcurrentMap<String, i32>> = Arc::new(ConcurrentMap::new());
        map.insert("counter".to_string(), 0);

        let handles: Vec<_> = (0..10)
            .map(|_| {
                let map = Arc::clone(&map);
                thread::spawn(move || {
                    for _ in 0..100 {
                        map.update(&"counter".to_string(), |v| v + 1);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(map.get(&"counter".to_string()), Some(1000));
    }
}