Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a prelude to #159 which introduces upgrade requests, and much of the diff is identical, with a few trivial changes in `Reqd` and a few major changes in `Server_connection`. The goals here are: 1. Make `Reqd` return better types that encapsulate its state instead of requiring the user to probe it with `requires_<input|output>` and `is_complete`. 2. Try to make queue management easier to reason about by folding bits of logic from `advance_request_queue_if_necessary` into `next_read_operation` and `next_write_operation` such that we only perform side-effects when the operation in question demands it. One of the ways I tried to make this easier to reason about was to make the `yield_<reader|writer>` and `next_<read|write>_operation` functions very parallel. For one, the extra logic in `yield_writer` was puzzling. Ideally, if you're calling `yield_writer`, you're doing so because you just called `next_action` and were told to `Yield`, so all of the conditions being checked should not be possible. Looking at the next-operation functions, they both start out with a short-circuit for shutting down when the server can no longer make progress (reader is closed and queue is empty). This doesn't feel like it belongs here. Perhaps this check should be part of `advance_request_queue` with some extra logic triggering in `shutdown_reader`? After that, the next-operation functions use some very simple probing of the input/output state of `Reqd` to determine what to do next. Only in the case of `Complete` do we move into a separate function (to make it easier to read): `_final_<read|write>_operation`. In these functions, we decide if we should shutdown the respective reader/writer or consider the `reqd` complete and move it off the queue. When we do shift it off, we recursively ask for the next operation given the new queue state. In all cases, before we return the result, we wakeup the other side so that it too can evaluate the next operation given the new queue state. Though on the surface, these pieces feel fairly straightforward, there are still a slew of re-entrancy bugs to consider. I think there are two things that we can do to make this drastically easier to manage: 1. We call `t.request_handler` in two places, and this is mostly because we want to keep the invariant that the head of the request queue has already been passed off to the handler. I feel like splitting this up into a simple queue of unhandled requests and a [Reqd.t option] that represents the current request would be easier to manage. 2. It would be nice to schedule calls. Things like waking up the writer before you let the read loop know its next operation just immediately makes my mind fall apart and lose track of state. There's a fairly obvious solution of asking for a `schedule : (unit -> unit) -> unit` function from the runtime that promises to not call the thunk synchronously, but rather waits until it is outside of the read and write loops. But maybe we can solve it using what we have now, like establishing a contract that when the reader/writer is woken up, they must schedule their work for a fresh call stack and not immediately ask for operations. I added a `Queue.clear` to shutdown, not because it was necessary in any sense, but because it was part of `advance_request_queue_if_necessary`, which could have come into play in certain situations where `shutdown` was called from the runtime (e.g., in case of some exception). I would like to note that despite the fact that all tests pass, I have very little confidence in this being correct right now and would like to do some further testing within the actual runtimes.
- Loading branch information