hansallis / elm-ratelimiter / RateLimiter

This library provides a simple sliding log rate limiter. It should be fed a Posix.Posix using a Time.every subscription.

Constructor

slidingLog : Basics.Int -> TypedTime -> RateLimiter comparable

Create a sliding log rate limiter that allows 5 operations every 5 minutes.

slidingLog 5 (minutes 5)


type RateLimiter comparable

Type representing a rate limiter

Triggering an operation

trigger : RateLimiter comparable -> Time.Posix -> comparable -> (RateLimiter comparable -> response) -> response -> response

Try to execute an operation. If allowed, the accept function is called with an updated RateLimiter instance and then returned. If not allowed, the reject argument is returned.

let
    accept =
        \rl -> ( { model | rateLimiter = rl }, expensiveHttpRequest RequestCompleted } )
in
RateLimiter.trigger rateLimiter identifier accept ( model, Cmd.none )

Defining window size

hours : Basics.Float -> TypedTime

One and a half hours

hours 1.5

minutes : Basics.Float -> TypedTime

5 minutes

minutes 5

seconds : Basics.Float -> TypedTime

10 seconds

seconds 10

days : Basics.Float -> TypedTime

A single day

days 1

weeks : Basics.Float -> TypedTime

A week

weeks 1