Skip to content
Merged
6 changes: 5 additions & 1 deletion libs/runtime/core/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::{
},
consts::{
DEFAULT_EVENT_QUEUE_CAPACITY, DEFAULT_RUNTIME_HEALTH_CHECK_ENDPOINT,
DISPATCHER_COMMAND_BUFFER,
DISPATCHER_COMMAND_BUFFER, WS_CONNECTION_DEFAULT_HANDLER_CONCURRENCY,
},
consumer_handler::{
ConsumerEventHandler, EventQueueConsumerEventHandler, SharedConsumerEventHandler,
Expand Down Expand Up @@ -444,6 +444,10 @@ impl Application {
.map(|_| websocket_config.auth_strategy.clone()),
connection_auth_guard_names: websocket_config.connection_auth_guard.clone(),
connection_auth_guards: self.custom_auth_guards.clone(),
handler_concurrency: self
.runtime_config
.ws_handler_concurrency
.unwrap_or(WS_CONNECTION_DEFAULT_HANDLER_CONCURRENCY),
cors: api_config.cors.clone(),
resource_store: resource_store.clone(),
}),
Expand Down
51 changes: 51 additions & 0 deletions libs/runtime/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ pub struct RuntimeConfig {
/// for the suggested default the WebSocket runtime protocol names, which is 3.
/// Set via `CELERITY_WS_ACK_MAX_ATTEMPTS`.
pub ws_ack_max_attempts: Option<u32>,
/// How many of one connection's messages may be handled at the same time.
///
/// Leave unset for eight. Set it to one for a connection whose messages have
/// to be handled in the order they arrived, at the cost of capping the
/// connection at one handler's worth of latency per message. Bounds one
/// connection rather than the process.
/// Set via `CELERITY_WS_HANDLER_CONCURRENCY`.
pub ws_handler_concurrency: Option<usize>,
}

impl RuntimeConfig {
Expand Down Expand Up @@ -363,6 +371,7 @@ impl RuntimeConfig {

let ws_ack_timeout_ms = ws_ack_timeout_ms_from_env(env);
let ws_ack_max_attempts = ws_ack_max_attempts_from_env(env);
let ws_handler_concurrency = ws_handler_concurrency_from_env(env);

RuntimeConfig {
blueprint_config_path,
Expand Down Expand Up @@ -393,6 +402,7 @@ impl RuntimeConfig {
deploy_target,
ws_ack_timeout_ms,
ws_ack_max_attempts,
ws_handler_concurrency,
}
}

Expand Down Expand Up @@ -450,6 +460,24 @@ pub fn ws_ack_max_attempts_from_env(env: &impl EnvVars) -> Option<u32> {
})
}

/// Reads how many of a connection's messages may be handled at the same time.
///
/// See [`ws_ack_timeout_ms_from_env`] for why this is not only read in
/// [`RuntimeConfig::from_env`].
pub fn ws_handler_concurrency_from_env(env: &impl EnvVars) -> Option<usize> {
env.var("CELERITY_WS_HANDLER_CONCURRENCY").ok().map(|val| {
let concurrency: usize = val
.parse()
.expect("Invalid WebSocket handler concurrency, must be a whole number");
assert!(
concurrency > 0,
"Invalid WebSocket handler concurrency, a connection has to handle at least one \
message at a time"
);
Comment thread
fr3shw3b marked this conversation as resolved.
Resolved
concurrency
})
}

#[derive(Debug)]
pub struct AppConfig {
pub api: Option<ApiConfig>,
Expand Down Expand Up @@ -857,4 +885,27 @@ mod tests {
fn test_an_ack_timeout_that_is_not_a_number_is_refused() {
RuntimeConfig::from_env(&env(&[("CELERITY_WS_ACK_TIMEOUT_MS", "ten seconds")]));
}

#[test]
fn test_handler_concurrency_is_left_to_the_runtime_when_unset() {
assert_eq!(
RuntimeConfig::from_env(&env(&[])).ws_handler_concurrency,
None
);
}

#[test]
fn test_handler_concurrency_is_read_from_the_environment() {
let config = RuntimeConfig::from_env(&env(&[("CELERITY_WS_HANDLER_CONCURRENCY", "8")]));

assert_eq!(config.ws_handler_concurrency, Some(8));
}

/// Nothing at all would leave a connection unable to handle anything,
/// rather than meaning handle nothing in parallel.
#[test]
#[should_panic(expected = "one message at a time")]
fn test_no_handler_concurrency_at_all_is_refused() {
RuntimeConfig::from_env(&env(&[("CELERITY_WS_HANDLER_CONCURRENCY", "0")]));
}
}
44 changes: 44 additions & 0 deletions libs/runtime/core/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ pub const WS_CONNECTION_WORK_SHED_GRACE_MS: u64 = 1_000;
// enough for work that is nearly done to land.
pub const WS_CONNECTION_DRAIN_GRACE_MS: u64 = 5_000;

// How many of one connection's messages may be handled at the same time.
//
// More than one, because the same API deploys here or to a serverless gateway,
// which runs a function per message with nothing between them. Handling one at
// a time would make this the only target that puts a connection's messages in a
// queue, and an application built against that would carry an ordering
// dependency it cannot see until it is deployed somewhere that does not honour
// it. A deployment that needs them in order asks for one.
//
// Bounds one connection rather than the process. What bounds the process is the
// credit a handler grants its stream, and what stops a connection's messages
// taking all of that is the default per tag limit, which keeps a place for the
// tags nothing else can stand in for, the disconnect handler among them.
pub const WS_CONNECTION_DEFAULT_HANDLER_CONCURRENCY: usize = 8;

// The `retryAfter` hint sent in the close frame when a connection is shed for
// outrunning its handlers.
//
Expand All @@ -136,6 +151,35 @@ pub const WS_CONNECTION_DRAIN_GRACE_MS: u64 = 5_000;
// their own jitter.
pub const WS_CONNECTION_SATURATED_RETRY_AFTER_MS: u64 = 5_000;

// How long a cancelled event is left with its handler before the runtime takes
// back what it was holding.
//
// Credit and a per tag place are consumed when an event is dispatched and
// returned when the handler answers, including when it answers late. A handler
// that never answers would otherwise hold both for the life of the process, so
// a stream would lose a place every time one hung and would eventually stop
// dispatching altogether.
//
// Taking them back is not a statement that the handler stopped, since nothing
// can make it stop. It says the runtime will no longer keep room for an answer
// it asked to be abandoned. Long enough for a handler that honours a
// cancellation to unwind and answer, which is the case that should not be
// treated as a hang.
pub const HANDLER_CANCEL_RECLAIM_GRACE_SECS: u64 = 5;

// How long the runtime remembers an event whose place it took back.
//
// A result carrying no credit is a handler withholding, asking for nothing
// further until it grants again. Arriving after the place was taken back, that
// asks for a place the runtime has already returned, so the return has to be
// undone or the handler is sent work it said it could not take. Remembering
// which events those were is what makes that possible.
//
// Bounded because a handler that never answers would otherwise be remembered
// for good, and generous because the answer being late is the whole reason
// there is anything to remember.
pub const HANDLER_RECLAIM_MEMORY_SECS: u64 = 60;

// How many dispatcher commands may be queued before a handler stream waits.
// Commands are small and are drained by a single task, so this only needs to
// absorb bursts of results arriving together.
Expand Down
Loading
Loading