-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathhandle.rs
More file actions
169 lines (144 loc) · 4.76 KB
/
handle.rs
File metadata and controls
169 lines (144 loc) · 4.76 KB
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
use rivetkit_client::{self as rivetkit_rs};
use futures_util::FutureExt;
use pyo3::{prelude::*, types::{PyList, PyString, PyTuple}};
use tokio::sync::mpsc;
use crate::util::{self, SYNC_RUNTIME};
const EVENT_BUFFER_SIZE: usize = 100;
struct ActorEvent {
name: String,
args: Vec<serde_json::Value>,
}
#[pyclass]
pub struct ActorHandle {
handle: rivetkit_rs::connection::ActorHandle,
event_rx: Option<mpsc::Receiver<ActorEvent>>,
event_tx: mpsc::Sender<ActorEvent>,
}
impl ActorHandle {
pub fn new(handle: rivetkit_rs::connection::ActorHandle) -> Self {
let (event_tx, event_rx) = mpsc::channel(EVENT_BUFFER_SIZE);
Self {
handle,
event_tx,
event_rx: Some(event_rx),
}
}
}
#[pymethods]
impl ActorHandle {
#[new]
pub fn py_new() -> PyResult<Self> {
Err(py_runtime_err!("Actor handle cannot be instantiated directly"))
}
#[pyo3(signature=(method, *py_args))]
pub fn action<'a>(
&self,
py: Python<'a>,
method: &str,
py_args: &Bound<'_, PyTuple>,
) -> PyResult<Bound<'a, PyAny>> {
let args = py_args.extract::<Vec<PyObject>>()?;
let result = self.handle.action(
method,
util::py_to_json_value(py, &args)?
);
let result = SYNC_RUNTIME.block_on(result);
let Ok(result) = result else {
return Err(py_runtime_err!(
"Failed to call action: {:?}",
result.err()
));
};
let mut result = util::json_to_py_value(py, &vec![result])?;
let Some(result) = result.drain(0..1).next() else {
return Err(py_runtime_err!(
"Expected one result, got {}",
result.len()
));
};
Ok(result)
}
pub fn subscribe(
&self,
event_name: &str,
) -> PyResult<()> {
let event_name = event_name.to_string();
let tx = self.event_tx.clone();
SYNC_RUNTIME.block_on(
self.handle.on_event(&event_name.clone(), move |args| {
let event_name = event_name.clone();
let args = args.clone();
let tx = tx.clone();
tokio::spawn(async move {
let event = ActorEvent {
name: event_name,
args: args.clone(),
};
// Send this upstream(?)
tx.send(event).await.map_err(|e| {
py_runtime_err!(
"Failed to send via inner tx: {}",
e
)
}).ok();
});
})
);
Ok(())
}
#[pyo3(signature=(count, timeout=None))]
pub fn receive<'a>(
&mut self,
py: Python<'a>,
count: u32,
timeout: Option<f64>
) -> PyResult<Bound<'a, PyList>> {
let mut rx = self.event_rx.take().ok_or_else(|| {
py_runtime_err!("Two .receive() calls cannot co-exist")
})?;
let result: Vec<ActorEvent> = SYNC_RUNTIME.block_on(async {
let mut events: Vec<ActorEvent> = Vec::new();
loop {
if events.len() >= count as usize {
break;
}
let timeout_rx_future = match timeout {
Some(timeout) => {
let timeout = std::time::Duration::from_secs_f64(timeout);
tokio::time::timeout(timeout, rx.recv())
.map(|x| x.unwrap_or(None)).boxed()
},
None => rx.recv().boxed()
};
tokio::select! {
result = timeout_rx_future => {
match result {
Some(event) => events.push(event),
None => break,
}
},
// TODO: Add more signal support
_ = tokio::signal::ctrl_c() => {
py.check_signals()?;
}
};
}
Ok::<_, PyErr>(events)
})?;
// Convert events to Python objects
let py_events = PyList::empty(py);
for event in result {
let event = PyTuple::new(py, &[
PyString::new(py, &event.name).as_any(),
PyList::new(py, &util::json_to_py_value(py, &event.args)?)?.as_any(),
])?;
py_events.append(event)?;
}
Ok(py_events)
}
pub fn disconnect(&self) {
SYNC_RUNTIME.block_on(
self.handle.disconnect()
)
}
}