From 2a264307c2ef0eefb6bdc790b53310751d47214e Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Fri, 29 Nov 2024 12:15:44 -0800 Subject: [PATCH 1/2] refactor: unify {input,output}_state into Io_state --- lib/client_connection.ml | 6 +++--- lib/input_state.ml | 4 ---- lib/output_state.ml | 4 ---- lib/reqd.ml | 2 +- lib/respd.ml | 6 +++--- lib/response_state.ml | 4 ++-- lib/server_connection.ml | 8 ++++---- 7 files changed, 13 insertions(+), 21 deletions(-) delete mode 100644 lib/input_state.ml delete mode 100644 lib/output_state.ml diff --git a/lib/client_connection.ml b/lib/client_connection.ml index 5f85c11..46b21a7 100644 --- a/lib/client_connection.ml +++ b/lib/client_connection.ml @@ -206,7 +206,7 @@ let rec _next_read_operation t = let respd = current_respd_exn t in match Respd.input_state respd with | Wait -> `Yield - | Ready -> Reader.next t.reader + | Ready -> Reader.next t.reader | Complete -> _final_read_operation_for t respd ) @@ -217,7 +217,7 @@ and _final_read_operation_for t respd = Reader.next t.reader; ) else ( match Respd.output_state respd with - | Waiting | Ready -> `Yield + | Wait | Ready -> `Yield | Complete -> match Reader.next t.reader with | `Error _ | `Read as operation -> @@ -271,7 +271,7 @@ let rec _next_write_operation t = ) else ( let respd = current_respd_exn t in match Respd.output_state respd with - | Waiting -> `Yield + | Wait -> `Yield | Ready -> Respd.flush_request_body respd; Writer.next t.writer diff --git a/lib/input_state.ml b/lib/input_state.ml deleted file mode 100644 index cc9ce4c..0000000 --- a/lib/input_state.ml +++ /dev/null @@ -1,4 +0,0 @@ -type t = - | Ready - | Wait - | Complete diff --git a/lib/output_state.ml b/lib/output_state.ml deleted file mode 100644 index 5f066b7..0000000 --- a/lib/output_state.ml +++ /dev/null @@ -1,4 +0,0 @@ -type t = - | Waiting - | Ready - | Complete diff --git a/lib/reqd.ml b/lib/reqd.ml index 3775711..ef3c54e 100644 --- a/lib/reqd.ml +++ b/lib/reqd.ml @@ -256,7 +256,7 @@ let error_code t = let persistent_connection t = t.persistent -let input_state t : Input_state.t = +let input_state t : Io_state.t = match t.response_state with | Upgrade _ -> Ready | _ -> diff --git a/lib/respd.ml b/lib/respd.ml index 5ba1d33..7c0499e 100644 --- a/lib/respd.ml +++ b/lib/respd.ml @@ -93,7 +93,7 @@ let close_response_body t = Body.Reader.close response_body | Upgraded _ -> t.state <- Closed -let input_state t : Input_state.t = +let input_state t : Io_state.t = match t.state with | Uninitialized | Awaiting_response -> Ready @@ -108,14 +108,14 @@ let input_state t : Input_state.t = | Upgraded _ | Closed -> Complete -let output_state { request_body; state; writer; _ } : Output_state.t = +let output_state { request_body; state; writer; _ } : Io_state.t = match state with | Upgraded _ -> (* XXX(anmonteiro): Connections that have been upgraded "require output" * forever, but outside the HTTP layer, meaning they're permanently * "yielding". For now they need to be explicitly shutdown in order to * transition the response descriptor to the `Closed` state. *) - Waiting + Wait | state -> if Writer.is_closed writer then Complete else if state = Uninitialized || Body.Writer.requires_output request_body diff --git a/lib/response_state.ml b/lib/response_state.ml index 92dfa38..a88f305 100644 --- a/lib/response_state.ml +++ b/lib/response_state.ml @@ -4,12 +4,12 @@ type t = | Streaming of Response.t * Body.Writer.t | Upgrade of Response.t * (unit -> unit) -let output_state t ~writer : Output_state.t = +let output_state t ~writer : Io_state.t = match t with | Fixed _ -> Complete | Waiting -> if Serialize.Writer.is_closed writer then Complete - else Waiting + else Wait | Streaming(_, response_body) -> if Serialize.Writer.is_closed writer then Complete else if Body.Writer.requires_output response_body diff --git a/lib/server_connection.ml b/lib/server_connection.ml index f51c650..e737627 100644 --- a/lib/server_connection.ml +++ b/lib/server_connection.ml @@ -259,7 +259,7 @@ let rec _next_read_operation t = and there are still bytes remaining to be read in the request body. *) Reader.next t.reader - | Waiting | Ready -> + | Wait | Ready -> (* `Wait` signals that we should add backpressure to the read channel, * meaning the reader should tell the runtime to yield. * @@ -281,7 +281,7 @@ and _final_read_operation_for t reqd = Reader.next t.reader; ) else match Reqd.output_state reqd with - | Waiting | Ready -> `Yield + | Wait | Ready -> `Yield | Complete -> (* The "final read" operation for a request descriptor that is * `Complete` from both input and output perspectives needs to account @@ -340,7 +340,7 @@ let rec _next_write_operation t = Writer.next t.writer | Error { response_state; _ } -> match Response_state.output_state response_state ~writer:t.writer with - | Waiting -> `Yield + | Wait -> `Yield | Ready -> flush_response_error_body response_state; Writer.next t.writer @@ -350,7 +350,7 @@ let rec _next_write_operation t = ) else ( let reqd = current_reqd_exn t in match Reqd.output_state reqd with - | Waiting -> Writer.next t.writer + | Wait -> Writer.next t.writer | Ready -> Reqd.flush_response_body reqd; Writer.next t.writer From fc6c795c97812edc8555cacaf8e73e4a73d6531a Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Fri, 29 Nov 2024 12:24:31 -0800 Subject: [PATCH 2/2] add file --- lib/io_state.ml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 lib/io_state.ml diff --git a/lib/io_state.ml b/lib/io_state.ml new file mode 100644 index 0000000..cc9ce4c --- /dev/null +++ b/lib/io_state.ml @@ -0,0 +1,4 @@ +type t = + | Ready + | Wait + | Complete