Skip to content

Commit d6b4d75

Browse files
committed
Make the WebSocket timeouts configurable
1 parent c79a2c4 commit d6b4d75

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

ui/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ use std::{
88
net::SocketAddr,
99
path::{Path, PathBuf},
1010
sync::Arc,
11+
time::Duration,
1112
};
1213
use tracing::{error, info, warn};
1314
use tracing_subscriber::EnvFilter;
1415

1516
const DEFAULT_ADDRESS: &str = "127.0.0.1";
1617
const DEFAULT_PORT: u16 = 5000;
18+
19+
const DEFAULT_WEBSOCKET_IDLE_TIMEOUT: Duration = Duration::from_secs(60);
20+
const DEFAULT_WEBSOCKET_SESSION_TIMEOUT: Duration = Duration::from_secs(45 * 60);
21+
1722
const DEFAULT_COORDINATORS_LIMIT: usize = 25;
1823
const DEFAULT_PROCESSES_LIMIT: usize = 10;
1924

@@ -50,6 +55,7 @@ struct Config {
5055
metrics_token: Option<String>,
5156
feature_flags: FeatureFlags,
5257
request_db_path: Option<PathBuf>,
58+
websocket_config: WebSocketConfig,
5359
limits: Arc<dyn ResourceLimits>,
5460
port: u16,
5561
root: PathBuf,
@@ -108,6 +114,23 @@ impl Config {
108114

109115
let request_db_path = env::var_os("PLAYGROUND_REQUEST_DATABASE").map(Into::into);
110116

117+
let websocket_config = {
118+
let idle_timeout = env::var("PLAYGROUND_WEBSOCKET_IDLE_TIMEOUT_S")
119+
.ok()
120+
.and_then(|l| l.parse().map(Duration::from_secs).ok())
121+
.unwrap_or(DEFAULT_WEBSOCKET_IDLE_TIMEOUT);
122+
123+
let session_timeout = env::var("PLAYGROUND_WEBSOCKET_SESSION_TIMEOUT_S")
124+
.ok()
125+
.and_then(|l| l.parse().map(Duration::from_secs).ok())
126+
.unwrap_or(DEFAULT_WEBSOCKET_SESSION_TIMEOUT);
127+
128+
WebSocketConfig {
129+
idle_timeout,
130+
session_timeout,
131+
}
132+
};
133+
111134
let coordinators_limit = env::var("PLAYGROUND_COORDINATORS_LIMIT")
112135
.ok()
113136
.and_then(|l| l.parse().ok())
@@ -131,6 +154,7 @@ impl Config {
131154
metrics_token,
132155
feature_flags,
133156
request_db_path,
157+
websocket_config,
134158
limits,
135159
port,
136160
root,
@@ -232,3 +256,9 @@ impl limits::Lifecycle for LifecycleMetrics {
232256
metrics::PROCESS_ACTIVE.dec();
233257
}
234258
}
259+
260+
#[derive(Debug, Copy, Clone)]
261+
struct WebSocketConfig {
262+
idle_timeout: Duration,
263+
session_timeout: Duration,
264+
}

ui/src/server_axum.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
UNAVAILABLE_WS,
66
},
77
request_database::Handle,
8-
Config, GhToken, MetricsToken,
8+
Config, GhToken, MetricsToken, WebSocketConfig,
99
};
1010
use async_trait::async_trait;
1111
use axum::{
@@ -111,7 +111,8 @@ pub(crate) async fn serve(config: Config) {
111111
.layer(Extension(db_handle))
112112
.layer(Extension(Arc::new(SandboxCache::default())))
113113
.layer(Extension(config.github_token()))
114-
.layer(Extension(config.feature_flags));
114+
.layer(Extension(config.feature_flags))
115+
.layer(Extension(config.websocket_config));
115116

116117
if let Some(token) = config.metrics_token() {
117118
app = app.layer(Extension(token))
@@ -652,11 +653,12 @@ async fn metrics(_: MetricsAuthorization) -> Result<Vec<u8>, StatusCode> {
652653

653654
async fn websocket(
654655
ws: WebSocketUpgrade,
656+
Extension(config): Extension<WebSocketConfig>,
655657
Extension(factory): Extension<Factory>,
656658
Extension(feature_flags): Extension<crate::FeatureFlags>,
657659
Extension(db): Extension<Handle>,
658660
) -> impl IntoResponse {
659-
ws.on_upgrade(move |s| websocket::handle(s, factory.0, feature_flags.into(), db))
661+
ws.on_upgrade(move |s| websocket::handle(s, config, factory.0, feature_flags.into(), db))
660662
}
661663

662664
#[derive(Debug, serde::Deserialize)]

ui/src/server_axum/websocket.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
metrics::{self, record_metric, Endpoint, HasLabelsCore, Outcome},
33
request_database::Handle,
44
server_axum::api_orchestrator_integration_impls::*,
5+
WebSocketConfig,
56
};
67

78
use axum::extract::ws::{Message, WebSocket};
@@ -199,6 +200,7 @@ struct ExecuteResponse {
199200
#[instrument(skip_all, fields(ws_id))]
200201
pub(crate) async fn handle(
201202
socket: WebSocket,
203+
config: WebSocketConfig,
202204
factory: Arc<CoordinatorFactory>,
203205
feature_flags: FeatureFlags,
204206
db: Handle,
@@ -212,7 +214,7 @@ pub(crate) async fn handle(
212214
tracing::Span::current().record("ws_id", &id);
213215
info!("WebSocket started");
214216

215-
handle_core(socket, factory, feature_flags, db).await;
217+
handle_core(socket, config, factory, feature_flags, db).await;
216218

217219
info!("WebSocket ending");
218220
metrics::LIVE_WS.dec();
@@ -242,9 +244,6 @@ struct CoordinatorManager {
242244
}
243245

244246
impl CoordinatorManager {
245-
const IDLE_TIMEOUT: Duration = Duration::from_secs(60);
246-
const SESSION_TIMEOUT: Duration = Duration::from_secs(45 * 60);
247-
248247
const N_PARALLEL: usize = 2;
249248

250249
const N_KINDS: usize = 1;
@@ -343,6 +342,7 @@ type CoordinatorManagerResult<T, E = CoordinatorManagerError> = std::result::Res
343342

344343
async fn handle_core(
345344
mut socket: WebSocket,
345+
config: WebSocketConfig,
346346
factory: Arc<CoordinatorFactory>,
347347
feature_flags: FeatureFlags,
348348
db: Handle,
@@ -363,7 +363,7 @@ async fn handle_core(
363363
}
364364

365365
let mut manager = CoordinatorManager::new(&factory);
366-
let mut session_timeout = pin!(time::sleep(CoordinatorManager::SESSION_TIMEOUT));
366+
let mut session_timeout = pin!(time::sleep(config.session_timeout));
367367
let mut idle_timeout = pin!(Fuse::terminated());
368368

369369
let mut active_executions = BTreeMap::new();
@@ -409,7 +409,7 @@ async fn handle_core(
409409
// The last task has completed which means we are a
410410
// candidate for idling in a little while.
411411
if manager.is_empty() {
412-
idle_timeout.set(time::sleep(CoordinatorManager::IDLE_TIMEOUT).fuse());
412+
idle_timeout.set(time::sleep(config.idle_timeout).fuse());
413413
}
414414

415415
let (error, meta) = match task {

0 commit comments

Comments
 (0)