packages feed

wai-middleware-throttle 0.2.0.2 → 0.2.1.0

raw patch · 3 files changed

+17/−70 lines, 3 filesdep −hlintdep ~basePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies removed: hlint

Dependency ranges changed: base

API changes (from Hackage documentation)

- Network.Wai.Middleware.Throttle: instance Eq Address
- Network.Wai.Middleware.Throttle: instance Hashable Address
- Network.Wai.Middleware.Throttle: instance Ord Address
- Network.Wai.Middleware.Throttle: isThrottled :: ThrottleSettings -> !(Request -> IO Bool)
- Network.Wai.Middleware.Throttle: onThrottled :: ThrottleSettings -> !(Word64 -> Response)
- Network.Wai.Middleware.Throttle: throttleBurst :: ThrottleSettings -> !Integer
- Network.Wai.Middleware.Throttle: throttleRate :: ThrottleSettings -> !Integer
+ Network.Wai.Middleware.Throttle: [isThrottled] :: ThrottleSettings -> !(Request -> IO Bool)
+ Network.Wai.Middleware.Throttle: [onThrottled] :: ThrottleSettings -> !(Word64 -> Response)
+ Network.Wai.Middleware.Throttle: [throttleBurst] :: ThrottleSettings -> !Integer
+ Network.Wai.Middleware.Throttle: [throttlePeriod] :: ThrottleSettings -> !Integer
+ Network.Wai.Middleware.Throttle: [throttleRate] :: ThrottleSettings -> !Integer
+ Network.Wai.Middleware.Throttle: instance Data.Hashable.Class.Hashable Network.Wai.Middleware.Throttle.Address
+ Network.Wai.Middleware.Throttle: instance GHC.Classes.Eq Network.Wai.Middleware.Throttle.Address
+ Network.Wai.Middleware.Throttle: instance GHC.Classes.Ord Network.Wai.Middleware.Throttle.Address
- Network.Wai.Middleware.Throttle: ThrottleSettings :: !(Request -> IO Bool) -> !(Word64 -> Response) -> !Integer -> !Integer -> ThrottleSettings
+ Network.Wai.Middleware.Throttle: ThrottleSettings :: !(Request -> IO Bool) -> !(Word64 -> Response) -> !Integer -> !Integer -> !Integer -> ThrottleSettings

Files

Network/Wai/Middleware/Throttle.hs view
@@ -23,9 +23,9 @@ --   Warp.run 3000 app -- @ +{-# LANGUAGE CPP               #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards   #-}-{-# LANGUAGE CPP   #-}  module Network.Wai.Middleware.Throttle ( @@ -44,12 +44,12 @@     , defaultThrottleSettings     ) where +import           Control.Applicative            ((<$>)) import           Control.Concurrent.STM import           Control.Concurrent.TokenBucket-import           Control.Monad                  (liftM,join)-import           Data.Functor                   ((<$>))+import           Control.Monad                  (join, liftM) import           Data.Function                  (on)-import           Data.Hashable                  (hash,hashWithSalt,Hashable)+import           Data.Hashable                  (Hashable, hash, hashWithSalt) import qualified Data.IntMap                    as IM import           Data.List                      (unionBy) import           GHC.Word                       (Word64)@@ -100,29 +100,20 @@ data ThrottleSettings = ThrottleSettings     {       -- | Determines whether the 'Request' is throttled-      isThrottled   :: !(Request -> IO Bool)+      isThrottled    :: !(Request -> IO Bool)        -- | Function to run when the request is throttled.       --       -- The first argument is a 'Word64' containing the amount       -- of microseconds until the next retry should be attempted-    , onThrottled   :: !(Word64 -> Response)--      -- Zone name-      ---      -- TODO use list of zones (rules)-      -- , throttleZone      :: !T.Text+    , onThrottled    :: !(Word64 -> Response)        -- | Rate-    , throttleRate  :: !Integer  -- requests / second--      -- Maximum size of the address cache in MB (similar to nginx)-      ---      -- With nginx, can store approximately 160,000 addresses in 10MB.-      -- , throttleCacheSize :: !Integer+    , throttleRate   :: !Integer  -- requests / throttlePeriod+    , throttlePeriod :: !Integer -- microseconds        -- | Burst rate-    , throttleBurst :: !Integer+    , throttleBurst  :: !Integer     }  @@ -135,9 +126,8 @@ defaultThrottleSettings     = ThrottleSettings {         isThrottled         = return . const True-        -- , throttleZone        = "" -- empty zone-      , throttleRate        = 1  -- req / sec-        -- , throttleCacheSize   = 10 -- 10M address cache+      , throttleRate        = 1  -- req / throttlePeriod+      , throttlePeriod      = 10^6 -- microseconds       , throttleBurst       = 1  -- concurrent requests       , onThrottled         = onThrottled'       }@@ -146,10 +136,6 @@       responseLBS         Http.status429         [ ("Content-Type", "application/json")-          -- , ("X-RateLimit-Limit", limit)-          -- , ("X-RateLimit-Remaining", remaining)-          -- , ("X-RateLimit-Reset",-          --      bshow (fromIntegral rt / 1000000.0 :: Double))         ]         "{\"message\":\"Too many requests.\"}" @@ -159,9 +145,9 @@ -- Uses a 'Request's 'remoteHost' function to resolve the -- remote IP address. throttle :: ThrottleSettings-            -> WaiThrottle-            -> Application-            -> Application+         -> WaiThrottle+         -> Application+         -> Application throttle ThrottleSettings{..} (WT tmap) app req respond = do      -- determine whether the request needs throttling@@ -188,7 +174,8 @@      throttleReq' remoteAddr (ThrottleState m) = do -      let toInvRate r = round (1e6 / r)+      let toInvRate r = round (period / r)+          period     = (fromInteger throttlePeriod :: Double)           invRate     = toInvRate (fromInteger throttleRate :: Double)           burst       = fromInteger throttleBurst 
− test/HLint.hs
@@ -1,31 +0,0 @@--------------------------------------------------------------------------------- |--- Module      : HLint--- Description : Lint Testing Coverage--- Copyright   : (c) 2015 Christopher Reichert--- License     : BSD3--- Maintainer  : Christopher Reichert <creichert07@gmail.com>--- Stability   : testing--- Portability : POSIX--module Main (main) where--import           Language.Haskell.HLint (hlint)-import           System.Exit            (exitFailure, exitSuccess)---main :: IO ()-main = do-    hints <- hlint arguments-    print hints-    if null hints-       then exitSuccess-       else exitFailure---arguments :: [String]-arguments =-      [-        "Network"-      , "test"-      ]
wai-middleware-throttle.cabal view
@@ -1,6 +1,6 @@  name:                wai-middleware-throttle-version:             0.2.0.2+version:             0.2.1.0 license:             BSD3 license-file:        LICENSE author:              Christopher Reichert@@ -60,12 +60,3 @@                      , HUnit                      , stm                      , transformers---test-suite hlint-  main-is:          test/HLint.hs-  ghc-options:      -Wall -threaded-  type:             exitcode-stdio-1.0-  default-language: Haskell2010-  build-depends:    base-                  , hlint