wai-middleware-throttle 0.1.0.1 → 0.2.0.0
raw patch · 3 files changed
+25/−30 lines, 3 filesdep +containersdep −textdep −unordered-containersdep ~hlintsetup-changedPVP ok
version bump matches the API change (PVP)
Dependencies added: containers
Dependencies removed: text, unordered-containers
Dependency ranges changed: hlint
API changes (from Hackage documentation)
Files
- Network/Wai/Middleware/Throttle.hs +13/−23
- Setup.hs +9/−1
- wai-middleware-throttle.cabal +3/−6
Network/Wai/Middleware/Throttle.hs view
@@ -17,7 +17,7 @@ -- @ -- main = do -- st <- initThrottler--- let payload = "{ \"api\", \"return data\" }"+-- let payload = "{ \"api\": \"return data\" }" -- app = throttle defaultThrottleSettings st -- $ \_ f -> f (responseLBS status200 [] payload) -- Warp.run 3000 app@@ -43,13 +43,10 @@ , defaultThrottleSettings ) where -import Control.Applicative import Control.Concurrent.STM import Control.Concurrent.TokenBucket import Control.Monad (liftM)-import qualified Data.ByteString.Char8 as BS-import qualified Data.HashMap.Strict as H-import qualified Data.Text as T+import qualified Data.IntMap as IM import GHC.Word (Word64) import qualified Network.HTTP.Types.Status as Http import Network.Socket@@ -60,7 +57,7 @@ -- | A 'HashMap' mapping the remote IP address to a 'TokenBucket'-data ThrottleState = ThrottleState !(H.HashMap T.Text TokenBucket)+data ThrottleState = ThrottleState !(IM.IntMap TokenBucket) -- | Settings which control various behaviors in the middleware.@@ -85,8 +82,7 @@ -- Maximum size of the address cache in MB (similar to nginx) --- -- You can store approximately 160,000 addresses in 10MB with- -- \$binary_remote_addr.+ -- With nginx, can store approximately 160,000 addresses in 10MB. -- , throttleCacheSize :: !Integer -- | Burst rate@@ -95,7 +91,7 @@ initThrottler :: IO WaiThrottle-initThrottler = liftM WT $ newTVarIO $ ThrottleState H.empty+initThrottler = liftM WT $ newTVarIO $ ThrottleState IM.empty -- | Default settings to throttle requests.@@ -110,20 +106,15 @@ , onThrottled = onThrottled' } where- bshow = BS.pack . show- -- remaining = bshow (if 5000 - c < 0- -- then 0- -- else 5000 - c)- onThrottled' rt =+ onThrottled' _ = responseLBS Http.status429 [ ("Content-Type", "application/json")- -- , ("X-RateLimit-Limit", "5000")+ -- , ("X-RateLimit-Limit", limit) -- , ("X-RateLimit-Remaining", remaining)- , ("X-RateLimit-Reset",- bshow (fromIntegral rt / 1000000.0 :: Double))+ -- , ("X-RateLimit-Reset",+ -- bshow (fromIntegral rt / 1000000.0 :: Double)) ]- -- match YesodAuth error message renderer "{\"message\":\"Too many requests.\"}" @@ -151,10 +142,9 @@ where throttleReq = do - let SockAddrInet _ host = remoteHost req- remoteAddr <- T.pack <$> inet_ntoa host+ let SockAddrInet _ remoteAddr = remoteHost req throttleState <- atomically $ readTVar tmap- (tst, success) <- throttleReq' remoteAddr throttleState+ (tst, success) <- throttleReq' (fromIntegral remoteAddr) throttleState -- write the throttle state back atomically $ writeTVar tmap (ThrottleState tst)@@ -166,7 +156,7 @@ invRate = toInvRate (fromInteger throttleRate :: Double) burst = fromInteger throttleBurst - bucket <- maybe newTokenBucket return $ H.lookup remoteAddr m+ bucket <- maybe newTokenBucket return $ IM.lookup remoteAddr m remaining <- tokenBucketTryAlloc1 bucket burst invRate - return (H.insert remoteAddr bucket m, remaining)+ return (IM.insert remoteAddr bucket m, remaining)
Setup.hs view
@@ -1,2 +1,10 @@ import Distribution.Simple-main = defaultMain+import Distribution.Simple.Program++main :: IO ()+main = defaultMainWithHooks simpleUserHooks+ {+ -- cabal-install is used during testing to run+ -- and verify haddock documentation.+ hookedPrograms = [ simpleProgram "cabal" ]+ }
wai-middleware-throttle.cabal view
@@ -1,6 +1,6 @@ name: wai-middleware-throttle-version: 0.1.0.1+version: 0.2.0.0 license: BSD3 license-file: LICENSE author: Christopher Reichert@@ -27,9 +27,7 @@ default-language: Haskell2010 build-depends: base >= 4.6 && < 5.0 , wai >= 3.0- , text- , unordered-containers- , bytestring+ , containers , transformers , network , http-types@@ -55,7 +53,6 @@ , hspec >= 1.3 , http-types , HUnit- , unordered-containers , stm , transformers @@ -77,4 +74,4 @@ type: exitcode-stdio-1.0 default-language: Haskell2010 build-depends: base- , hlint == 1.8.*+ , hlint