Skip to content
Open
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
9 changes: 5 additions & 4 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ impl Storage {
where
I: IntoIterator<Item = (&'a [u8], (&'a [u8], u64))>,
{
// Compute base time once per batch to avoid per-item syscalls/conversions.
let base_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_secs();
let mut batch = WriteBatchWithTransaction::<true>::default();
// Reuse a byte buffer for capnp serialization across items to avoid repeated allocations.
let mut stored_contents: Vec<u8> = Vec::new();
for (id, (contents, visibility_timeout_secs)) in items.into_iter() {
let main_key = AvailableKey::from_id(id);
let now = std::time::SystemTime::now();
let visible_ts_secs = (now + std::time::Duration::from_secs(visibility_timeout_secs))
.duration_since(std::time::UNIX_EPOCH)?
.as_secs();
let visible_ts_secs = base_secs.saturating_add(visibility_timeout_secs);
let visibility_index_key =
VisibilityIndexKey::from_visible_ts_and_id(visible_ts_secs, id);

Expand Down
35 changes: 35 additions & 0 deletions tests/enqueue_visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use queueber::storage::Storage;

#[test]
fn enqueue_uses_batched_time_for_visibility_index() {
let tmp = tempfile::tempdir().expect("tempdir");
let storage = Storage::new(tmp.path()).expect("storage open");

// Enqueue three items with increasing visibility offsets
storage
.add_available_items_from_parts([
(b"id-a".as_ref(), (b"a".as_ref(), 0u64)),
(b"id-b".as_ref(), (b"b".as_ref(), 1u64)),
(b"id-c".as_ref(), (b"c".as_ref(), 2u64)),
])
.expect("enqueue batch");

// Peek the earliest visibility timestamp; it should be >= now and <= now + 2
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();

let first_ts = storage
.peek_next_visibility_ts_secs()
.expect("peek ok")
.expect("some ts");

// Allow small slop since the test itself takes a little time
assert!(
first_ts >= now_secs && first_ts <= now_secs + 2,
"first_ts={}, now={}",
first_ts,
now_secs
);
}
Loading