Skip to content

Commit c2a8e86

Browse files
authored
fix(buffer): Consistent cmp vs == for Priority (#4022)
Derive `Priority::Eq` from `Priority::PartialOrd`. After INC-875, the custom `Ord` implementation for `Priority` was a suspect for occurring panics rust-lang/rust#129561. It turned out the panic occurred somewhere else, but making `Eq` and `PartialOrd` consistent cannot hurt.
1 parent b82aa67 commit c2a8e86

File tree

1 file changed

+34
-19
lines changed
  • relay-server/src/services/buffer/envelope_buffer

1 file changed

+34
-19
lines changed

relay-server/src/services/buffer/envelope_buffer/mod.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,10 @@ impl<K: PartialEq, V> PartialEq for QueueItem<K, V> {
511511

512512
impl<K: PartialEq, V> Eq for QueueItem<K, V> {}
513513

514-
#[derive(Debug)]
514+
#[derive(Debug, Clone)]
515515
struct Priority {
516516
readiness: Readiness,
517517
received_at: Instant,
518-
// FIXME(jjbayer): `last_peek` is currently never updated, see https://github.com/getsentry/relay/pull/3960.
519518
last_peek: Instant,
520519
}
521520

@@ -529,22 +528,6 @@ impl Priority {
529528
}
530529
}
531530

532-
impl PartialEq for Priority {
533-
fn eq(&self, other: &Self) -> bool {
534-
self.readiness.ready() == other.readiness.ready()
535-
&& self.received_at == other.received_at
536-
&& self.last_peek == other.last_peek
537-
}
538-
}
539-
540-
impl PartialOrd for Priority {
541-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
542-
Some(self.cmp(other))
543-
}
544-
}
545-
546-
impl Eq for Priority {}
547-
548531
impl Ord for Priority {
549532
fn cmp(&self, other: &Self) -> Ordering {
550533
match (self.readiness.ready(), other.readiness.ready()) {
@@ -565,7 +548,21 @@ impl Ord for Priority {
565548
}
566549
}
567550

568-
#[derive(Debug)]
551+
impl PartialOrd for Priority {
552+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
553+
Some(self.cmp(other))
554+
}
555+
}
556+
557+
impl PartialEq for Priority {
558+
fn eq(&self, other: &Self) -> bool {
559+
self.cmp(other).is_eq()
560+
}
561+
}
562+
563+
impl Eq for Priority {}
564+
565+
#[derive(Debug, Clone, Copy)]
569566
struct Readiness {
570567
own_project_ready: bool,
571568
sampling_project_ready: bool,
@@ -948,6 +945,24 @@ mod tests {
948945
assert_eq!(buffer.priority_queue.len(), 2);
949946
}
950947

948+
#[test]
949+
fn test_total_order() {
950+
let p1 = Priority {
951+
readiness: Readiness {
952+
own_project_ready: true,
953+
sampling_project_ready: true,
954+
},
955+
received_at: Instant::now(),
956+
last_peek: Instant::now(),
957+
};
958+
let mut p2 = p1.clone();
959+
p2.last_peek += Duration::from_millis(1);
960+
961+
// Last peek does not matter because project is ready:
962+
assert_eq!(p1.cmp(&p2), Ordering::Equal);
963+
assert_eq!(p1, p2);
964+
}
965+
951966
#[tokio::test]
952967
async fn test_last_peek_internal_order() {
953968
let mut buffer = EnvelopeBuffer::<MemoryStackProvider>::new(mock_memory_checker());

0 commit comments

Comments
 (0)