diff options
| author | Adelyn Breelove <[email protected]> | 2019-02-13 10:58:39 -0700 |
|---|---|---|
| committer | Adelyn Breelove <[email protected]> | 2019-02-13 10:58:39 -0700 |
| commit | afa6b297bd7bf9361727ae78794d9310d3678d13 (patch) | |
| tree | 7b0a4cda613e07585e16a3bdf48135cecb3b5174 /lib/http | |
| parent | Add docs to cache (diff) | |
| download | disml-afa6b297bd7bf9361727ae78794d9310d3678d13.tar.xz disml-afa6b297bd7bf9361727ae78794d9310d3678d13.zip | |
Re-arrange and whatnot
Diffstat (limited to 'lib/http')
| -rw-r--r-- | lib/http/rl.ml | 30 | ||||
| -rw-r--r-- | lib/http/rl.mli | 37 |
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/http/rl.ml b/lib/http/rl.ml new file mode 100644 index 0000000..f0c15be --- /dev/null +++ b/lib/http/rl.ml @@ -0,0 +1,30 @@ +open Core +open Async + +module RouteMap = Map.Make(String) + +type rl = { + limit: int; + remaining: int; + reset: int; +} + +type t = ((rl, read_write) Mvar.t) RouteMap.t + +let rl_of_header h = + let module C = Cohttp.Header in + match C.get h "X-RateLimit-Limit", C.get h "X-RateLimit-Remaining", C.get h "X-RateLimit-Reset" with + | Some lim, Some rem, Some re -> + let limit = Int.of_string lim in + let remaining = Int.of_string rem in + let reset = Int.of_string re in + Some { limit; remaining; reset; } + | _ -> None + +let default = { limit = 1; remaining = 1; reset = 0; } +let empty : t = RouteMap.empty +let update = RouteMap.update +let find = RouteMap.find +let find_exn m s = match find m s with + | Some r -> r + | None -> raise (Not_found_s (String.sexp_of_t s))
\ No newline at end of file diff --git a/lib/http/rl.mli b/lib/http/rl.mli new file mode 100644 index 0000000..973f02f --- /dev/null +++ b/lib/http/rl.mli @@ -0,0 +1,37 @@ +(** Internal ratelimit route mapping. *) + +open Core +open Async + +(** Type for mapping route -> {!rl}. *) +module RouteMap : module type of Map.Make(String) + +(** Type representing ratelimit information. *) +type rl = { + limit: int; + remaining: int; + reset: int; +} + +(** Type representing the specific case of {!RouteMap}. *) +type t = ((rl, read_write) Mvar.t) RouteMap.t + +(** Converts Cohttp header data into ratelimit information. + @return Some of ratelimit information or None on bad headers +*) +val rl_of_header : Cohttp.Header.t -> rl option + +(** Default for type rl. Used for prepopulating routes. *) +val default : rl + +(** Empty ratelimit route map. *) +val empty : t + +(** Analogous to {!RouteMap.update}. *) +val update : 'a RouteMap.t -> string -> f:('a option -> 'a) -> 'a RouteMap.t + +(** Analogous to {!RouteMap.find}. *) +val find : 'a RouteMap.t -> string -> 'a option + +(** Analogous to {!RouteMap.find_exn}. *) +val find_exn : 'a RouteMap.t -> string -> 'a
\ No newline at end of file |