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
use std::sync::Arc;
pub struct RwShare<T>(Arc<parking_lot::RwLock<T>>);
impl<T> Clone for RwShare<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> Default for RwShare<T>
where
T: Default,
{
fn default() -> Self {
Self(Default::default())
}
}
impl<T> RwShare<T> {
pub fn new(value: T) -> Self {
Self(Arc::new(parking_lot::RwLock::new(value)))
}
pub fn share_ref<R, F>(&self, f: F) -> R
where
F: FnOnce(&T) -> R,
{
let t = self
.0
.try_read_for(std::time::Duration::from_millis(100))
.or_else(|| {
tracing::info!(
"Took over 100ms to get a RwShare reader. Conductor might be over utilized"
);
self.0.try_read_for(std::time::Duration::from_secs(30))
})
.or_else(|| {
tracing::warn!("Failed to get fair reader, trying for recursive reader");
self.0
.try_read_recursive_for(std::time::Duration::from_secs(60))
})
.or_else(|| {
tracing::error!(
"Failed to get a RwShare read lock for over 120s this could be a dead lock"
);
self.0
.try_read_recursive_for(std::time::Duration::from_secs(180))
})
.expect("Failed to take a read lock for over 5 minutes this must be a deadlock");
f(&t)
}
pub fn share_mut<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
let mut t = self
.0
.try_write_for(std::time::Duration::from_millis(100))
.or_else(|| {
tracing::info!(
"Took over 100ms to get a RwShare writer. Conductor might be over utilized"
);
self.0.try_write_for(std::time::Duration::from_secs(120))
})
.or_else(|| {
tracing::error!(
"Failed to get a RwShare write lock for over 120s this could be a dead lock"
);
self.0.try_write_for(std::time::Duration::from_secs(180))
})
.expect("Failed to take a write lock for over 5 minutes this must be a deadlock");
f(&mut t)
}
}