Skip to content

Commit 56b2575

Browse files
committed
stream_vdi: Move to using Mtime_clock.counter
Signed-off-by: Andrii Sultanov <[email protected]>
1 parent 62f55d0 commit 56b2575

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

ocaml/xapi/stream_vdi.ml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ let send_one ofd (__context : Context.t) rpc session_id progress refresh_session
216216
(prefix, vdi_ref, _size) =
217217
let size = Db.VDI.get_virtual_size ~__context ~self:vdi_ref in
218218
(* Remember when we last wrote something so that we can work around firewalls which close 'idle' connections *)
219-
let last_transmission_time = ref 0L in
219+
let time_since_transmission = ref (Mtime_clock.counter ()) in
220+
let need_to_retransmit time_since =
221+
Mtime.Span.(is_longer ~than:(5 * s) time_since)
222+
in
220223
let reusable_buffer = Bytes.make (Int64.to_int chunk_size) '\000' in
221224

222225
(* Generic function that reads a chunk of [this_chunk_size] at [offset] and,
@@ -235,7 +238,7 @@ let send_one ofd (__context : Context.t) rpc session_id progress refresh_session
235238
Unix.LargeFile.lseek ifd offset Unix.SEEK_SET |> ignore ;
236239
Unixext.really_read ifd buffer 0 this_chunk_size ;
237240
if write_check buffer first_or_last then (
238-
last_transmission_time := Mtime_clock.now_ns () ;
241+
time_since_transmission := Mtime_clock.counter () ;
239242
write_block ~__context filename buffer ofd this_chunk_size
240243
) ;
241244
made_progress __context progress (Int64.of_int this_chunk_size)
@@ -257,18 +260,14 @@ let send_one ofd (__context : Context.t) rpc session_id progress refresh_session
257260
min (Int64.to_int remaining) (Int64.to_int chunk_size)
258261
in
259262
let last_chunk = this_chunk_size = Int64.to_int remaining in
260-
let now = Mtime_clock.now_ns () in
261-
let time_since_transmission =
262-
Int64.sub now !last_transmission_time
263-
in
264263
(* We always include the first and last blocks *)
265264
let first_or_last = this_chunk_no = 0 || last_chunk in
266265
if
267-
time_since_transmission > 5_000_000_000L
266+
need_to_retransmit (Mtime_clock.count !time_since_transmission)
268267
&& (not first_or_last)
269268
&& timeout_workaround
270269
then (
271-
last_transmission_time := now ;
270+
time_since_transmission := Mtime_clock.counter () ;
272271
let filename = Printf.sprintf "%s/%08d" prefix this_chunk_no in
273272
write_block ~__context filename Bytes.empty ofd 0 ;
274273
(* no progress has been made *)
@@ -353,12 +352,12 @@ let send_one ofd (__context : Context.t) rpc session_id progress refresh_session
353352
min (Int64.to_int chunk_size) (Int64.to_int remaining)
354353
in
355354
let this_chunk_no = Int64.(to_int (div offset chunk_size)) in
356-
let now = Mtime_clock.now_ns () in
357-
let time_since_transmission =
358-
Int64.sub now !last_transmission_time
359-
in
360355
let first_or_last = offset = 0L || remaining <= chunk_size in
361-
if first_or_last || time_since_transmission > 5000000000L then (
356+
if
357+
first_or_last
358+
|| need_to_retransmit
359+
(Mtime_clock.count !time_since_transmission)
360+
then (
362361
actually_write_chunk ~this_chunk_no ~offset ~this_chunk_size
363362
~ifd ~first_or_last
364363
~write_check:(fun _ _ -> true)

0 commit comments

Comments
 (0)