use std::time::Duration;
use hatchet_proto::v1;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IdempotencyMethod {
Ttl,
Status,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[must_use = "an idempotency config does nothing until it is given to a declaration"]
pub struct Idempotency {
expression: String,
window: Duration,
method: IdempotencyMethod,
}
impl Idempotency {
pub fn ttl(expression: impl Into<String>, window: Duration) -> Self {
Self {
expression: expression.into(),
window,
method: IdempotencyMethod::Ttl,
}
}
pub fn until_run_ends(expression: impl Into<String>, fallback: Duration) -> Self {
Self {
expression: expression.into(),
window: fallback,
method: IdempotencyMethod::Status,
}
}
pub(crate) fn validate(&self) -> Result<(), String> {
if self.expression.trim().is_empty() {
return Err(
"an idempotency expression must not be empty; it is CEL over the \
run's input, such as `input.order_id`"
.to_string(),
);
}
if self.window.as_millis() == 0 {
return Err(
"an idempotency window must not round down to a zero ttl in \
milliseconds; the engine rejects a zero ttl at registration on \
v0.107.0, and what it does with it on older engines is not \
documented, so the SDK refuses it rather than letting idempotency \
quietly not apply"
.to_string(),
);
}
Ok(())
}
pub(crate) fn to_proto(&self) -> v1::IdempotencyConfig {
let method = match self.method {
IdempotencyMethod::Ttl => v1::IdempotencyMethod::Ttl,
IdempotencyMethod::Status => v1::IdempotencyMethod::Status,
};
v1::IdempotencyConfig {
expression: self.expression.clone(),
ttl_ms: i64::try_from(self.window.as_millis()).unwrap_or(i64::MAX),
method: Some(method as i32),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use hatchet_proto::v1;
#[test]
fn a_ttl_config_sends_the_expression_the_window_and_the_method() {
assert_eq!(
Idempotency::ttl("input.order_id", Duration::from_secs(600)).to_proto(),
v1::IdempotencyConfig {
expression: "input.order_id".to_string(),
ttl_ms: 600_000,
method: Some(v1::IdempotencyMethod::Ttl as i32),
}
);
}
#[test]
fn a_status_config_sends_its_fallback_as_the_ttl() {
assert_eq!(
Idempotency::until_run_ends("input.order_id", Duration::from_secs(10)).to_proto(),
v1::IdempotencyConfig {
expression: "input.order_id".to_string(),
ttl_ms: 10_000,
method: Some(v1::IdempotencyMethod::Status as i32),
}
);
}
#[test]
fn sub_millisecond_windows_are_truncated_to_whole_milliseconds() {
let config = Idempotency::ttl("input.id", Duration::from_micros(1_500));
assert_eq!(config.to_proto().ttl_ms, 1);
}
#[test]
fn an_empty_expression_is_invalid() {
let reason = Idempotency::ttl(" ", Duration::from_secs(60))
.validate()
.unwrap_err();
assert!(reason.contains("expression"), "{reason}");
}
#[test]
fn a_zero_window_is_invalid() {
let reason = Idempotency::ttl("input.id", Duration::ZERO)
.validate()
.unwrap_err();
assert!(reason.contains("zero"), "{reason}");
}
#[test]
fn a_window_longer_than_i64_milliseconds_saturates_rather_than_panicking() {
let config = Idempotency::ttl("input.id", Duration::MAX);
assert_eq!(config.to_proto().ttl_ms, i64::MAX);
}
#[test]
fn a_window_under_one_millisecond_is_invalid() {
let reason = Idempotency::ttl("input.id", Duration::from_micros(500))
.validate()
.unwrap_err();
assert!(reason.contains("zero"), "{reason}");
}
}