From c848b9cc265f5ac2bcc70bd73e1cc8945d512e34 Mon Sep 17 00:00:00 2001 From: Adelyn Breelove Date: Thu, 13 Dec 2018 14:11:23 -0700 Subject: Add rate limit handling --- lib/rl.ml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/rl.ml (limited to 'lib/rl.ml') diff --git a/lib/rl.ml b/lib/rl.ml new file mode 100644 index 0000000..316abcf --- /dev/null +++ b/lib/rl.ml @@ -0,0 +1,29 @@ +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 update = RouteMap.update +let empty : t = RouteMap.empty +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 -- cgit v1.2.3 From 179d9598fe62e2966471b312fd438e98ff3a272a Mon Sep 17 00:00:00 2001 From: Adelyn Breelove Date: Thu, 13 Dec 2018 15:50:37 -0700 Subject: Fix more dispatch issues --- lib/rl.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/rl.ml') diff --git a/lib/rl.ml b/lib/rl.ml index 316abcf..f0c15be 100644 --- a/lib/rl.ml +++ b/lib/rl.ml @@ -21,8 +21,9 @@ let rl_of_header h = Some { limit; remaining; reset; } | _ -> None -let update = RouteMap.update +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 -- cgit v1.2.3