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
2 changes: 1 addition & 1 deletion rust/src/runtime/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ where
"query_snapshot_exceeds_budget",
));
}
let task_id = ulid::Ulid::new().to_string();
let task_id = super::store::id();
let key = key(&task_id)?;
let admitted = authority::now();
let (deadline, retained_until) = config.timing(admitted)?;
Expand Down
22 changes: 21 additions & 1 deletion rust/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,28 @@ pub(super) struct Store<DB: Backend> {
pub(super) history_budget: super::history_budget::Config,
pub(super) search_attribute_limit: i64,
}
/// A ULID that sorts after every earlier one from this process, as Laravel's
/// (Symfony's) generator does. Rows created in the same millisecond, such as
/// the activity tasks of one completion, keep their creation order in
/// `ORDER BY available_at,id`.
pub(super) fn id() -> String {
Ulid::new().to_string()
static GENERATOR: std::sync::Mutex<ulid::Generator> =
std::sync::Mutex::new(ulid::Generator::new());
let mut generator = GENERATOR
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
// Overflow needs 2^80 identifiers in one millisecond.
generator
.generate()
.unwrap_or_else(|_| Ulid::new())
.to_string()
}

#[cfg(test)]
#[test]
fn identifiers_keep_creation_order_within_a_millisecond() {
let ids: Vec<String> = (0..10_000).map(|_| id()).collect();
assert!(ids.windows(2).all(|pair| pair[0] < pair[1]));
}
pub(super) fn now() -> DateTime<Utc> {
DateTime::from_timestamp_millis(Utc::now().timestamp_millis()).expect("current timestamp")
Expand Down
Loading