From 833ae530fe8de62126db97aa6bc30caa95851f0e Mon Sep 17 00:00:00 2001 From: Levi Roth Date: Sun, 8 Dec 2024 15:06:52 -0500 Subject: [PATCH] WIP connection state machine --- reddit_api_kernel/connection_state_machine.ml | 0 .../connection_state_machine.mli | 46 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 reddit_api_kernel/connection_state_machine.ml create mode 100644 reddit_api_kernel/connection_state_machine.mli diff --git a/reddit_api_kernel/connection_state_machine.ml b/reddit_api_kernel/connection_state_machine.ml new file mode 100644 index 00000000..e69de29b diff --git a/reddit_api_kernel/connection_state_machine.mli b/reddit_api_kernel/connection_state_machine.mli new file mode 100644 index 00000000..645722ca --- /dev/null +++ b/reddit_api_kernel/connection_state_machine.mli @@ -0,0 +1,46 @@ +open! Core + +module When_to_send : sig + type t = + | Now + | Check_after_receiving_response + | After of Time_ns.t +end + +type t [@@deriving sexp_of] + +(** {1 Constructors} *) +val create : rate_limiter:Rate_limiter_state_machine.t -> t + +val by_headers : t +val with_minimum_delay : delay:Time_ns.Span.t -> t +val combine : t list -> t + +(** {1 Events} *) + +(* TODO: Move *) +module Access_token : sig + type t = + { token : string + ; expiration : Time_ns.t + } +end + +val send_request + : t + -> now:Time_ns.t + -> t * [ `Send_it | `Get_access_token | `Wait_until of Time_ns.t ] + +val got_access_token : t -> Access_token.t -> t + +(** -- *) + +(** [sent_request_unchecked] should called immediately after sending a request. + It is the caller's responsibility to first call {!wait_until}. *) +val sent_request_unchecked : t -> now:Time_ns.t -> t + +val received_response : t -> Cohttp.Response.t -> t + +(** {1 Accessors} *) + +val wait_until : t -> When_to_send.t