Skip to content

Commit

Permalink
Merge pull request #23 from dinosaure/fix-unsigned-int
Browse files Browse the repository at this point in the history
  • Loading branch information
seliopou committed Sep 1, 2017
2 parents 42944d3 + 52def33 commit d205f66
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/faraday.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
()
Expand Down

0 comments on commit d205f66

Please sign in to comment.