litellm-rs 0.9.0

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
//! Single-key Lua deployment circuit state (cluster-safe: `KEYS[1]` only).

use super::pool::{RedisLiveConnection, RedisPool};
use crate::utils::error::gateway_error::{GatewayError, Result};
use std::collections::HashMap;
use std::sync::{Arc, OnceLock};
use std::time::Duration;

const CIRCUIT_OPERATION_TIMEOUT: Duration = Duration::from_secs(5);

const CIRCUIT_SCRIPT: &str = r#"
local op = ARGV[1]
local now = tonumber(ARGV[2]) or 0
local epoch = tonumber(ARGV[3]) or 0
local token = ARGV[4]
local allowed = tonumber(ARGV[5]) or 3
local min_req = tonumber(ARGV[6]) or 10
local cooldown = tonumber(ARGV[7]) or 5
local success_th = tonumber(ARGV[8]) or 3
local reason = tonumber(ARGV[9]) or 0

local function load_state()
  local f = tonumber(redis.call('HGET', KEYS[1], 'f') or '0') or 0
  local r = tonumber(redis.call('HGET', KEYS[1], 'r') or '0') or 0
  local tot = tonumber(redis.call('HGET', KEYS[1], 'tot') or '0') or 0
  local fail = tonumber(redis.call('HGET', KEYS[1], 'fail') or '0') or 0
  local opened = tonumber(redis.call('HGET', KEYS[1], 'opened') or '0') or 0
  local consec = tonumber(redis.call('HGET', KEYS[1], 'consec') or '0') or 0
  local e = tonumber(redis.call('HGET', KEYS[1], 'e') or '0') or 0
  local h = tonumber(redis.call('HGET', KEYS[1], 'h') or '1') or 1
  local owner = redis.call('HGET', KEYS[1], 'owner')
  if not owner then owner = '' end
  local own_until = tonumber(redis.call('HGET', KEYS[1], 'own_until') or '0') or 0
  return f, r, tot, fail, opened, consec, e, h, owner, own_until
end

local function save(f, r, tot, fail, opened, consec, e, h, owner, own_until)
  redis.call('HSET', KEYS[1], 'f', f, 'r', r, 'tot', tot, 'fail', fail,
    'opened', opened, 'consec', consec, 'e', e, 'h', h, 'owner', owner,
    'own_until', own_until)
end

local f, r, tot, fail, opened, consec, e, h, owner, own_until = load_state()
local function claim_owner()
  if owner == '' or own_until <= now then
    consec = 0
    owner = token
    own_until = now + cooldown
    if own_until < now + 1 then own_until = now + 1 end
    h = 2
  end
end
local function clear_owner()
  owner = ''
  own_until = 0
end
if e ~= epoch then
  f = 0
  r = 0
  e = epoch
end

if op == 'fail' then
  tot = tot + 1
  fail = fail + 1
  f = f + 1
  consec = 0
  if opened > now then
    h = 4
  elseif opened > 0 then
    opened = now + cooldown
    clear_owner()
    h = 4
  else
    local trip = 0
    if reason == 1 then
      trip = 1
    elseif reason == 2 then
      if tot >= min_req and (fail * 100 / tot) > 50 then trip = 1 end
    elseif f >= allowed and (r + f) >= min_req then
      trip = 1
    end
    if trip == 1 then
      opened = now + cooldown
      clear_owner()
      h = 4
    elseif h == 1 or h == 0 then
      h = 2
    end
  end
elseif op == 'ok' then
  tot = tot + 1
  r = r + 1
  if opened == 0 or (owner == token and own_until > now) then
    consec = consec + 1
  end
  if opened > now then
    h = 4
  elseif opened > 0 then
    if owner == token and own_until > now then
      if consec >= success_th then
        opened = 0
        clear_owner()
        h = 1
      else
        claim_owner()
      end
    end
  elseif h == 2 and consec >= success_th then
    h = 1
  end
end

local status = 0
local owned = 1
if opened > now then
  status = 1
  owned = 0
elseif opened > 0 then
  status = 2
  claim_owner()
  if owner == token then owned = 1 else owned = 0 end
end

if op ~= 'observe' or opened > 0 then
  save(f, r, tot, fail, opened, consec, e, h, owner, own_until)
end
-- Refresh active history without changing cooldown or probe-owner deadlines.
redis.call('EXPIRE', KEYS[1], math.max(600, opened - now + 1, own_until - now + 1))
return {status, opened, f, consec, h, owned}
"#;

const STATUS_OPEN: i64 = 1;
const STATUS_HALF: i64 = 2;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct CircuitState {
    pub status: i64,
    pub opened_until: i64,
    pub fails: i64,
    pub consecutive_successes: i64,
    pub health: i64,
    pub owned: bool,
}

impl CircuitState {
    pub(crate) fn blocks_selection(self) -> bool {
        self.status == STATUS_OPEN || (self.status == STATUS_HALF && !self.owned)
    }
}

pub(crate) struct CircuitArgs<'a> {
    pub op: &'a str,
    pub now_secs: i64,
    pub window_epoch: i64,
    pub token: &'a str,
    pub allowed_fails: i64,
    pub min_requests: i64,
    pub cooldown_secs: i64,
    pub success_threshold: i64,
    pub reason: i64,
}

fn circuit_script() -> &'static redis::Script {
    static SCRIPT: OnceLock<redis::Script> = OnceLock::new();
    SCRIPT.get_or_init(|| redis::Script::new(CIRCUIT_SCRIPT))
}

fn circuit_runtime_connections()
-> &'static parking_lot::Mutex<HashMap<String, Arc<RedisLiveConnection>>> {
    static CACHE: OnceLock<parking_lot::Mutex<HashMap<String, Arc<RedisLiveConnection>>>> =
        OnceLock::new();
    CACHE.get_or_init(|| parking_lot::Mutex::new(HashMap::new()))
}

async fn connection_on_current_runtime(pool: &RedisPool) -> Result<Arc<RedisLiveConnection>> {
    let cache_key = format!("{}|{}", pool.config.url, pool.config.cluster);
    {
        let cache = circuit_runtime_connections().lock();
        if let Some(conn) = cache.get(&cache_key) {
            return Ok(conn.clone());
        }
    }
    // Reuse the bounded, non-replaying transport for uncertain routing writes.
    let conn = Arc::new(
        pool.open_budget_connection(CIRCUIT_OPERATION_TIMEOUT)
            .await?,
    );
    let mut cache = circuit_runtime_connections().lock();
    Ok(cache.entry(cache_key).or_insert(conn).clone())
}

fn parse_circuit_state(values: Vec<i64>) -> Result<CircuitState> {
    if values.len() != 6 {
        return Err(GatewayError::Storage(format!(
            "Unexpected Redis circuit result length: {}",
            values.len()
        )));
    }
    Ok(CircuitState {
        status: values[0],
        opened_until: values[1].max(0),
        fails: values[2].max(0),
        consecutive_successes: values[3].max(0),
        health: values[4],
        owned: values[5] == 1,
    })
}

impl RedisPool {
    pub(crate) fn circuit_key(deployment_id: &str) -> String {
        format!("litellm-rs:circuit:v1:{deployment_id}")
    }

    pub(crate) async fn circuit_invoke(
        &self,
        key: &str,
        args: CircuitArgs<'_>,
    ) -> Result<CircuitState> {
        if self.noop_mode {
            return Err(GatewayError::Storage(
                "circuit redis backend is unavailable".to_string(),
            ));
        }

        let mut connection = None;
        let result =
            tokio::time::timeout(CIRCUIT_OPERATION_TIMEOUT, async {
                let _permit =
                    self.semaphore.clone().acquire_owned().await.map_err(|_| {
                        GatewayError::Internal("Redis semaphore closed".to_string())
                    })?;
                let cached = connection_on_current_runtime(self).await?;
                connection = Some(Arc::clone(&cached));
                let mut conn = cached.as_ref().clone();

                let values: Vec<i64> = circuit_script()
                    .key(key)
                    .arg(args.op)
                    .arg(args.now_secs)
                    .arg(args.window_epoch)
                    .arg(args.token)
                    .arg(args.allowed_fails)
                    .arg(args.min_requests)
                    .arg(args.cooldown_secs)
                    .arg(args.success_threshold)
                    .arg(args.reason)
                    .invoke_async(&mut conn)
                    .await
                    .map_err(GatewayError::from)?;
                parse_circuit_state(values)
            })
            .await
            .unwrap_or_else(|_| {
                Err(GatewayError::Unavailable(
                    "Redis circuit operation deadline exceeded".into(),
                ))
            });
        if result.is_err()
            && let Some(connection) = connection
        {
            let key = format!("{}|{}", self.config.url, self.config.cluster);
            let mut cache = circuit_runtime_connections().lock();
            // A late failure from an old connection must preserve a replacement.
            if cache
                .get(&key)
                .is_some_and(|current| Arc::ptr_eq(current, &connection))
            {
                cache.remove(&key);
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::models::storage::RedisConfig;

    #[tokio::test]
    async fn expired_probe_owner_cannot_supply_recovery_successes() {
        let Ok(url) = std::env::var("REDIS_URL") else {
            assert!(std::env::var("CI").is_err(), "REDIS_URL is required in CI");
            return;
        };
        let pool = RedisPool::new(&RedisConfig {
            url,
            enabled: true,
            allow_degraded: false,
            ..RedisConfig::default()
        })
        .await
        .unwrap();
        let key = RedisPool::circuit_key(&uuid::Uuid::new_v4().to_string());
        async fn invoke(
            pool: &RedisPool,
            key: &str,
            op: &str,
            now: i64,
            token: &str,
            cooldown: i64,
        ) -> CircuitState {
            // This test owns a short-lived runtime; do not populate the
            // production bridge's process-lifetime connection cache.
            let mut conn = pool.open_live_connection().await.unwrap();
            let values = circuit_script()
                .key(key)
                .arg(op)
                .arg(now)
                .arg(0)
                .arg(token)
                .arg(1)
                .arg(1)
                .arg(cooldown)
                .arg(2)
                .arg(1)
                .invoke_async(&mut conn)
                .await
                .unwrap();
            parse_circuit_state(values).unwrap()
        }
        assert!(
            invoke(&pool, &key, "fail", 100, "a", 10)
                .await
                .blocks_selection()
        );
        assert!(
            !invoke(&pool, &key, "observe", 110, "a", 10)
                .await
                .blocks_selection()
        );
        assert!(
            invoke(&pool, &key, "observe", 119, "b", 10)
                .await
                .blocks_selection()
        );
        assert!(
            !invoke(&pool, &key, "observe", 120, "b", 10)
                .await
                .blocks_selection()
        );
        invoke(&pool, &key, "ok", 121, "a", 10).await;
        let first = invoke(&pool, &key, "ok", 122, "b", 10).await;
        assert_eq!(
            first.status, STATUS_HALF,
            "expired owner's late success must not close the circuit"
        );
        assert_eq!(first.consecutive_successes, 1);
        assert!(
            invoke(&pool, &key, "observe", 122, "a", 10)
                .await
                .blocks_selection()
        );
        assert_eq!(invoke(&pool, &key, "ok", 123, "b", 10).await.status, 0);
        let mut conn = pool.open_live_connection().await.unwrap();
        let history: (i64, i64, i64) = redis::cmd("HMGET")
            .arg(&key)
            .arg(&["tot", "fail", "opened"])
            .query_async(&mut conn)
            .await
            .unwrap();
        assert_eq!(history, (4, 1, 0));
        redis::cmd("PEXPIRE")
            .arg(&key)
            .arg(60_000)
            .query_async::<i64>(&mut conn)
            .await
            .unwrap();
        invoke(&pool, &key, "observe", 123, "a", 10).await;
        let ttl: i64 = redis::cmd("TTL")
            .arg(&key)
            .query_async(&mut conn)
            .await
            .unwrap();
        assert!(
            (570..=600).contains(&ttl),
            "closed observe must refresh idle lifetime: {ttl}"
        );
        let retained: (i64, i64, i64) = redis::cmd("HMGET")
            .arg(&key)
            .arg(&["tot", "fail", "opened"])
            .query_async(&mut conn)
            .await
            .unwrap();
        assert_eq!(
            retained, history,
            "active observations must retain cumulative history"
        );
        pool.delete(&key).await.unwrap();

        let key = RedisPool::circuit_key(&uuid::Uuid::new_v4().to_string());
        let opened = invoke(&pool, &key, "fail", 100, "a", 900).await;
        assert_eq!(opened.opened_until, 1_000);
        let ttl: i64 = redis::cmd("TTL")
            .arg(&key)
            .query_async(&mut conn)
            .await
            .unwrap();
        assert!(
            (871..=901).contains(&ttl),
            "long cooldown must outlive idle TTL: {ttl}"
        );
        let observed = invoke(&pool, &key, "observe", 200, "b", 900).await;
        assert_eq!(
            observed.opened_until, 1_000,
            "observing must not extend the open deadline"
        );
        let ttl: i64 = redis::cmd("TTL")
            .arg(&key)
            .query_async(&mut conn)
            .await
            .unwrap();
        assert!((771..=801).contains(&ttl));
        let probe = invoke(&pool, &key, "observe", 1_000, "a", 900).await;
        assert_eq!(probe.status, STATUS_HALF);
        assert!(probe.owned);
        let ttl: i64 = redis::cmd("TTL")
            .arg(&key)
            .query_async(&mut conn)
            .await
            .unwrap();
        assert!(
            (871..=901).contains(&ttl),
            "live probe owner must outlive idle TTL: {ttl}"
        );
        let foreign = invoke(&pool, &key, "observe", 1_001, "b", 900).await;
        assert!(foreign.blocks_selection());
        let deadlines: (i64, i64) = redis::cmd("HMGET")
            .arg(&key)
            .arg(&["opened", "own_until"])
            .query_async(&mut conn)
            .await
            .unwrap();
        assert_eq!(deadlines, (1_000, 1_900));
        pool.delete(&key).await.unwrap();

        // A closed observation of an unused namespace must not create a hash.
        invoke(&pool, &key, "observe", 2_000, "a", 900).await;
        let exists: bool = redis::cmd("EXISTS")
            .arg(&key)
            .query_async(&mut conn)
            .await
            .unwrap();
        assert!(!exists);
    }

    #[test]
    fn parses_circuit_state() {
        let open = parse_circuit_state(vec![1, 10, 3, 0, 4, 0]).unwrap();
        assert!(open.blocks_selection());
        assert_eq!(open.opened_until, 10);

        let half_owned = parse_circuit_state(vec![2, 5, 3, 1, 2, 1]).unwrap();
        assert!(!half_owned.blocks_selection());
        assert!(half_owned.owned);

        let half_foreign = parse_circuit_state(vec![2, 5, 3, 0, 2, 0]).unwrap();
        assert!(half_foreign.blocks_selection());
        assert!(parse_circuit_state(vec![1, 0]).is_err());
    }

    #[tokio::test]
    async fn noop_redis_pool_fails_closed_on_circuit_observe() {
        let pool = RedisPool::new(&RedisConfig {
            enabled: false,
            ..RedisConfig::default()
        })
        .await
        .expect("disabled Redis should create a no-op pool");

        let err = pool
            .circuit_invoke(
                "litellm-rs:circuit:v1:noop-test",
                CircuitArgs {
                    op: "observe",
                    now_secs: 1,
                    window_epoch: 0,
                    token: "tok",
                    allowed_fails: 3,
                    min_requests: 1,
                    cooldown_secs: 5,
                    success_threshold: 1,
                    reason: 0,
                },
            )
            .await
            .expect_err("no-op Redis must not fail-open circuit observe");
        assert!(err.to_string().contains("unavailable"));
    }
}