diff --git a/lib/faraday.ml b/lib/faraday.ml index d48d7e3..901b8e7 100644 --- a/lib/faraday.ml +++ b/lib/faraday.ml @@ -422,7 +422,23 @@ let rec shift_buffers t written = let rec shift_flushes t = try let (threshold, f) as flush = Flushes.dequeue_exn t.flushed in - if t.bytes_written >= threshold then begin f (); shift_flushes t end + (* Edited notes from @dinosaure: + * + * The quantities [t.bytes_written] and [threshold] are always going to be + * positive integers. Therefore, we can treat them as unsinged integers for + * the purposes of comparision. Doing so allows us to handle overflows in + * either quantity as long as they're both within one overflow of each other. + * We can accomplish this by subracting [min_int] from both quantities before + * comparision. This shift a quantity that has not overflowed into the + * negative integer range while shifting a quantity that has overflow into + * the positive integer range. + * + * This effectively restablishes the relative difference when an overflow + * has occurred, and otherwise just compares numbers that haven't + * overflowed as similarly, just shifted down a bit. + *) + if t.bytes_written - min_int >= threshold - min_int + then begin f (); shift_flushes t end else Flushes.enqueue_front flush t.flushed with Not_found -> ()