Skip to content

Commit

Permalink
clock: remove is_{shorter,longest}, add shortest,longest
Browse files Browse the repository at this point in the history
the former are present in Mtime.Span already, while the latter arent

Signed-off-by: Pau Ruiz Safont <[email protected]>
  • Loading branch information
psafont committed Dec 31, 2024
1 parent 27fb153 commit 4ebcd0c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
4 changes: 1 addition & 3 deletions ocaml/database/master_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ end

exception Content_length_required

let shortest a b = if Clock.Timer.span_is_longer a ~than:b then b else a

let do_db_xml_rpc_persistent_with_reopen ~host:_ ~path (req : string) :
Db_interface.response =
let call_started = Time.start () in
Expand All @@ -257,7 +255,7 @@ let do_db_xml_rpc_persistent_with_reopen ~host:_ ~path (req : string) :
let backoff_delay = ref min_backoff_delay in
let update_backoff_delay () =
let doubled_delay = Mtime.Span.(2 * !backoff_delay) in
backoff_delay := shortest doubled_delay max_backoff_delay
backoff_delay := Clock.Timer.span_shortest doubled_delay max_backoff_delay
in
while not !write_ok do
try
Expand Down
4 changes: 2 additions & 2 deletions ocaml/forkexecd/test/fe_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ type in_range = In_range | Longer | Shorter

let in_range ~e:leeway ~around span =
let upper = Mtime.Span.add around leeway in
if Clock.Timer.span_is_shorter ~than:around span then
if Mtime.Span.is_shorter ~than:around span then
Shorter
else if Clock.Timer.span_is_longer ~than:upper span then
else if Mtime.Span.is_longer ~than:upper span then
Longer
else
In_range
Expand Down
13 changes: 6 additions & 7 deletions ocaml/libs/clock/test_timer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ let test_timer_remaining =
The following equation must hold:
[duration / 2 <= actual < duration]
*)
QCheck2.assume (Timer.span_is_shorter actual ~than:duration) ;
QCheck2.assume
(not (Timer.span_is_shorter Mtime.Span.(2 * actual) ~than:duration)) ;
QCheck2.assume (Mtime.Span.is_shorter actual ~than:duration) ;
QCheck2.assume (not Mtime.Span.(is_shorter (2 * actual) ~than:duration)) ;
let () =
match Timer.remaining timer with
| Expired _ when half < 0.05 ->
Expand All @@ -38,7 +37,7 @@ let test_timer_remaining =
Mtime.Span.pp t Mtime.Span.pp duration Mtime.Span.pp actual Timer.pp
timer
| Remaining t ->
if Timer.span_is_longer Mtime.Span.(2 * t) ~than:duration then
if Mtime.Span.is_longer Mtime.Span.(2 * t) ~than:duration then
Test.fail_reportf
"Expected to have less than half spare time, but got: %a. \
Duration: %a, actual: %a, timer: %a"
Expand All @@ -49,7 +48,7 @@ let test_timer_remaining =
(* 3 * half > duration, so we expect Excess to be reported now *)
Unix.sleepf (2. *. half) ;
let actual = Mtime_clock.count elapsed in
QCheck2.assume (Timer.span_is_longer actual ~than:duration) ;
QCheck2.assume (Mtime.Span.is_longer actual ~than:duration) ;
let () =
match Timer.remaining timer with
| Expired _ ->
Expand Down Expand Up @@ -87,7 +86,7 @@ let test_span_compare =
let ( < ) a b = Mtime.Span.compare a b < 0 in
Alcotest.(check bool)
"is_shorter doesn't match compare" (a < b)
(Timer.span_is_shorter a ~than:b)
(Mtime.Span.is_shorter a ~than:b)
in
let tests_shorter =
List.map
Expand All @@ -100,7 +99,7 @@ let test_span_compare =
let ( > ) a b = Mtime.Span.compare a b > 0 in
Alcotest.(check bool)
"is_longer doesn't match compare" (a > b)
(Timer.span_is_longer a ~than:b)
(Mtime.Span.is_longer a ~than:b)
in
let tests_longer =
List.map
Expand Down
10 changes: 5 additions & 5 deletions ocaml/libs/clock/timer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ type t = {elapsed: Mtime_clock.counter; duration: Mtime.Span.t}

type countdown = Remaining of Mtime.Span.t | Expired of Mtime.Span.t

let span_is_shorter a ~than:b = Mtime.Span.compare a b < 0
let span_shortest a b = if Mtime.Span.is_shorter b ~than:a then b else a

let span_is_longer a ~than:b = Mtime.Span.compare a b > 0
let span_longest a b = if Mtime.Span.is_longer b ~than:a then b else a

let start ~duration = {elapsed= Mtime_clock.counter (); duration}

Expand All @@ -15,18 +15,18 @@ let elapsed t = Mtime_clock.count t.elapsed
let remaining t =
let elapsed = Mtime_clock.count t.elapsed in
let difference = Mtime.Span.abs_diff elapsed t.duration in
if span_is_shorter elapsed ~than:t.duration then
if Mtime.Span.is_shorter elapsed ~than:t.duration then
Remaining difference
else
Expired difference

let has_expired t =
let elapsed = Mtime_clock.count t.elapsed in
not (span_is_shorter elapsed ~than:t.duration)
not (Mtime.Span.is_shorter elapsed ~than:t.duration)

let shorten_by dur t =
let duration =
if span_is_longer dur ~than:t.duration then
if Mtime.Span.is_longer dur ~than:t.duration then
Mtime.Span.zero
else
Mtime.Span.abs_diff dur t.duration
Expand Down
10 changes: 6 additions & 4 deletions ocaml/libs/clock/timer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ val pp : t Fmt.t

(** Mtime.Span helpers *)

val span_is_shorter : Mtime.Span.t -> than:Mtime.Span.t -> bool
(** [is_shorter dur ~than] returns whether [dur] lasts less than [than]. *)
val span_shortest : Mtime.Span.t -> Mtime.Span.t -> Mtime.Span.t
(** [span_shortest a b] returns the shortest span of [a] and [b],
functionally equivalent to [min a b]. *)

val span_is_longer : Mtime.Span.t -> than:Mtime.Span.t -> bool
(** [is_longer dur ~than] returns whether [dur] lasts more than [than]. *)
val span_longest : Mtime.Span.t -> Mtime.Span.t -> Mtime.Span.t
(** [span_longest a b] returns the longest span of [a] and [b],
functionally equivalent to [max a b]. *)

val span_to_s : Mtime.Span.t -> float
(** [span_to_s span] converts a time span into seconds, represented by a float.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ let extra_timeout = Mtime.Span.(250 * ms)

let check_timeout elapsed timeout =
let timeout_span = Clock.Timer.s_to_span timeout |> Option.get in
if
Clock.Timer.span_is_longer elapsed
~than:(Mtime.Span.add Mtime.Span.(2 * timeout_span) extra_timeout)
if Mtime.Span.(is_longer elapsed ~than:(add (2 * timeout_span) extra_timeout))
then
Test.fail_reportf "Timed out too late: %a > %f" Mtime.Span.pp elapsed
timeout ;
Expand Down Expand Up @@ -268,7 +266,7 @@ let test_select =
let () =
match ready with
| [], [], [] ->
if Clock.Timer.span_is_shorter elapsed ~than:timeout_span then
if Mtime.Span.is_shorter elapsed ~than:timeout_span then
Test.fail_reportf "Timed out too early: %a < %f" Mtime.Span.pp elapsed
timeout
| _ ->
Expand Down
2 changes: 1 addition & 1 deletion ocaml/xapi/helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,7 @@ module AuthenticationCache = struct
match Q.find_opt user t.cache with
| Some {data= secret; expires} ->
let elapsed = Mtime_clock.count t.elapsed in
if Clock.Timer.span_is_longer elapsed ~than:expires then (
if Mtime.Span.is_longer elapsed ~than:expires then (
(* Remove expired entry - regardless of whether
authentication would succeed. *)
Q.remove t.cache user ;
Expand Down

0 comments on commit 4ebcd0c

Please sign in to comment.