This commit is contained in:
Evgeny @ SimpleX Chat committed 2026-10-13 10:42:47 +00:00
1 parent 09fa5b6449
commit 0fd07d99d6
3 files changed
+23 -19

No files matched your search

+12 -10
View File
@@ -147,22 +147,22 @@ Functions:
3. The counter is read from `current` with `TM.lookupIO`.
4. An absent counter is looked up or inserted with the evaluated amount in one transaction.
5. A present counter is increased with `modifyTVar'`.
The amount is positive at every call site.
- `closeUsagePeriod :: UsageCounter c => UsageStats c -> IO ()`, per client:
1. In one transaction:
- `current` is set to an empty map; the counts of its counters are the closed counts;
- `previous` is set to the closed counts.
2. The entry is deleted when the closed counts are empty.
- `closeUsagePeriod :: UsageCounter c => UsageStats c -> IO ()`, per client, in one transaction:
- `current` is set to an empty map; the counts of its counters are the closed counts;
- `previous` is set to the closed counts;
- the entry is deleted when the closed counts are empty.
Then, per counter, the histogram is updated with the closed counts:
- `bucketCounts`: 1 is added at the bucket index of each count
- `usageSum`: the counts are added
`lastPeriodMax` is set to the largest closed count, 0 without closed counts.
- `usagePeriodThread :: UsageCounter c => Int64 -> UsageStats c -> IO ()`: `closeUsagePeriod` every `period` seconds.
- `topClients :: UsageCounter c => UsageStats c -> c -> Int -> IO [(ClientAddr, Int, Int)]`:
- address, previous count, current count
- both counts of a client read in one transaction
- sorted by the sum of both counts, largest first
- clients with sum 0 excluded
- limited to the given number
@@ -171,7 +171,7 @@ Functions:
Counts lost in `closeUsagePeriod`:
- a count added after the closing transaction to an entry deleted after it;
- a count added after the closing transaction to an entry deleted by it;
- a count added after the closing transaction to a counter read with `TM.lookupIO` before it.
## Prometheus
@@ -340,7 +340,9 @@ Other changes:
- `counterByName`:
- each counter name
- unknown name: `Nothing`
- `addUsage`: one entry per address
- `addUsage`:
- one entry per address
- amount evaluated when a counter is created
- `closeUsagePeriod`:
- histogram buckets, sum and max
- counters without counts excluded from histograms
+7 -8
View File
@@ -144,11 +144,10 @@ closeUsagePeriod UsageStats {clientCounts, histograms} = do
closedCounts <- mapM closeClientPeriod . M.toList =<< readTVarIO clientCounts
atomicModifyIORef'_ histograms $ M.mapWithKey $ \c -> addPeriodCounts (counterBounds c) (mapMaybe (M.lookup c) closedCounts)
where
closeClientPeriod (addr, ClientCounts {current, previous}) = do
counts <- atomically $ do
counts <- mapM readTVar =<< swapTVar current M.empty
counts <$ writeTVar previous counts
when (M.null counts) $ atomically $ TM.delete addr clientCounts
closeClientPeriod (addr, ClientCounts {current, previous}) = atomically $ do
counts <- mapM readTVar =<< swapTVar current M.empty
writeTVar previous counts
when (M.null counts) $ TM.delete addr clientCounts
pure counts
addPeriodCounts :: [Int] -> [Int] -> UsageHistogram -> UsageHistogram
@@ -169,11 +168,11 @@ usagePeriodThread period stats = do
topClients :: UsageCounter c => UsageStats c -> c -> Int -> IO [(ClientAddr, Int, Int)]
topClients UsageStats {clientCounts} c n = do
entries <- M.toList <$> readTVarIO clientCounts
counts <- forM entries $ \(addr, ClientCounts {previous, current}) -> (addr,,) <$> previousCount previous <*> currentCount current
counts <- forM entries $ \(addr, ClientCounts {previous, current}) -> atomically $ (addr,,) <$> previousCount previous <*> currentCount current
pure $ take n $ sortOn (\(_, p, cur) -> Down (p + cur)) $ filter (\(_, p, cur) -> p + cur > 0) counts
where
previousCount = fmap (M.findWithDefault 0 c) . readTVarIO
currentCount = maybe (pure 0) readTVarIO <=< TM.lookupIO c
previousCount = fmap (M.findWithDefault 0 c) . readTVar
currentCount = maybe (pure 0) readTVar <=< TM.lookup c
printTopClients :: UsageCounter c => Handle -> Text -> Maybe Int -> Maybe (UsageStats c) -> IO ()
printTopClients h name n_ = \case
+4 -1
View File
@@ -118,7 +118,7 @@ counterByNameTests = do
testCounterNames cs = map (counterByName . counterName) cs `shouldBe` map Just cs
addUsageTests :: Spec
addUsageTests =
addUsageTests = do
it "should count usage of one address in one entry" $ do
stats@UsageStats {clientCounts} <- newUsageStats
incUsage (ClientUsage stats addr1) TCSend
@@ -127,6 +127,9 @@ addUsageTests =
M.keys <$> readTVarIO clientCounts `shouldReturn` [addr1, addr2]
counts stats addr1 TCSend `shouldReturn` Just (0, 2)
counts stats addr2 TCGet `shouldReturn` Just (0, 1)
it "should evaluate the amount when a counter is created" $ do
stats <- newUsageStats
addUsage (ClientUsage stats addr1) TCSend undefined `shouldThrow` anyErrorCall
closeUsagePeriodTests :: Spec
closeUsagePeriodTests = do