@@ -50,7 +50,7 @@ data ThrottleConfig = ThrottleConfig
50
50
, _responseBody100ByteCost :: Int
51
51
, _maxBudget :: Int
52
52
-- TODO: charge for time, per second
53
- , _freeRate :: Int
53
+ , _tokenBucketRefillPerSecond :: Int
54
54
, _throttleExpiry :: Seconds
55
55
} deriving stock (Show , Eq )
56
56
@@ -62,7 +62,7 @@ instance ToJSON ThrottleConfig where
62
62
, " requestBody100ByteCost" .= _requestBody100ByteCost o
63
63
, " responseBody100ByteCost" .= _responseBody100ByteCost o
64
64
, " maxBudget" .= _maxBudget o
65
- , " freeRate " .= _freeRate o
65
+ , " tokenBucketRefillPerSecond " .= _tokenBucketRefillPerSecond o
66
66
, " throttleExpiry" .= int @ Seconds @ Int (_throttleExpiry o)
67
67
]
68
68
@@ -72,7 +72,7 @@ instance FromJSON (ThrottleConfig -> ThrottleConfig) where
72
72
<*< requestBody100ByteCost ..: " requestBody100ByteCost" % o
73
73
<*< responseBody100ByteCost ..: " responseBody100ByteCost" % o
74
74
<*< maxBudget ..: " maxBudget" % o
75
- <*< freeRate ..: " freeRate " % o
75
+ <*< tokenBucketRefillPerSecond ..: " tokenBucketRefillPerSecond " % o
76
76
<*< throttleExpiry . (iso (int @ Seconds @ Int ) (int @ Int @ Seconds )) ..: " throttleExpiry" % o
77
77
78
78
instance FromJSON ThrottleConfig where
@@ -81,7 +81,7 @@ instance FromJSON ThrottleConfig where
81
81
_requestBody100ByteCost <- o .: " requestBody100ByteCost"
82
82
_responseBody100ByteCost <- o .: " responseBody100ByteCost"
83
83
_maxBudget <- o .: " maxBudget"
84
- _freeRate <- o .: " freeRate "
84
+ _tokenBucketRefillPerSecond <- o .: " tokenBucketRefillPerSecond "
85
85
_throttleExpiry <- int @ Natural @ Seconds <$> o .: " throttleExpiry"
86
86
return ThrottleConfig {.. }
87
87
@@ -94,7 +94,19 @@ hashWithSalt' :: Hashable a => a -> Int -> Int
94
94
hashWithSalt' = flip hashWithSalt
95
95
96
96
newtype 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
+
98
110
instance Hashable HashableSockAddr where
99
111
hashWithSalt salt (HashableSockAddr sockAddr) = case sockAddr of
100
112
SockAddrInet _port hostAddr ->
@@ -114,7 +126,7 @@ instance Hashable HashableSockAddr where
114
126
. hashWithSalt' sock
115
127
$ salt
116
128
117
- debitOrDie :: Hashable k => TokenLimitMap k -> (Text , k ) -> Int -> IO ()
129
+ debitOrDie :: ( Hashable k ) => TokenLimitMap k -> (Text , k ) -> Int -> IO ()
118
130
debitOrDie tokenLimitMap (name, k) cost = do
119
131
tryDebit cost k tokenLimitMap >>= \ case
120
132
True -> return ()
@@ -149,7 +161,7 @@ throttleMiddleware logfun name ThrottleConfig{..} k =
149
161
limitConfig = defaultLimitConfig
150
162
{ maxBucketTokens = _maxBudget
151
163
, initialBucketTokens = _maxBudget
152
- , bucketRefillTokensPerSecond = _freeRate
164
+ , bucketRefillTokensPerSecond = _tokenBucketRefillPerSecond
153
165
}
154
166
155
167
meterRequest debit request
0 commit comments