Skip to content

Commit d205f66

Browse files
committed
Merge pull request #23 from dinosaure/fix-unsigned-int
2 parents 42944d3 + 52def33 commit d205f66

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/faraday.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,23 @@ let rec shift_buffers t written =
422422
let rec shift_flushes t =
423423
try
424424
let (threshold, f) as flush = Flushes.dequeue_exn t.flushed in
425-
if t.bytes_written >= threshold then begin f (); shift_flushes t end
425+
(* Edited notes from @dinosaure:
426+
*
427+
* The quantities [t.bytes_written] and [threshold] are always going to be
428+
* positive integers. Therefore, we can treat them as unsinged integers for
429+
* the purposes of comparision. Doing so allows us to handle overflows in
430+
* either quantity as long as they're both within one overflow of each other.
431+
* We can accomplish this by subracting [min_int] from both quantities before
432+
* comparision. This shift a quantity that has not overflowed into the
433+
* negative integer range while shifting a quantity that has overflow into
434+
* the positive integer range.
435+
*
436+
* This effectively restablishes the relative difference when an overflow
437+
* has occurred, and otherwise just compares numbers that haven't
438+
* overflowed as similarly, just shifted down a bit.
439+
*)
440+
if t.bytes_written - min_int >= threshold - min_int
441+
then begin f (); shift_flushes t end
426442
else Flushes.enqueue_front flush t.flushed
427443
with Not_found ->
428444
()

0 commit comments

Comments
 (0)