hansallis/elm-ratelimiter/1.1.0

NOTE: the latest version is 2.0.0

RateLimiter

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

Constructor

slidingLog : 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

RateLimiter comparable
-> 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 : Float -> TypedTime

One and a half hours

hours 1.5
minutes : Float -> TypedTime

5 minutes

minutes 5
seconds : Float -> TypedTime

10 seconds

seconds 10
days : Float -> TypedTime

A single day

days 1
weeks : Float -> TypedTime

A week

weeks 1

Keeping time

update : Msg -> RateLimiter comparable -> RateLimiter comparable

Update the current time.

sub : RateLimiter comparable -> Sub Msg

Provides a suitable Time.every subscriptions

command : Cmd Msg

Provides a command to initialize the current time

type Msg

Message to update current time