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
//! Event bus for sandbox lifecycle events.
//!
//! Produces `SandboxEvent` structs via a `tokio::broadcast` channel.
//! Consumers: webhook dispatcher, SSE endpoint, OTel span emitter.
use chrono::{DateTime, Utc};
use http_body_util::BodyExt;
use hyper::body::Incoming;
use hyper::{Request, Response, StatusCode};
use serde::Serialize;
use std::collections::HashMap;
use std::time::Duration;
use tokio::sync::broadcast;
use crate::http_api::BoxBody;
/// A sandbox lifecycle event.
#[derive(Debug, Clone, Serialize)]
pub struct SandboxEvent {
/// Event type, e.g. "sandbox.created", "sandbox.exec.completed".
pub event: String,
/// When the event occurred.
pub timestamp: DateTime<Utc>,
/// Sandbox name.
pub sandbox: String,
/// Sandbox labels at the time of the event.
pub labels: HashMap<String, String>,
/// Arbitrary metadata (exit_code, duration_ms, backend, image, etc.).
pub metadata: serde_json::Value,
}
/// Broadcast sender half – cloneable, cheap.
pub type EventBus = broadcast::Sender<SandboxEvent>;
/// Create a new event bus with a reasonable buffer.
pub fn new_event_bus() -> EventBus {
broadcast::channel(256).0
}
// ---------------------------------------------------------------------------
// Webhook dispatcher
// ---------------------------------------------------------------------------
/// Background task that forwards events to webhook URLs with retry.
///
/// Uses a bounded semaphore to limit concurrent in-flight deliveries.
pub async fn webhook_dispatcher(mut rx: broadcast::Receiver<SandboxEvent>, urls: Vec<String>) {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.build()
.unwrap_or_default();
// Limit concurrent webhook deliveries to avoid unbounded task growth
let semaphore = std::sync::Arc::new(tokio::sync::Semaphore::new(64));
loop {
match rx.recv().await {
Ok(event) => {
for url in &urls {
let client = client.clone();
let url = url.clone();
let payload = event.clone();
let permit = semaphore.clone();
tokio::spawn(async move {
let _permit = permit.acquire().await;
for attempt in 0..3u32 {
match client.post(&url).json(&payload).send().await {
Ok(resp) if resp.status().is_success() => break,
Ok(resp) => {
eprintln!(
"[webhook] POST {} returned {} (attempt {})",
url,
resp.status(),
attempt + 1
);
}
Err(e) => {
eprintln!(
"[webhook] POST {} failed: {} (attempt {})",
url,
e,
attempt + 1
);
}
}
tokio::time::sleep(Duration::from_millis(100 * 2u64.pow(attempt)))
.await;
}
});
}
}
Err(broadcast::error::RecvError::Lagged(skipped)) => {
eprintln!(
"[webhook] receiver lagged, skipped {} event(s); continuing",
skipped
);
}
Err(broadcast::error::RecvError::Closed) => break,
}
}
}
// ---------------------------------------------------------------------------
// SSE endpoint: GET /events
// ---------------------------------------------------------------------------
fn full_body<T: Into<bytes::Bytes>>(chunk: T) -> BoxBody {
http_body_util::Full::new(chunk.into())
.map_err(|never| match never {})
.boxed()
}
/// Handle `GET /events` — streams sandbox lifecycle events as SSE.
///
/// Supports optional `?sandbox=<name>` query filter.
pub async fn handle_events_sse(req: &Request<Incoming>, event_bus: &EventBus) -> Response<BoxBody> {
let query = req.uri().query().unwrap_or("");
let sandbox_filter: Option<String> = query
.split('&')
.filter_map(|pair| {
let mut kv = pair.splitn(2, '=');
match (kv.next(), kv.next()) {
(Some("sandbox"), Some(v)) => Some(urlencoding::decode(v).ok()?.into_owned()),
_ => None,
}
})
.next();
let mut rx = event_bus.subscribe();
// Collect events for a short window then flush (non-streaming SSE for hyper 1.x).
// For a long-lived SSE stream we'd need http-body channels; for now we collect
// for up to 30s or 100 events, whichever comes first.
let mut body = String::new();
let deadline = tokio::time::Instant::now() + Duration::from_secs(30);
let mut count = 0usize;
// Send an initial comment so the client knows the stream is alive.
body.push_str(": connected\n\n");
loop {
if count >= 100 {
break;
}
let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
if remaining.is_zero() {
break;
}
match tokio::time::timeout(remaining, rx.recv()).await {
Ok(Ok(event)) => {
if let Some(ref filter) = sandbox_filter
&& event.sandbox != *filter
{
continue;
}
let data = serde_json::to_string(&event).unwrap_or_else(|_| "{}".to_string());
body.push_str(&format!("event: {}\ndata: {}\n\n", event.event, data));
count += 1;
}
Ok(Err(broadcast::error::RecvError::Lagged(n))) => {
body.push_str(&format!(": lagged {n} events\n\n"));
}
Ok(Err(_)) => break, // channel closed
Err(_) => break, // timeout
}
}
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "text/event-stream")
.header("Cache-Control", "no-cache")
.header("Connection", "keep-alive")
.body(full_body(body))
.unwrap()
}
// ---------------------------------------------------------------------------
// Helper: emit an event to the bus (no-op if bus is None)
// ---------------------------------------------------------------------------
/// Fire-and-forget: send an event to the bus. Silently drops if no subscribers.
pub fn emit(bus: Option<&EventBus>, event: SandboxEvent) {
if let Some(bus) = bus {
let _ = bus.send(event);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sandbox_event_serialization() {
let event = SandboxEvent {
event: "sandbox.created".to_string(),
timestamp: Utc::now(),
sandbox: "test-sandbox".to_string(),
labels: HashMap::new(),
metadata: serde_json::json!({"image": "alpine:3.20"}),
};
let json = serde_json::to_string(&event).unwrap();
assert!(json.contains("sandbox.created"));
assert!(json.contains("test-sandbox"));
}
#[test]
fn test_event_bus_send_recv() {
let bus = new_event_bus();
let mut rx = bus.subscribe();
let event = SandboxEvent {
event: "sandbox.created".to_string(),
timestamp: Utc::now(),
sandbox: "test".to_string(),
labels: HashMap::new(),
metadata: serde_json::json!({}),
};
bus.send(event.clone()).unwrap();
let received = rx.try_recv().unwrap();
assert_eq!(received.event, "sandbox.created");
assert_eq!(received.sandbox, "test");
}
#[test]
fn test_emit_none_bus() {
// Should not panic
emit(
None,
SandboxEvent {
event: "sandbox.created".to_string(),
timestamp: Utc::now(),
sandbox: "test".to_string(),
labels: HashMap::new(),
metadata: serde_json::json!({}),
},
);
}
}