Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
let the users that exceed reserves in
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Jan 11, 2021
1 parent ddab307 commit a5aea1c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
32 changes: 21 additions & 11 deletions src/app/endpoint/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,13 +1165,22 @@ mod test {
intent: ConnectIntent::Read,
};

// Expect failure.
let err = handle_request::<ConnectHandler>(&mut context, &reader1, payload)
// Should be ok since we disregard reserves.
handle_request::<ConnectHandler>(&mut context, &reader1, payload)
.await
.expect_err("Connected to RTC while expected capacity exceeded error");
.expect("RTC connect failed");

assert_eq!(err.status(), ResponseStatus::SERVICE_UNAVAILABLE);
assert_eq!(err.kind(), "capacity_exceeded");
// Delete agent
{
let conn = context.get_conn().expect("Failed to acquire db conn");
let row_count = db::agent::DeleteQuery::new()
.agent_id(&reader1.agent_id())
.room_id(rtc2.room_id())
.execute(&conn)
.expect("Failed to delete user from agents");
// Check that we actually deleted something
assert_eq!(row_count, 1);
}

// Connect to the rtc in the room with free reserved slots.
let payload = ConnectRequest {
Expand Down Expand Up @@ -1671,13 +1680,14 @@ mod test {
};

// Last room has NO reserve AND there is free capacity BUT it was exhausted by first two rooms
// So we cant connect to this room
let err = handle_request::<ConnectHandler>(&mut context, &new_reader, payload)
// So in theory we should not be able to connect to this room due to capacity_exceeded error
// But we still let the user through because:
// 1. we almost never fill any server with users upto max capacity
// 2. thus there are unused slots anyway
// So its better to let them in
handle_request::<ConnectHandler>(&mut context, &new_reader, payload)
.await
.expect_err("Connected to RTC while expected capacity exceeded error");

assert_eq!(err.status(), ResponseStatus::SERVICE_UNAVAILABLE);
assert_eq!(err.kind(), "capacity_exceeded");
.expect("RTC connect failed");
});
}

Expand Down
22 changes: 13 additions & 9 deletions src/db/janus_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,19 @@ const FREE_CAPACITY_SQL: &str = r#"
CASE
WHEN COALESCE(jb.capacity, 2147483647) <= COALESCE(jbl.total_taken, 0) THEN 0
ELSE (
CASE
WHEN COALESCE(ar.reserve, 0) > COALESCE(rl.taken, 0)
THEN LEAST(
COALESCE(ar.reserve, 0) - COALESCE(rl.taken, 0),
COALESCE(jb.capacity, 2147483647) - COALESCE(jbl.total_taken, 0)
)
ELSE
GREATEST(COALESCE(jb.capacity, 2147483647) - COALESCE(jbl.load, 0), 0)
END
GREATEST(
(
CASE
WHEN COALESCE(ar.reserve, 0) > COALESCE(rl.taken, 0)
THEN LEAST(
COALESCE(ar.reserve, 0) - COALESCE(rl.taken, 0),
COALESCE(jb.capacity, 2147483647) - COALESCE(jbl.total_taken, 0)
)
ELSE
GREATEST(COALESCE(jb.capacity, 2147483647) - COALESCE(jbl.load, 0), 0)
END
),
1)
)
END
)::INT AS free_capacity
Expand Down

0 comments on commit a5aea1c

Please sign in to comment.