llm_agent_runtime/
util.rs1pub fn recover_lock<'a, T>(
8 result: std::sync::LockResult<std::sync::MutexGuard<'a, T>>,
9 ctx: &str,
10) -> std::sync::MutexGuard<'a, T>
11where
12 T: ?Sized,
13{
14 match result {
15 Ok(guard) => guard,
16 Err(poisoned) => {
17 tracing::warn!("mutex poisoned in {ctx}, recovering inner value");
18 poisoned.into_inner()
19 }
20 }
21}
22
23pub fn timed_lock<'a, T>(mutex: &'a std::sync::Mutex<T>, ctx: &str) -> std::sync::MutexGuard<'a, T>
28where
29 T: ?Sized,
30{
31 let start = std::time::Instant::now();
32 let result = mutex.lock();
33 let elapsed = start.elapsed();
34 if elapsed > std::time::Duration::from_millis(5) {
35 tracing::warn!(
36 duration_ms = elapsed.as_millis(),
37 ctx = ctx,
38 "slow mutex acquisition"
39 );
40 }
41 match result {
42 Ok(guard) => guard,
43 Err(poisoned) => {
44 tracing::warn!("mutex poisoned in {ctx}, recovering inner value");
45 poisoned.into_inner()
46 }
47 }
48}
49
50pub fn djb2(s: &str) -> u64 {
53 let mut h: u64 = 5381;
54 for b in s.bytes() {
55 h = h.wrapping_mul(33).wrapping_add(b as u64);
56 }
57 h
58}