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
//! Atomic value container using arc-swap
//!
//! Provides a concurrent-safe single value container with atomic swap semantics.
//! Ideal for configuration values or shared state that needs to be updated atomically.

use arc_swap::ArcSwap;
use std::sync::Arc;

/// A concurrent-safe single value container using arc-swap.
///
/// This container provides atomic load and store operations for a single value.
/// It's ideal for configuration values or shared state that needs to be read
/// frequently and updated occasionally.
///
/// # Type Parameters
///
/// * `T` - The value type
///
/// # Example
///
/// ```rust
/// use litellm_rs::utils::sync::AtomicValue;
///
/// let value: AtomicValue<String> = AtomicValue::new("initial".to_string());
/// assert_eq!(value.load().as_ref(), "initial");
///
/// value.store("updated".to_string());
/// assert_eq!(value.load().as_ref(), "updated");
/// ```
#[derive(Debug)]
pub struct AtomicValue<T> {
    inner: ArcSwap<T>,
}

impl<T> AtomicValue<T> {
    /// Creates a new `AtomicValue` with the specified initial value.
    ///
    /// # Arguments
    ///
    /// * `value` - The initial value
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// ```
    pub fn new(value: T) -> Self {
        Self {
            inner: ArcSwap::from_pointee(value),
        }
    }

    /// Loads the current value.
    ///
    /// Returns an `Arc` pointing to the current value. This is a very fast
    /// operation and does not block other readers or writers.
    ///
    /// # Returns
    ///
    /// An `Arc<T>` containing the current value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// let current = value.load();
    /// assert_eq!(*current, 42);
    /// ```
    pub fn load(&self) -> Arc<T> {
        self.inner.load_full()
    }

    /// Stores a new value, replacing the current one.
    ///
    /// This operation is atomic and will not block readers.
    ///
    /// # Arguments
    ///
    /// * `value` - The new value to store
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// value.store(100);
    /// assert_eq!(*value.load(), 100);
    /// ```
    pub fn store(&self, value: T) {
        self.inner.store(Arc::new(value));
    }

    /// Swaps the current value with a new one, returning the old value.
    ///
    /// # Arguments
    ///
    /// * `value` - The new value to store
    ///
    /// # Returns
    ///
    /// An `Arc<T>` containing the previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// let old = value.swap(100);
    /// assert_eq!(*old, 42);
    /// assert_eq!(*value.load(), 100);
    /// ```
    pub fn swap(&self, value: T) -> Arc<T> {
        self.inner.swap(Arc::new(value))
    }

    /// Stores a new value from an Arc.
    ///
    /// This is useful when you already have an Arc and want to avoid
    /// an extra allocation.
    ///
    /// # Arguments
    ///
    /// * `value` - An Arc containing the new value
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    /// use std::sync::Arc;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// value.store_arc(Arc::new(100));
    /// assert_eq!(*value.load(), 100);
    /// ```
    pub fn store_arc(&self, value: Arc<T>) {
        self.inner.store(value);
    }

    /// Swaps the current value with a new Arc, returning the old value.
    ///
    /// # Arguments
    ///
    /// * `value` - An Arc containing the new value
    ///
    /// # Returns
    ///
    /// An `Arc<T>` containing the previous value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    /// use std::sync::Arc;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// let old = value.swap_arc(Arc::new(100));
    /// assert_eq!(*old, 42);
    /// ```
    pub fn swap_arc(&self, value: Arc<T>) -> Arc<T> {
        self.inner.swap(value)
    }
}

impl<T: Clone> AtomicValue<T> {
    /// Updates the value using a closure, with atomic compare-and-swap retry.
    ///
    /// This operation uses `ArcSwap::rcu()` to perform a read-copy-update loop,
    /// retrying until the compare-and-swap succeeds. The closure may be called
    /// more than once if concurrent updates occur, so it must be side-effect-free
    /// (i.e., `Fn` rather than `FnOnce`).
    ///
    /// # Arguments
    ///
    /// * `f` - A closure that takes a reference to the current value and returns the new value
    ///
    /// # Returns
    ///
    /// The new value after the update.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<i32> = AtomicValue::new(42);
    /// let new_value = value.update(|v| v + 1);
    /// assert_eq!(new_value, 43);
    /// ```
    pub fn update<F>(&self, f: F) -> T
    where
        F: Fn(&T) -> T,
    {
        use std::cell::Cell;
        // `rcu` retries the closure until a compare-and-swap succeeds, so the
        // closure may be called more than once.  The last invocation is always
        // the one whose result was actually stored, so we capture that Arc and
        // return a clone of it.
        let last_new: Cell<Option<Arc<T>>> = Cell::new(None);
        self.inner.rcu(|current| {
            let new_arc = Arc::new(f(current.as_ref()));
            last_new.set(Some(Arc::clone(&new_arc)));
            new_arc
        });
        match last_new.into_inner() {
            Some(arc) => (*arc).clone(),
            None => unreachable!("rcu always invokes the closure at least once"),
        }
    }

    /// Gets a clone of the current value.
    ///
    /// This is a convenience method that loads the value and clones it.
    ///
    /// # Returns
    ///
    /// A clone of the current value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use litellm_rs::utils::sync::AtomicValue;
    ///
    /// let value: AtomicValue<String> = AtomicValue::new("hello".to_string());
    /// let cloned: String = value.get();
    /// assert_eq!(cloned, "hello");
    /// ```
    pub fn get(&self) -> T {
        (*self.load()).clone()
    }
}

impl<T: Default> Default for AtomicValue<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> Clone for AtomicValue<T> {
    /// Creates a new `AtomicValue` pointing to the same underlying data.
    ///
    /// Note: This creates a new `AtomicValue` that shares the same `Arc`,
    /// but updates to one will not affect the other after the clone.
    fn clone(&self) -> Self {
        Self {
            inner: ArcSwap::new(self.inner.load_full()),
        }
    }
}

impl<T> From<T> for AtomicValue<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl<T> From<Arc<T>> for AtomicValue<T> {
    fn from(value: Arc<T>) -> Self {
        Self {
            inner: ArcSwap::new(value),
        }
    }
}

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

    #[test]
    fn test_new_and_load() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        assert_eq!(*value.load(), 42);
    }

    #[test]
    fn test_store() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        value.store(100);
        assert_eq!(*value.load(), 100);
    }

    #[test]
    fn test_swap() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        let old = value.swap(100);
        assert_eq!(*old, 42);
        assert_eq!(*value.load(), 100);
    }

    #[test]
    fn test_store_arc() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        value.store_arc(Arc::new(100));
        assert_eq!(*value.load(), 100);
    }

    #[test]
    fn test_swap_arc() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        let old = value.swap_arc(Arc::new(100));
        assert_eq!(*old, 42);
        assert_eq!(*value.load(), 100);
    }

    #[test]
    fn test_update() {
        let value: AtomicValue<i32> = AtomicValue::new(42);
        let new_value = value.update(|v| v + 1);
        assert_eq!(new_value, 43);
        assert_eq!(*value.load(), 43);
    }

    #[test]
    fn test_get() {
        let value: AtomicValue<String> = AtomicValue::new("hello".to_string());
        let cloned: String = value.get();
        assert_eq!(cloned, "hello");
    }

    #[test]
    fn test_default() {
        let value: AtomicValue<i32> = AtomicValue::default();
        assert_eq!(*value.load(), 0);

        let value: AtomicValue<String> = AtomicValue::default();
        assert_eq!(*value.load(), "");
    }

    #[test]
    fn test_clone() {
        let value1: AtomicValue<i32> = AtomicValue::new(42);
        let value2 = value1.clone();

        // Both start with the same value
        assert_eq!(*value1.load(), 42);
        assert_eq!(*value2.load(), 42);

        // Updates to one don't affect the other
        value1.store(100);
        assert_eq!(*value1.load(), 100);
        assert_eq!(*value2.load(), 42);
    }

    #[test]
    fn test_from_value() {
        let value: AtomicValue<i32> = AtomicValue::from(42);
        assert_eq!(*value.load(), 42);
    }

    #[test]
    fn test_from_arc() {
        let arc = Arc::new(42);
        let value: AtomicValue<i32> = AtomicValue::from(arc);
        assert_eq!(*value.load(), 42);
    }

    #[test]
    fn test_with_string() {
        let value: AtomicValue<String> = AtomicValue::new("initial".to_string());
        assert_eq!(value.load().as_ref(), "initial");

        value.store("updated".to_string());
        assert_eq!(value.load().as_ref(), "updated");
    }

    #[test]
    fn test_with_struct() {
        #[derive(Debug, Clone, PartialEq)]
        struct Config {
            host: String,
            port: u16,
        }

        let config = Config {
            host: "localhost".to_string(),
            port: 8080,
        };

        let value: AtomicValue<Config> = AtomicValue::new(config);
        assert_eq!(value.load().host, "localhost");
        assert_eq!(value.load().port, 8080);

        value.store(Config {
            host: "0.0.0.0".to_string(),
            port: 9090,
        });
        assert_eq!(value.load().host, "0.0.0.0");
        assert_eq!(value.load().port, 9090);
    }

    #[test]
    fn test_concurrent_reads() {
        let value: Arc<AtomicValue<i32>> = Arc::new(AtomicValue::new(42));
        let handles: Vec<_> = (0..10)
            .map(|_| {
                let value = Arc::clone(&value);
                thread::spawn(move || {
                    for _ in 0..1000 {
                        let _ = value.load();
                    }
                })
            })
            .collect();

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

        assert_eq!(*value.load(), 42);
    }

    #[test]
    fn test_concurrent_writes() {
        let value: Arc<AtomicValue<i32>> = Arc::new(AtomicValue::new(0));
        let handles: Vec<_> = (0..10)
            .map(|i| {
                let value = Arc::clone(&value);
                thread::spawn(move || {
                    for _ in 0..100 {
                        value.store(i);
                    }
                })
            })
            .collect();

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

        // Value should be one of the written values (0-9)
        let final_value = *value.load();
        assert!((0..10).contains(&final_value));
    }

    #[test]
    fn test_concurrent_updates() {
        let value: Arc<AtomicValue<i32>> = Arc::new(AtomicValue::new(0));
        let handles: Vec<_> = (0..10)
            .map(|_| {
                let value = Arc::clone(&value);
                thread::spawn(move || {
                    for _ in 0..100 {
                        value.update(|v| v + 1);
                    }
                })
            })
            .collect();

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

        // update() uses rcu() for atomic compare-and-swap retry, so all 1000
        // increments must be visible — no updates are lost under concurrency.
        let final_value = *value.load();
        assert_eq!(final_value, 1000);
    }
}