Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions libs/runtime/core/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ impl Application {
WebSocketConnRegistryConfig {
ack_worker_config: Some(AckWorkerConfig {
message_action_check_interval_ms: None,
message_timeout_ms: None,
max_attempts: None,
message_timeout_ms: self.runtime_config.ws_ack_timeout_ms,
max_attempts: self.runtime_config.ws_ack_max_attempts,
}),
server_node_name: "node1".to_string(),
},
Expand Down Expand Up @@ -443,8 +443,7 @@ impl Application {
.as_ref()
.map(|_| websocket_config.auth_strategy.clone()),
connection_auth_guard_names: websocket_config.connection_auth_guard.clone(),
connection_auth_guards: self
.get_custom_auth_guards_blocking(&websocket_config.connection_auth_guard),
connection_auth_guards: self.custom_auth_guards.clone(),
cors: api_config.cors.clone(),
resource_store: resource_store.clone(),
}),
Expand Down Expand Up @@ -505,30 +504,6 @@ impl Application {
Ok(http_server_app)
}

fn get_custom_auth_guards_blocking(
&self,
guard_names: &Option<Vec<String>>,
) -> HashMap<String, Arc<dyn AuthGuardHandler + Send + Sync>> {
let mut guards = HashMap::new();
if let Some(names) = guard_names {
// Taken without blocking. Setup is the only thing touching this map
// at this point, so the lock is always free, and a blocking take
// panics outright when setup is called from inside a runtime, which
// is how an application with connection auth guards is started from
// an async context.
let Ok(all_guards) = self.custom_auth_guards.try_lock() else {
error!("custom auth guards were unavailable while setting up the server");
return guards;
};
for name in names {
if let Some(guard) = all_guards.get(name) {
guards.insert(name.clone(), guard.clone());
}
}
}
guards
}

/// Adds the endpoint that invokes a handler directly by name, which exists
/// so that a handler can be exercised while developing or testing.
///
Expand Down
97 changes: 97 additions & 0 deletions libs/runtime/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ pub struct RuntimeConfig {
///
/// Set via `CELERITY_DEPLOY_TARGET`.
pub deploy_target: Option<String>,
/// How long the runtime waits for a client to acknowledge a message that
/// asked to be acknowledged, before sending it again.
///
/// Leave unset for the suggested default the WebSocket runtime protocol names,
/// which is 10 seconds. Set via `CELERITY_WS_ACK_TIMEOUT_MS`.
pub ws_ack_timeout_ms: Option<u64>,
/// How many times a message asking to be acknowledged is sent before it is
/// considered lost and the clients waiting on it are told so.
///
/// Counts the first send, so 3 means the original and two more. Leave unset
/// 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>,
}

impl RuntimeConfig {
Expand Down Expand Up @@ -348,6 +361,9 @@ impl RuntimeConfig {

let deploy_target = env.var("CELERITY_DEPLOY_TARGET").ok();

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

RuntimeConfig {
blueprint_config_path,
runtime_call_mode,
Expand Down Expand Up @@ -375,6 +391,8 @@ impl RuntimeConfig {
metrics_enabled,
trace_sample_ratio,
deploy_target,
ws_ack_timeout_ms,
ws_ack_max_attempts,
}
}

Expand All @@ -397,6 +415,41 @@ impl RuntimeConfig {
}
}

/// Reads the WebSocket acknowledgement timeout from the environment.
///
/// Separate from [`RuntimeConfig::from_env`] so that the SDKs, which rebuild a
/// runtime configuration from their own, read it the same way rather than each
/// deciding what the variable is called.
pub fn ws_ack_timeout_ms_from_env(env: &impl EnvVars) -> Option<u64> {
env.var("CELERITY_WS_ACK_TIMEOUT_MS").ok().map(|val| {
let timeout: u64 = val
.parse()
.expect("Invalid WebSocket ack timeout, must be a whole number of milliseconds");
assert!(
timeout > 0,
"Invalid WebSocket ack timeout, a client needs some time to answer in"
);
timeout
})
}

/// Reads the WebSocket acknowledgement attempt limit from the environment.
///
/// See [`ws_ack_timeout_ms_from_env`] for why this is not only read in
/// [`RuntimeConfig::from_env`].
pub fn ws_ack_max_attempts_from_env(env: &impl EnvVars) -> Option<u32> {
env.var("CELERITY_WS_ACK_MAX_ATTEMPTS").ok().map(|val| {
let attempts: u32 = val
.parse()
.expect("Invalid WebSocket ack max attempts, must be a whole number");
assert!(
attempts > 0,
"Invalid WebSocket ack max attempts, a message has to be sent at least once"
);
attempts
})
}

#[derive(Debug)]
pub struct AppConfig {
pub api: Option<ApiConfig>,
Expand Down Expand Up @@ -760,4 +813,48 @@ mod tests {
assert_eq!(config.runtime_socket_fallback_port, 0);
assert!(!config.runtime_socket_fallback_enabled);
}

/// Unset means the good defaults the protocol names, which the worker
/// applies, rather than a value chosen here.
#[test]
fn test_ack_timings_are_left_to_the_worker_when_unset() {
let config = RuntimeConfig::from_env(&env(&[]));

assert_eq!(config.ws_ack_timeout_ms, None);
assert_eq!(config.ws_ack_max_attempts, None);
}

#[test]
fn test_ack_timings_are_read_from_the_environment() {
let config = RuntimeConfig::from_env(&env(&[
("CELERITY_WS_ACK_TIMEOUT_MS", "2500"),
("CELERITY_WS_ACK_MAX_ATTEMPTS", "5"),
]));

assert_eq!(config.ws_ack_timeout_ms, Some(2500));
assert_eq!(config.ws_ack_max_attempts, Some(5));
}

/// A message has to be sent once before it can be resent, so no attempts at
/// all is a configuration that cannot be honoured rather than one meaning
/// never send.
#[test]
#[should_panic(expected = "sent at least once")]
fn test_no_ack_attempts_at_all_is_refused() {
RuntimeConfig::from_env(&env(&[("CELERITY_WS_ACK_MAX_ATTEMPTS", "0")]));
}

/// No time at all leaves every message overdue as soon as it is sent, so
/// the first check resends it and the attempts run out in milliseconds.
#[test]
#[should_panic(expected = "some time to answer in")]
fn test_an_ack_timeout_of_nothing_is_refused() {
RuntimeConfig::from_env(&env(&[("CELERITY_WS_ACK_TIMEOUT_MS", "0")]));
}

#[test]
#[should_panic(expected = "whole number of milliseconds")]
fn test_an_ack_timeout_that_is_not_a_number_is_refused() {
RuntimeConfig::from_env(&env(&[("CELERITY_WS_ACK_TIMEOUT_MS", "ten seconds")]));
}
}
2 changes: 2 additions & 0 deletions libs/runtime/core/src/transform_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,8 @@ mod tests {
metrics_enabled: false,
trace_sample_ratio: 1.0,
deploy_target: None,
ws_ack_timeout_ms: None,
ws_ack_max_attempts: None,
}
}

Expand Down
Loading
Loading