nationstates 0.2.0.1 → 0.2.0.2
raw patch · 3 files changed
+16/−13 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- NationStates.RateLimit: newRateLimit :: TimeSpec -> IO RateLimit
+ NationStates.RateLimit: newRateLimit :: Rational -> IO RateLimit
Files
- NationStates.hs +1/−3
- NationStates/RateLimit.hs +14/−9
- nationstates.cabal +1/−1
NationStates.hs view
@@ -24,11 +24,9 @@ -> IO Context newContext userAgent = do man <- newManager tlsManagerSettings- limit <- newRateLimit delay+ limit <- newRateLimit 0.6 return Context { contextManager = man, contextRateLimit = rateLimit limit, contextUserAgent = userAgent }- where- delay = 600 * 1000 * 1000 -- 0.6 seconds
NationStates/RateLimit.hs view
@@ -21,25 +21,30 @@ -- | Create a new rate limiter with the specified delay. -- -- The rate limiter is thread-safe, and can be shared between threads.-newRateLimit :: TimeSpec -> IO RateLimit-newRateLimit delay = do+newRateLimit+ :: Rational+ -- ^ Delay, in seconds+ -> IO RateLimit+newRateLimit delay' = do lock <- newMVar $! negate delay return RateLimit { rateLock = lock, rateDelay = delay }+ where+ delay = fromInteger . ceiling $ delay' * 1000 * 1000 * 1000 -- | Run the given action, pausing as necessary to keep under the rate limit. rateLimit :: RateLimit -> IO a -> IO a rateLimit RateLimit { rateLock = lock, rateDelay = delay } action =- mask $ \restore -> do- prev <- takeMVar lock- now <- getTime Monotonic- threadDelay' $ prev + delay - now- result <- restore action- putMVar lock =<< getTime Monotonic- return result+ bracketOnError+ (takeMVar lock)+ (tryPutMVar lock)+ (\prev -> do+ now <- getTime Monotonic+ threadDelay' $ prev + delay - now+ action `finally` (putMVar lock =<< getTime Monotonic)) threadDelay' :: TimeSpec -> IO ()
nationstates.cabal view
@@ -1,5 +1,5 @@ name: nationstates-version: 0.2.0.1+version: 0.2.0.2 synopsis: NationStates API client description: NationStates API client