Skip to content

Commit 7c2ec1e

Browse files
committed
fix HashableSockAddr Eq instance
1 parent 4c62d97 commit 7c2ec1e

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/Chainweb/Chainweb/Configuration.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ defaultServiceApiConfig = ServiceApiConfig
252252
, Throttle._requestBody100ByteCost = 1
253253
, Throttle._responseBody100ByteCost = 2
254254
, Throttle._maxBudget = 50_000
255-
, Throttle._freeRate = 50_000
255+
, Throttle._tokenBucketRefillPerSecond = 1_000
256256
, Throttle._throttleExpiry = 30
257257
}
258258
}

src/Chainweb/Utils/Throttle.hs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7878
instance 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
9494
hashWithSalt' = flip hashWithSalt
9595

9696
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+
98110
instance 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 ()
118130
debitOrDie 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

src/P2P/Node/Configuration.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ defaultP2pConfiguration = P2pConfiguration
134134
, Throttle._requestBody100ByteCost = 1
135135
, Throttle._responseBody100ByteCost = 2
136136
, Throttle._maxBudget = 35_000
137-
, Throttle._freeRate = 35_000
137+
, Throttle._tokenBucketRefillPerSecond = 750
138138
, Throttle._throttleExpiry = 30
139139
}
140140
}

test/unit/Chainweb/Test/Throttle.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tests = testGroup "Chainweb.Test.Throttle"
3333
, _requestBody100ByteCost = 0
3434
, _responseBody100ByteCost = 0
3535
, _maxBudget = 1
36-
, _freeRate = 1
36+
, _tokenBucketRefillPerSecond = 1
3737
, _throttleExpiry = Seconds 20
3838
}
3939
$ \req manager -> do
@@ -49,7 +49,7 @@ tests = testGroup "Chainweb.Test.Throttle"
4949
, _requestBody100ByteCost = 1
5050
, _responseBody100ByteCost = 0
5151
, _maxBudget = 2
52-
, _freeRate = 1
52+
, _tokenBucketRefillPerSecond = 1
5353
, _throttleExpiry = Seconds 20
5454
}
5555
$ \req manager -> do
@@ -63,7 +63,7 @@ tests = testGroup "Chainweb.Test.Throttle"
6363
, _requestBody100ByteCost = 0
6464
, _responseBody100ByteCost = 1
6565
, _maxBudget = 2
66-
, _freeRate = 1
66+
, _tokenBucketRefillPerSecond = 1
6767
, _throttleExpiry = Seconds 20
6868
}
6969
$ \req manager -> do

0 commit comments

Comments
 (0)