Skip to content

Commit 2256f1b

Browse files
committed
balancer: don't accept websocket connections if no monoliths are available
fixes #1248
1 parent abd5d09 commit 2256f1b

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

crates/ott-balancer/src/service.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,23 @@ impl Service<Request<IncomingBody>> for BalancerService {
120120
};
121121

122122
let room_name: RoomName = room_name.to_owned().into();
123+
if room_name.to_string() == "list" {
124+
// special case for listing rooms -- "list" is never a valid room name
125+
return match list_rooms(ctx.clone()).await {
126+
Ok(res) => Ok(res),
127+
Err(e) => {
128+
error!("error listing rooms: {}", e);
129+
mk_response("error listing rooms".to_owned())
130+
}
131+
};
132+
}
133+
134+
let ctx_read = ctx.read().await;
135+
if ctx_read.monoliths.len() == 0 {
136+
debug!(message = "no monoliths", request_id, room = %room_name);
137+
return Ok(no_monoliths());
138+
}
139+
123140
if is_websocket_upgrade(&req) {
124141
debug!(message = "upgrading to websocket", request_id, room = %room_name);
125142
let (response, websocket) = match upgrade(req, None) {
@@ -143,17 +160,7 @@ impl Service<Request<IncomingBody>> for BalancerService {
143160

144161
// Return the response so the spawned future can continue.
145162
Ok(response)
146-
} else if room_name.to_string() == "list" {
147-
// special case for listing rooms
148-
match list_rooms(ctx.clone()).await {
149-
Ok(res) => Ok(res),
150-
Err(e) => {
151-
error!("error listing rooms: {}", e);
152-
mk_response("error listing rooms".to_owned())
153-
}
154-
}
155163
} else {
156-
let ctx_read = ctx.read().await;
157164
let monolith =
158165
if let Some(locator) = ctx_read.rooms_to_monoliths.get(&room_name) {
159166
info!(
@@ -175,7 +182,7 @@ impl Service<Request<IncomingBody>> for BalancerService {
175182
}
176183
}
177184
} else {
178-
mk_response("no monoliths available".to_owned())
185+
Ok(no_monoliths())
179186
}
180187
}
181188
}
@@ -195,7 +202,7 @@ impl Service<Request<IncomingBody>> for BalancerService {
195202
}
196203
}
197204
} else {
198-
mk_response("no monoliths available".to_owned())
205+
Ok(no_monoliths())
199206
}
200207
}
201208
_ => Ok(not_found()),
@@ -220,6 +227,15 @@ fn interval_server_error() -> Response<Full<Bytes>> {
220227
.unwrap()
221228
}
222229

230+
fn no_monoliths() -> Response<Full<Bytes>> {
231+
Response::builder()
232+
.status(StatusCode::SERVICE_UNAVAILABLE)
233+
.body(Full::new(
234+
"No monoliths available to handle request.".into(),
235+
))
236+
.expect("failed to build NO_MONOLITHS")
237+
}
238+
223239
async fn proxy_request(
224240
in_req: Request<IncomingBody>,
225241
target: &BalancerMonolith,

0 commit comments

Comments
 (0)