@@ -50,7 +50,7 @@ data ThrottleConfig = ThrottleConfig
5050 , _responseBody100ByteCost :: Int
5151 , _maxBudget :: Int
5252 -- TODO: charge for time, per second
53- , _freeRate :: Int
53+ , _tokenBucketRefillPerSecond :: Int
5454 , _throttleExpiry :: Seconds
5555 } deriving stock (Show , Eq )
5656
@@ -62,7 +62,7 @@ instance ToJSON ThrottleConfig where
6262 , " requestBody100ByteCost" .= _requestBody100ByteCost o
6363 , " responseBody100ByteCost" .= _responseBody100ByteCost o
6464 , " maxBudget" .= _maxBudget o
65- , " freeRate " .= _freeRate o
65+ , " tokenBucketRefillPerSecond " .= _tokenBucketRefillPerSecond o
6666 , " throttleExpiry" .= int @ Seconds @ Int (_throttleExpiry o)
6767 ]
6868
@@ -72,7 +72,7 @@ instance FromJSON (ThrottleConfig -> ThrottleConfig) where
7272 <*< requestBody100ByteCost ..: " requestBody100ByteCost" % o
7373 <*< responseBody100ByteCost ..: " responseBody100ByteCost" % o
7474 <*< maxBudget ..: " maxBudget" % o
75- <*< freeRate ..: " freeRate " % o
75+ <*< tokenBucketRefillPerSecond ..: " tokenBucketRefillPerSecond " % o
7676 <*< throttleExpiry . (iso (int @ Seconds @ Int ) (int @ Int @ Seconds )) ..: " throttleExpiry" % o
7777
7878instance FromJSON ThrottleConfig where
@@ -81,7 +81,7 @@ instance FromJSON ThrottleConfig where
8181 _requestBody100ByteCost <- o .: " requestBody100ByteCost"
8282 _responseBody100ByteCost <- o .: " responseBody100ByteCost"
8383 _maxBudget <- o .: " maxBudget"
84- _freeRate <- o .: " freeRate "
84+ _tokenBucketRefillPerSecond <- o .: " tokenBucketRefillPerSecond "
8585 _throttleExpiry <- int @ Natural @ Seconds <$> o .: " throttleExpiry"
8686 return ThrottleConfig {.. }
8787
@@ -94,7 +94,19 @@ hashWithSalt' :: Hashable a => a -> Int -> Int
9494hashWithSalt' = flip hashWithSalt
9595
9696newtype HashableSockAddr = HashableSockAddr SockAddr
97- deriving newtype Eq
97+ deriving newtype (Show )
98+
99+ instance Eq HashableSockAddr where
100+ HashableSockAddr sockAddr1 == HashableSockAddr sockAddr2 = case (sockAddr1, sockAddr2) of
101+ (SockAddrInet _port1 hostAddr1, SockAddrInet _port2 hostAddr2) ->
102+ -- constructor port not used deliberately, requests can come from different ports
103+ hostAddr1 == hostAddr2
104+ (SockAddrInet6 _port1 flowInfo1 hostAddr1 scopeId1, SockAddrInet6 _port2 flowInfo2 hostAddr2 scopeId2) ->
105+ flowInfo1 == flowInfo2 && hostAddr1 == hostAddr2 && scopeId1 == scopeId2
106+ (SockAddrUnix sock1, SockAddrUnix sock2) ->
107+ sock1 == sock2
108+ _ -> False
109+
98110instance Hashable HashableSockAddr where
99111 hashWithSalt salt (HashableSockAddr sockAddr) = case sockAddr of
100112 SockAddrInet _port hostAddr ->
@@ -114,7 +126,7 @@ instance Hashable HashableSockAddr where
114126 . hashWithSalt' sock
115127 $ salt
116128
117- debitOrDie :: Hashable k => TokenLimitMap k -> (Text , k ) -> Int -> IO ()
129+ debitOrDie :: ( Hashable k ) => TokenLimitMap k -> (Text , k ) -> Int -> IO ()
118130debitOrDie tokenLimitMap (name, k) cost = do
119131 tryDebit cost k tokenLimitMap >>= \ case
120132 True -> return ()
@@ -149,7 +161,7 @@ throttleMiddleware logfun name ThrottleConfig{..} k =
149161 limitConfig = defaultLimitConfig
150162 { maxBucketTokens = _maxBudget
151163 , initialBucketTokens = _maxBudget
152- , bucketRefillTokensPerSecond = _freeRate
164+ , bucketRefillTokensPerSecond = _tokenBucketRefillPerSecond
153165 }
154166
155167 meterRequest debit request
0 commit comments