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

input-state: use a type to indicate Reqd input state #174

Merged
merged 1 commit into from
Apr 29, 2020
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
18 changes: 15 additions & 3 deletions lib/reqd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ module Response_state = struct
| Streaming of Response.t * [`write] Body.t
end

module Input_state = struct
type t =
| Ready
| Complete
end

type error_handler =
?request:Request.t -> error -> (Headers.t -> [`write] Body.t) -> unit

Expand Down Expand Up @@ -224,8 +230,11 @@ let on_more_output_available t f =
let persistent_connection t =
t.persistent

let requires_input { request_body; _ } =
not (Body.is_closed request_body)
let input_state t : Input_state.t =
if Body.is_closed t.request_body
then Complete
else Ready
;;

let requires_output { response_state; _ } =
match response_state with
Expand All @@ -236,7 +245,10 @@ let requires_output { response_state; _ } =
| Waiting _ -> true

let is_complete t =
not (requires_input t || requires_output t)
match input_state t with
| Complete -> not (requires_output t)
| Ready -> false
;;

let flush_request_body t =
let request_body = request_body t in
Expand Down
28 changes: 17 additions & 11 deletions lib/server_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,30 @@ let advance_request_queue_if_necessary t =
wakeup_writer t;
if Reqd.is_complete reqd
then shutdown t
else if not (Reqd.requires_input reqd)
then shutdown_reader t
else
match Reqd.input_state reqd with
| Ready -> ()
| Complete -> shutdown_reader t
end
end else if Reader.is_closed t.reader
then shutdown t

let _next_read_operation t =
advance_request_queue_if_necessary t;
if is_active t then begin
if is_active t
then (
let reqd = current_reqd_exn t in
if Reqd.requires_input reqd then Reader.next t.reader
else if Reqd.persistent_connection reqd then `Yield
else begin
shutdown_reader t;
Reader.next t.reader
end
end else
Reader.next t.reader
match Reqd.input_state reqd with
| Ready -> Reader.next t.reader
| Complete ->
if Reqd.persistent_connection reqd
then `Yield
else (
shutdown_reader t;
Reader.next t.reader)
)
else Reader.next t.reader
;;

let next_read_operation t =
match _next_read_operation t with
Expand Down