Per-IP sliding window rate limiter
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-rate-limit: bind handle_request callbacks

The example referenced [process_request] / [respond_429] as free
identifiers; pass them as parameters to [handle_request] along with
[~now] (avoiding [Unix.gettimeofday]). Switch [Printf] to [Fmt.pr]
and add [fmt] to the mdx libraries.

+4 -5
+3 -4
README.md
··· 45 45 ```ocaml 46 46 let limiter = Rate_limit.v ~max_requests:100 ~window_seconds:60.0 () 47 47 48 - let handle_request ~ip = 49 - let now = Unix.gettimeofday () in 48 + let handle_request ~ip ~now ~process_request ~respond_429 = 50 49 match Rate_limit.check_and_record limiter ~ip ~now with 51 50 | Rate_limit.Allowed { remaining } -> 52 - Printf.printf "Allowed, %d requests remaining\n" remaining; 51 + Fmt.pr "Allowed, %d requests remaining@." remaining; 53 52 process_request () 54 53 | Rate_limit.Rejected { retry_after } -> 55 - Printf.printf "Rate limited, retry after %.0f seconds\n" retry_after; 54 + Fmt.pr "Rate limited, retry after %.0f seconds@." retry_after; 56 55 respond_429 ~retry_after:(int_of_float retry_after) 57 56 ``` 58 57
+1 -1
dune
··· 4 4 5 5 (mdx 6 6 (files README.md) 7 - (libraries rate-limit)) 7 + (libraries rate-limit fmt))