-- | Create a new rate limiter with the specified delay.
--
-- The rate limiter is thread-safe, and can be shared between threads.
newRateLimit
    :: Rational
        -- ^ Delay, in seconds
    -> IO RateLimit
newRateLimit delay' = do
    lock <- newMVar $! negate delay
    return $ RateLimit lock delay
  where
    delay = fromSeconds delay'


-- | Run the given action, pausing as necessary to keep under the rate limit.
newRateLimit
    :: Rational
        -- ^ Delay, in seconds
    -> IO RateLimit
newRateLimit delay' = do
    lock <- newMVar $! negate delay
    return $ RateLimit lock delay
  where
    delay = fromSeconds delay'


-- | Run the given action, pausing as necessary to keep under the rate limit.
newRateLimit delay' = do
    lock <- newMVar $! negate delay
    return $ RateLimit lock delay
  where
    delay = fromSeconds delay'


-- | Run the given action, pausing as necessary to keep under the rate limit.