Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

body: convert ref into mutable record field #249

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/lwt/lwt_echo_server2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let connection_handler : Unix.sockaddr -> Lwt_unix.file_descr -> unit Lwt.t =
in
(* let (finished, notify) = Lwt.wait () in *)
let rec on_read _request_data ~off:_ ~len:_ =
Body.Writer.flush response_body (fun () ->
Body.Writer.flush response_body (fun _ ->
Body.Reader.schedule_read request_body ~on_eof ~on_read)
and on_eof () =
set_interval
Expand All @@ -93,13 +93,13 @@ let connection_handler : Unix.sockaddr -> Lwt_unix.file_descr -> unit Lwt.t =
let _ =
Body.Writer.write_string response_body "data: some data\n\n"
in
Body.Writer.flush response_body (fun () -> ());
Body.Writer.flush response_body ignore;
true)
(fun () ->
let _ =
Body.Writer.write_string response_body "event: end\ndata: 1\n\n"
in
Body.Writer.flush response_body (fun () ->
Body.Writer.flush response_body (fun _ ->
Body.Writer.close response_body))
in
Body.Reader.schedule_read ~on_read ~on_eof request_body;
Expand Down
13 changes: 5 additions & 8 deletions lib/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ module Reader = struct
; mutable read_scheduled : bool
; mutable on_eof : unit -> unit
; mutable on_read : Bigstringaf.t -> off:int -> len:int -> unit
; buffered_bytes : int ref
; done_reading : int -> unit
}

Expand All @@ -51,7 +50,6 @@ module Reader = struct
; read_scheduled = false
; on_eof = default_on_eof
; on_read = default_on_read
; buffered_bytes = ref 0
; done_reading
}

Expand Down Expand Up @@ -110,12 +108,12 @@ module Writer = struct

type t =
{ faraday : Faraday.t
; buffered_bytes : int ref
; mutable buffered_bytes : int
; writer : Serialize.Writer.t
}

let create buffer ~writer =
{ faraday = Faraday.of_bigstring buffer; buffered_bytes = ref 0; writer }
{ faraday = Faraday.of_bigstring buffer; buffered_bytes = 0; writer }

let create_empty ~writer =
let t = create Bigstringaf.empty ~writer in
Expand Down Expand Up @@ -177,17 +175,16 @@ module Writer = struct
match Faraday.operation faraday with
| `Yield | `Close -> 0
| `Writev iovecs ->
let buffered = t.buffered_bytes in
let iovecs = Httpun_types.IOVec.shiftv iovecs !buffered in
let iovecs = Httpun_types.IOVec.shiftv iovecs t.buffered_bytes in
let lengthv = Httpun_types.IOVec.lengthv iovecs in
let writev_len = if max_bytes < lengthv then max_bytes else lengthv in
buffered := !buffered + writev_len;
t.buffered_bytes <- t.buffered_bytes + writev_len;
let frame_info = Writer.make_frame_info ~max_frame_size stream_id in
Writer.schedule_iovecs writer frame_info ~len:writev_len iovecs;
Writer.flush t.writer (function
| `Closed -> close_and_drain t
| `Written ->
Faraday.shift faraday writev_len;
buffered := !buffered - writev_len);
t.buffered_bytes <- t.buffered_bytes - writev_len);
writev_len
end