diff --git a/audit/window.go b/audit/window.go new file mode 100644 index 0000000..2709686 --- /dev/null +++ b/audit/window.go @@ -0,0 +1,23 @@ +package audit + +type Window struct { + Last uint16 + HasLast bool + Summary Summary +} + +func (w *Window) Observe(sequence uint16) string { + if !w.HasLast { + w.Last, w.HasLast = sequence, true + w.Summary.Accepted++ + w.Summary.Latest = sequence + return "accepted" + } + delta := uint16(sequence - w.Last) + if delta == 0 { w.Summary.Duplicate++; return "duplicate" } + if delta > 32767 { w.Summary.Reordered++; return "reordered" } + if delta > 1 { w.Summary.Lost += uint64(delta - 1) } + w.Last, w.Summary.Latest = sequence, sequence + w.Summary.Accepted++ + return "accepted" +}