mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-23 04:40:27 +00:00
simplify
This commit is contained in:
1 parent
0c5823aa8c
commit
360424b090
9 files changed
+104
-187
No files matched your search
@@ -55,25 +55,32 @@ Counter types and command functions:
|
||||
Counters of every server:
|
||||
|
||||
- `connections`: connections passed to the protocol handler, after TLS setup.
|
||||
- One counter per command, named by the command tag: each parsed command is counted, failed ones included.
|
||||
- `errors`: parse errors and failed verifications.
|
||||
- Command counters: each parsed command is counted once, failed ones included.
|
||||
- A command with its own counter: the counter named by the command tag.
|
||||
- Other commands: `other`.
|
||||
|
||||
SMP proxy counters, by result of `PRXY`:
|
||||
Commands with their own counter:
|
||||
|
||||
- `PRXY_own`: own server.
|
||||
- `PRXY_connected`: other server, existing connection.
|
||||
- `PRXY_new`: other server, new connection.
|
||||
- `PRXY_onion`: other server, connection to an onion host.
|
||||
- `PRXY_failed`: error.
|
||||
- SMP:
|
||||
- `NEW`
|
||||
- `SUB`
|
||||
- `SEND`
|
||||
- `PFWD`
|
||||
- `RFWD`
|
||||
- `RSLV`
|
||||
- XFTP:
|
||||
- `FNEW`
|
||||
- NTF:
|
||||
- `TNEW`
|
||||
- `TRPL`
|
||||
- `SNEW`
|
||||
|
||||
SMP proxy counters, by result of `PFWD`:
|
||||
SMP proxy counters:
|
||||
|
||||
- `PFWD_own`: own server.
|
||||
- `PFWD_other`: other server.
|
||||
- `PFWD_onion`: other server, connection to an onion host.
|
||||
- `PFWD_failed`: error other than a protocol error, or no session.
|
||||
|
||||
Precedence: `failed`, then `own`, then `onion`.
|
||||
- `PRXY_new`: `PRXY` without a connected relay.
|
||||
- `PRXY_failed`: `PRXY` with an error.
|
||||
- `PFWD_failed`: `PFWD` with an error other than a protocol error, or without a session.
|
||||
|
||||
XFTP counters:
|
||||
|
||||
@@ -102,8 +109,8 @@ data UsageStats c = UsageStats
|
||||
}
|
||||
|
||||
data ClientCounts c = ClientCounts
|
||||
{ current :: IORef (Map c Int),
|
||||
previous :: IORef (Map c Int)
|
||||
{ current :: TMap c (TVar Int),
|
||||
previous :: TVar (Map c Int)
|
||||
}
|
||||
|
||||
data ClientUsage c = ClientUsage (UsageStats c) ClientAddr
|
||||
@@ -137,13 +144,16 @@ Functions:
|
||||
- `addUsage :: UsageCounter c => ClientUsage c -> c -> Int -> IO ()`:
|
||||
1. The client entry is read with `TM.lookupIO`.
|
||||
2. An absent entry is created, then looked up or inserted in one transaction.
|
||||
3. The count of the counter in `current` is increased with `atomicModifyIORef'`.
|
||||
3. The counter is read from `current` with `TM.lookupIO`.
|
||||
4. An absent counter is looked up or inserted with the 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. `current` is set to an empty map. Its counts are the closed counts.
|
||||
2. `previous` is set to the closed counts.
|
||||
3. The entry is deleted when the closed counts are empty.
|
||||
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.
|
||||
|
||||
Then, per counter, the closed counts are added to the histogram:
|
||||
- to each bucket with a bound at or above the count
|
||||
@@ -160,7 +170,10 @@ Functions:
|
||||
- `printTopClients :: UsageCounter c => Handle -> Text -> Maybe Int -> Maybe (UsageStats c) -> IO ()`: control port output.
|
||||
- `usageMetrics :: UsageCounter c => Text -> Text -> Map c UsageHistogram -> Text`: Prometheus text for a metric prefix and a timestamp.
|
||||
|
||||
A count added to an entry between its read and its deletion in `closeUsagePeriod` is lost.
|
||||
Counts lost in `closeUsagePeriod`:
|
||||
|
||||
- a count added to an entry between its read and its deletion;
|
||||
- a count added after the closing transaction to a counter read with `TM.lookupIO` before it.
|
||||
|
||||
## Prometheus
|
||||
|
||||
@@ -278,8 +291,8 @@ In `Simplex.Messaging.Server`:
|
||||
- `errors` is increased by the number of parse errors.
|
||||
- `errors` is incremented for each failed verification.
|
||||
- `processProxiedCmd`:
|
||||
- `incResult own sel c`: the proxy stat `sel` and the usage counter `c` are incremented when the result of `PRXY` or `PFWD` is known.
|
||||
- `proxyServerResponse` has the counter of a successful `PRXY` to another server as a parameter: `PRXY_connected` or `PRXY_new`.
|
||||
- `PRXY_new` is incremented before the connection to the relay is forked.
|
||||
- `incFailed own sel`: the proxy stat `sel` and `PRXY_failed` or `PFWD_failed` are incremented.
|
||||
|
||||
## NTF server
|
||||
|
||||
@@ -337,7 +350,7 @@ Other changes:
|
||||
- number of clients limited
|
||||
- clients with both counts 0 excluded
|
||||
- `usageMetrics` output
|
||||
- command function of each server
|
||||
- command function of each server: counter named by the command tag or `other`
|
||||
|
||||
`usageStatsPeriod = Just 1` is set in the default test configurations of the SMP, XFTP and NTF servers.
|
||||
|
||||
@@ -347,7 +360,7 @@ Control port tests, with `usageStatsPeriod = Just 300`:
|
||||
- output of `usage SEND` and `usage connections`: the test client address
|
||||
- output of `usage` without authentication: `AUTH`
|
||||
- output of `usage` with an unknown counter: `error: unknown counter`
|
||||
- SMP proxy: proxy counters for other servers and own servers.
|
||||
- SMP proxy: `other`, `PRXY_new`, `PRXY_failed`, `PFWD` and `PFWD_failed`.
|
||||
- XFTP server: command, transfer and recipient counters.
|
||||
|
||||
Other tests:
|
||||
|
||||
@@ -148,12 +148,7 @@ data XFTPUsageCounter
|
||||
= XUCConnections
|
||||
| XUCErrors
|
||||
| XUCFNew
|
||||
| XUCFAdd
|
||||
| XUCFPut
|
||||
| XUCFDel
|
||||
| XUCFGet
|
||||
| XUCFAck
|
||||
| XUCPing
|
||||
| XUCOther
|
||||
| XUCUploadKb
|
||||
| XUCDownloadKb
|
||||
| XUCRecipients
|
||||
@@ -164,12 +159,7 @@ instance UsageCounter XFTPUsageCounter where
|
||||
XUCConnections -> "connections"
|
||||
XUCErrors -> "errors"
|
||||
XUCFNew -> "FNEW"
|
||||
XUCFAdd -> "FADD"
|
||||
XUCFPut -> "FPUT"
|
||||
XUCFDel -> "FDEL"
|
||||
XUCFGet -> "FGET"
|
||||
XUCFAck -> "FACK"
|
||||
XUCPing -> "PING"
|
||||
XUCOther -> "other"
|
||||
XUCUploadKb -> "upload_kb"
|
||||
XUCDownloadKb -> "download_kb"
|
||||
XUCRecipients -> "recipients"
|
||||
@@ -182,12 +172,7 @@ instance UsageCounter XFTPUsageCounter where
|
||||
xftpCmdCounter :: FileCmd -> XFTPUsageCounter
|
||||
xftpCmdCounter (FileCmd _ cmd) = case cmd of
|
||||
FNEW {} -> XUCFNew
|
||||
FADD _ -> XUCFAdd
|
||||
FPUT -> XUCFPut
|
||||
FDEL -> XUCFDel
|
||||
FGET _ -> XUCFGet
|
||||
FACK -> XUCFAck
|
||||
PING -> XUCPing
|
||||
_ -> XUCOther
|
||||
|
||||
fileStore :: XFTPEnv s -> s
|
||||
fileStore = fromFileStore . store
|
||||
|
||||
@@ -129,15 +129,9 @@ data NtfUsageCounter
|
||||
= NUCConnections
|
||||
| NUCErrors
|
||||
| NUCTNew
|
||||
| NUCTVfy
|
||||
| NUCTChk
|
||||
| NUCTRpl
|
||||
| NUCTDel
|
||||
| NUCTCrn
|
||||
| NUCSNew
|
||||
| NUCSChk
|
||||
| NUCSDel
|
||||
| NUCPing
|
||||
| NUCOther
|
||||
deriving (Eq, Ord, Enum, Bounded, Show)
|
||||
|
||||
instance UsageCounter NtfUsageCounter where
|
||||
@@ -145,29 +139,17 @@ instance UsageCounter NtfUsageCounter where
|
||||
NUCConnections -> "connections"
|
||||
NUCErrors -> "errors"
|
||||
NUCTNew -> "TNEW"
|
||||
NUCTVfy -> "TVFY"
|
||||
NUCTChk -> "TCHK"
|
||||
NUCTRpl -> "TRPL"
|
||||
NUCTDel -> "TDEL"
|
||||
NUCTCrn -> "TCRN"
|
||||
NUCSNew -> "SNEW"
|
||||
NUCSChk -> "SCHK"
|
||||
NUCSDel -> "SDEL"
|
||||
NUCPing -> "PING"
|
||||
NUCOther -> "other"
|
||||
connectionsCounter = NUCConnections
|
||||
|
||||
ntfCmdCounter :: NtfCmd -> NtfUsageCounter
|
||||
ntfCmdCounter (NtfCmd _ cmd) = case cmd of
|
||||
TNEW _ -> NUCTNew
|
||||
TVFY _ -> NUCTVfy
|
||||
TCHK -> NUCTChk
|
||||
TRPL _ -> NUCTRpl
|
||||
TDEL -> NUCTDel
|
||||
TCRN _ -> NUCTCrn
|
||||
SNEW _ -> NUCSNew
|
||||
SCHK -> NUCSChk
|
||||
SDEL -> NUCSDel
|
||||
PING -> NUCPing
|
||||
_ -> NUCOther
|
||||
|
||||
newNtfServerEnv :: NtfServerConfig -> IO NtfEnv
|
||||
newNtfServerEnv config@NtfServerConfig {pushQSize, smpAgentCfg, apnsConfig, dbStoreConfig, ntfCredentials, useServiceCreds, usageStatsPeriod} = do
|
||||
|
||||
@@ -98,7 +98,7 @@ import Network.Socket (ServiceName, Socket, socketToHandle)
|
||||
import qualified Network.TLS as TLS
|
||||
import Numeric.Natural (Natural)
|
||||
import Simplex.Messaging.Agent.Lock
|
||||
import Simplex.Messaging.Client (ProtocolClient (thParams), ProtocolClientError (..), SMPClient, SMPClientError, clientHandlers, forwardSMPTransmission, smpProxyError, temporaryClientError, transportHost')
|
||||
import Simplex.Messaging.Client (ProtocolClient (thParams), ProtocolClientError (..), SMPClient, SMPClientError, clientHandlers, forwardSMPTransmission, smpProxyError, temporaryClientError)
|
||||
import Simplex.Messaging.Client.Agent (OwnServer, SMPClientAgent (..), SMPClientAgentEvent (..), closeSMPClientAgent, getSMPServerClient'', isOwnServer, lookupSMPServerClient, getConnectedSMPServerClient)
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import Simplex.Messaging.Encoding
|
||||
@@ -125,7 +125,6 @@ import Simplex.Messaging.TMap (TMap)
|
||||
import qualified Simplex.Messaging.TMap as TM
|
||||
import Simplex.Messaging.Transport
|
||||
import Simplex.Messaging.Transport.Buffer (trimCR)
|
||||
import Simplex.Messaging.Transport.Client (TransportHost (..))
|
||||
import Simplex.Messaging.Transport.Server
|
||||
import Simplex.Messaging.Util
|
||||
import Simplex.Messaging.Version
|
||||
@@ -1437,26 +1436,27 @@ client
|
||||
getRelay = do
|
||||
ProxyAgent {smpAgent = a} <- asks proxyAgent
|
||||
liftIO (getConnectedSMPServerClient a srv) >>= \case
|
||||
Just r -> Just <$> proxyServerResponse a SUCPrxyConnected r
|
||||
Nothing ->
|
||||
Just r -> Just <$> proxyServerResponse a r
|
||||
Nothing -> do
|
||||
incClientUsage SUCPrxyNew
|
||||
forkProxiedCmd $
|
||||
liftIO (runExceptT (getSMPServerClient'' a srv) `E.catches` clientHandlers)
|
||||
>>= proxyServerResponse a SUCPrxyNew
|
||||
proxyServerResponse :: SMPClientAgent 'Sender -> SMPUsageCounter -> Either SMPClientError (OwnServer, SMPClient) -> M s BrokerMsg
|
||||
proxyServerResponse a otherCounter smp_ = do
|
||||
>>= proxyServerResponse a
|
||||
proxyServerResponse :: SMPClientAgent 'Sender -> Either SMPClientError (OwnServer, SMPClient) -> M s BrokerMsg
|
||||
proxyServerResponse a smp_ = do
|
||||
ServerStats {pRelays, pRelaysOwn} <- asks serverStats
|
||||
let inc = mkIncProxyStats pRelays pRelaysOwn
|
||||
incResult own sel c = inc own sel >> incClientUsage c
|
||||
incFailed own sel = inc own sel >> incClientUsage SUCPrxyFailed
|
||||
case smp_ of
|
||||
Right (own, smp) -> do
|
||||
inc own pRequests
|
||||
case proxyResp smp of
|
||||
r@PKEY {} -> r <$ incResult own pSuccesses (proxyDestCounter own smp SUCPrxyOwn SUCPrxyOnion otherCounter)
|
||||
r -> r <$ incResult own pErrorsCompat SUCPrxyFailed
|
||||
r@PKEY {} -> r <$ inc own pSuccesses
|
||||
r -> r <$ incFailed own pErrorsCompat
|
||||
Left e -> do
|
||||
let own = isOwnServer a srv
|
||||
inc own pRequests
|
||||
incResult own (if temporaryClientError e then pErrorsConnect else pErrorsOther) SUCPrxyFailed
|
||||
incFailed own $ if temporaryClientError e then pErrorsConnect else pErrorsOther
|
||||
logWarn $ "Error connecting: " <> decodeLatin1 (strEncode $ host srv) <> " " <> tshow e
|
||||
pure . ERR $ smpProxyError e
|
||||
where
|
||||
@@ -1473,26 +1473,20 @@ client
|
||||
ProxyAgent {smpAgent = a} <- asks proxyAgent
|
||||
ServerStats {pMsgFwds, pMsgFwdsOwn} <- asks serverStats
|
||||
let inc = mkIncProxyStats pMsgFwds pMsgFwdsOwn
|
||||
incResult own sel c = inc own sel >> incClientUsage c
|
||||
incFailed own sel = inc own sel >> incClientUsage SUCPfwdFailed
|
||||
liftIO (lookupSMPServerClient a sessId) >>= \case
|
||||
Just (own, smp) -> do
|
||||
inc own pRequests
|
||||
let dest = proxyDestCounter own smp SUCPfwdOwn SUCPfwdOnion SUCPfwdOther
|
||||
forkProxiedCmd $ do
|
||||
liftIO (runExceptT (forwardSMPTransmission smp corrId fwdV pubKey encBlock) `E.catches` clientHandlers) >>= \case
|
||||
Right r -> PRES r <$ incResult own pSuccesses dest
|
||||
Right r -> PRES r <$ inc own pSuccesses
|
||||
Left e -> ERR (smpProxyError e) <$ case e of
|
||||
PCEProtocolError {} -> incResult own pSuccesses dest
|
||||
_ -> incResult own pErrorsOther SUCPfwdFailed
|
||||
Nothing -> inc False pRequests >> incResult False pErrorsConnect SUCPfwdFailed $> Just (ERR $ PROXY NO_SESSION)
|
||||
PCEProtocolError {} -> inc own pSuccesses
|
||||
_ -> incFailed own pErrorsOther
|
||||
Nothing -> inc False pRequests >> incFailed False pErrorsConnect $> Just (ERR $ PROXY NO_SESSION)
|
||||
where
|
||||
forkProxiedCmd :: M s BrokerMsg -> M s (Maybe BrokerMsg)
|
||||
forkProxiedCmd = forkCmd serverClientConcurrency corrId (EntityId sessId)
|
||||
proxyDestCounter :: OwnServer -> SMPClient -> SMPUsageCounter -> SMPUsageCounter -> SMPUsageCounter -> SMPUsageCounter
|
||||
proxyDestCounter own smp ownCounter onionCounter otherCounter
|
||||
| own = ownCounter
|
||||
| THOnionHost _ <- transportHost' smp = onionCounter
|
||||
| otherwise = otherCounter
|
||||
incClientUsage :: SMPUsageCounter -> M s ()
|
||||
incClientUsage c = liftIO $ forM_ clientUsage (`incUsage` c)
|
||||
-- Run a slow command on a thread
|
||||
|
||||
@@ -479,37 +479,13 @@ data SMPUsageCounter
|
||||
| SUCErrors
|
||||
| SUCNew
|
||||
| SUCSub
|
||||
| SUCSubs
|
||||
| SUCKey
|
||||
| SUCRKey
|
||||
| SUCLSet
|
||||
| SUCLDel
|
||||
| SUCNKey
|
||||
| SUCNDel
|
||||
| SUCGet
|
||||
| SUCAck
|
||||
| SUCOff
|
||||
| SUCDel
|
||||
| SUCQue
|
||||
| SUCSKey
|
||||
| SUCSend
|
||||
| SUCPing
|
||||
| SUCLKey
|
||||
| SUCLGet
|
||||
| SUCNSub
|
||||
| SUCNSubs
|
||||
| SUCPrxy
|
||||
| SUCPfwd
|
||||
| SUCRfwd
|
||||
| SUCRslv
|
||||
| SUCPrxyOwn
|
||||
| SUCPrxyConnected
|
||||
| SUCOther
|
||||
| SUCPrxyNew
|
||||
| SUCPrxyOnion
|
||||
| SUCPrxyFailed
|
||||
| SUCPfwdOwn
|
||||
| SUCPfwdOther
|
||||
| SUCPfwdOnion
|
||||
| SUCPfwdFailed
|
||||
deriving (Eq, Ord, Enum, Bounded, Show)
|
||||
|
||||
@@ -519,37 +495,13 @@ instance UsageCounter SMPUsageCounter where
|
||||
SUCErrors -> "errors"
|
||||
SUCNew -> "NEW"
|
||||
SUCSub -> "SUB"
|
||||
SUCSubs -> "SUBS"
|
||||
SUCKey -> "KEY"
|
||||
SUCRKey -> "RKEY"
|
||||
SUCLSet -> "LSET"
|
||||
SUCLDel -> "LDEL"
|
||||
SUCNKey -> "NKEY"
|
||||
SUCNDel -> "NDEL"
|
||||
SUCGet -> "GET"
|
||||
SUCAck -> "ACK"
|
||||
SUCOff -> "OFF"
|
||||
SUCDel -> "DEL"
|
||||
SUCQue -> "QUE"
|
||||
SUCSKey -> "SKEY"
|
||||
SUCSend -> "SEND"
|
||||
SUCPing -> "PING"
|
||||
SUCLKey -> "LKEY"
|
||||
SUCLGet -> "LGET"
|
||||
SUCNSub -> "NSUB"
|
||||
SUCNSubs -> "NSUBS"
|
||||
SUCPrxy -> "PRXY"
|
||||
SUCPfwd -> "PFWD"
|
||||
SUCRfwd -> "RFWD"
|
||||
SUCRslv -> "RSLV"
|
||||
SUCPrxyOwn -> "PRXY_own"
|
||||
SUCPrxyConnected -> "PRXY_connected"
|
||||
SUCOther -> "other"
|
||||
SUCPrxyNew -> "PRXY_new"
|
||||
SUCPrxyOnion -> "PRXY_onion"
|
||||
SUCPrxyFailed -> "PRXY_failed"
|
||||
SUCPfwdOwn -> "PFWD_own"
|
||||
SUCPfwdOther -> "PFWD_other"
|
||||
SUCPfwdOnion -> "PFWD_onion"
|
||||
SUCPfwdFailed -> "PFWD_failed"
|
||||
connectionsCounter = SUCConnections
|
||||
|
||||
@@ -557,29 +509,11 @@ smpCmdCounter :: Cmd -> SMPUsageCounter
|
||||
smpCmdCounter (Cmd _ cmd) = case cmd of
|
||||
NEW _ -> SUCNew
|
||||
SUB -> SUCSub
|
||||
SUBS {} -> SUCSubs
|
||||
KEY _ -> SUCKey
|
||||
RKEY _ -> SUCRKey
|
||||
LSET {} -> SUCLSet
|
||||
LDEL -> SUCLDel
|
||||
NKEY {} -> SUCNKey
|
||||
NDEL -> SUCNDel
|
||||
GET -> SUCGet
|
||||
ACK _ -> SUCAck
|
||||
OFF -> SUCOff
|
||||
DEL -> SUCDel
|
||||
QUE -> SUCQue
|
||||
SKEY _ -> SUCSKey
|
||||
SEND {} -> SUCSend
|
||||
PING -> SUCPing
|
||||
LKEY _ -> SUCLKey
|
||||
LGET -> SUCLGet
|
||||
NSUB -> SUCNSub
|
||||
NSUBS {} -> SUCNSubs
|
||||
PRXY {} -> SUCPrxy
|
||||
PFWD {} -> SUCPfwd
|
||||
RFWD _ -> SUCRfwd
|
||||
RSLV _ -> SUCRslv
|
||||
_ -> SUCOther
|
||||
|
||||
type VerifiedTransmission s = (Maybe (StoreQueue s, QueueRec), Transmission Cmd)
|
||||
|
||||
|
||||
@@ -62,8 +62,8 @@ class (Ord c, Enum c, Bounded c) => UsageCounter c where
|
||||
connectionsCounter :: c
|
||||
|
||||
data ClientCounts c = ClientCounts
|
||||
{ current :: IORef (Map c Int),
|
||||
previous :: IORef (Map c Int)
|
||||
{ current :: TMap c (TVar Int),
|
||||
previous :: TVar (Map c Int)
|
||||
}
|
||||
|
||||
data ClientUsage c = ClientUsage (UsageStats c) ClientAddr
|
||||
@@ -132,10 +132,12 @@ incUsage usage c = addUsage usage c 1
|
||||
addUsage :: UsageCounter c => ClientUsage c -> c -> Int -> IO ()
|
||||
addUsage (ClientUsage UsageStats {clientCounts} addr) c n = do
|
||||
ClientCounts {current} <- TM.lookupIO addr clientCounts >>= maybe newCounts pure
|
||||
atomicModifyIORef'_ current $ M.insertWith (+) c n
|
||||
TM.lookupIO c current >>= atomically . maybe (newCount current) add
|
||||
where
|
||||
add v = modifyTVar' v (+ n)
|
||||
newCount current = TM.lookup c current >>= maybe (TM.insertM c (newTVar n) current) add
|
||||
newCounts = do
|
||||
counts <- ClientCounts <$> newIORef M.empty <*> newIORef M.empty
|
||||
counts <- ClientCounts <$> TM.emptyIO <*> newTVarIO M.empty
|
||||
atomically $ TM.lookup addr clientCounts >>= maybe (counts <$ TM.insert addr counts clientCounts) pure
|
||||
|
||||
closeUsagePeriod :: UsageCounter c => UsageStats c -> IO ()
|
||||
@@ -145,8 +147,9 @@ closeUsagePeriod UsageStats {clientCounts, histograms} = do
|
||||
atomicModifyIORef'_ histograms $ M.mapWithKey $ \c -> addPeriodCounts (counterBounds c) (M.findWithDefault [] c periodCounts)
|
||||
where
|
||||
closeClientPeriod (addr, ClientCounts {current, previous}) = do
|
||||
counts <- atomicModifyIORef' current (M.empty,)
|
||||
writeIORef previous counts
|
||||
counts <- atomically $ do
|
||||
counts <- mapM readTVar =<< swapTVar current M.empty
|
||||
counts <$ writeTVar previous counts
|
||||
when (M.null counts) $ atomically $ TM.delete addr clientCounts
|
||||
pure counts
|
||||
|
||||
@@ -169,10 +172,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,,) <$> counterValue previous <*> counterValue current
|
||||
counts <- forM entries $ \(addr, ClientCounts {previous, current}) -> (addr,,) <$> previousCount previous <*> currentCount current
|
||||
pure $ take n $ sortOn (\(_, p, cur) -> Down (p + cur)) $ filter (\(_, p, cur) -> p + cur > 0) counts
|
||||
where
|
||||
counterValue = fmap (M.findWithDefault 0 c) . readIORef
|
||||
previousCount = fmap (M.findWithDefault 0 c) . readTVarIO
|
||||
currentCount = maybe (pure 0) readTVarIO <=< TM.lookupIO c
|
||||
|
||||
printTopClients :: UsageCounter c => Handle -> Text -> Maybe Int -> Maybe (UsageStats c) -> IO ()
|
||||
printTopClients h name n_ = \case
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
module CoreTests.UsageStatsTests where
|
||||
|
||||
import Control.Monad ((<=<))
|
||||
import Data.ByteString.Char8 (ByteString)
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import Data.IORef (readIORef)
|
||||
@@ -24,6 +25,7 @@ import Simplex.Messaging.Protocol
|
||||
import Simplex.Messaging.Server.Env.STM (SMPUsageCounter (..), smpCmdCounter)
|
||||
import Simplex.Messaging.Server.UsageStats
|
||||
import Simplex.Messaging.SimplexName (SimplexDomain (..), SimplexTLD (..))
|
||||
import qualified Simplex.Messaging.TMap as TM
|
||||
import Simplex.Messaging.Transport (supportedClientSMPRelayVRange, supportedServerSMPRelayVRange)
|
||||
import Simplex.Messaging.Version (VersionRange (..))
|
||||
import Test.Hspec hiding (fit, it)
|
||||
@@ -228,29 +230,34 @@ metricsTests =
|
||||
|
||||
commandCounterTests :: Spec
|
||||
commandCounterTests = do
|
||||
it "should count each SMP command in the counter named by its tag" $ do
|
||||
it "should count each SMP command in the counter named by its tag or in other" $ do
|
||||
cmds <- smpCommands
|
||||
map (counterName . smpCmdCounter) cmds `shouldBe` map (commandTag . encodeProtocol (maxVersion supportedServerSMPRelayVRange)) cmds
|
||||
S.fromList (map smpCmdCounter cmds) `shouldBe` S.fromList [SUCNew .. SUCRslv]
|
||||
it "should count each XFTP command in the counter named by its tag" $ do
|
||||
map (counterName . smpCmdCounter) cmds `shouldBe` map (commandCounter ["NEW", "SUB", "SEND", "PFWD", "RFWD", "RSLV"] . encodeProtocol (maxVersion supportedServerSMPRelayVRange)) cmds
|
||||
S.fromList (map smpCmdCounter cmds) `shouldBe` S.fromList [SUCNew .. SUCOther]
|
||||
it "should count each XFTP command in the counter named by its tag or in other" $ do
|
||||
cmds <- xftpCommands
|
||||
map (counterName . xftpCmdCounter) cmds `shouldBe` map (commandTag . encodeProtocol (maxVersion supportedFileServerVRange)) cmds
|
||||
S.fromList (map xftpCmdCounter cmds) `shouldBe` S.fromList [XUCFNew .. XUCPing]
|
||||
map (counterName . xftpCmdCounter) cmds `shouldBe` map (commandCounter ["FNEW"] . encodeProtocol (maxVersion supportedFileServerVRange)) cmds
|
||||
S.fromList (map xftpCmdCounter cmds) `shouldBe` S.fromList [XUCFNew .. XUCOther]
|
||||
#if defined(dbServerPostgres)
|
||||
it "should count each NTF command in the counter named by its tag" $ do
|
||||
it "should count each NTF command in the counter named by its tag or in other" $ do
|
||||
cmds <- ntfCommands
|
||||
map (counterName . ntfCmdCounter) cmds `shouldBe` map (commandTag . encodeProtocol (maxVersion supportedServerNTFVRange)) cmds
|
||||
S.fromList (map ntfCmdCounter cmds) `shouldBe` S.fromList [NUCTNew .. NUCPing]
|
||||
map (counterName . ntfCmdCounter) cmds `shouldBe` map (commandCounter ["TNEW", "TRPL", "SNEW"] . encodeProtocol (maxVersion supportedServerNTFVRange)) cmds
|
||||
S.fromList (map ntfCmdCounter cmds) `shouldBe` S.fromList [NUCTNew .. NUCOther]
|
||||
#endif
|
||||
where
|
||||
commandTag :: ByteString -> Text
|
||||
commandTag = decodeLatin1 . B.takeWhile (/= ' ')
|
||||
commandCounter :: [Text] -> ByteString -> Text
|
||||
commandCounter tags s
|
||||
| tag `elem` tags = tag
|
||||
| otherwise = "other"
|
||||
where
|
||||
tag = decodeLatin1 $ B.takeWhile (/= ' ') s
|
||||
|
||||
counts :: UsageStats TestCounter -> ClientAddr -> TestCounter -> IO (Maybe (Int, Int))
|
||||
counts UsageStats {clientCounts} addr c =
|
||||
readTVarIO clientCounts >>= mapM (\ClientCounts {previous, current} -> (,) <$> counterValue previous <*> counterValue current) . M.lookup addr
|
||||
readTVarIO clientCounts >>= mapM (\ClientCounts {previous, current} -> (,) <$> previousCount previous <*> currentCount current) . M.lookup addr
|
||||
where
|
||||
counterValue = fmap (M.findWithDefault 0 c) . readIORef
|
||||
previousCount = fmap (M.findWithDefault 0 c) . readTVarIO
|
||||
currentCount = maybe (pure 0) readTVarIO <=< TM.lookupIO c
|
||||
|
||||
testKeys :: IO (C.APublicAuthKey, C.APrivateAuthKey, C.PublicKeyX25519)
|
||||
testKeys = do
|
||||
|
||||
+8
-10
@@ -33,7 +33,6 @@ import Simplex.Messaging.Agent.Env.SQLite (AgentConfig (..), InitialAgentServers
|
||||
import Simplex.Messaging.Agent.Protocol hiding (CON, CONF, INFO, REQ)
|
||||
import qualified Simplex.Messaging.Agent.Protocol as A
|
||||
import Simplex.Messaging.Client
|
||||
import Simplex.Messaging.Client.Agent (SMPClientAgentConfig (..))
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import Simplex.Messaging.Crypto.Ratchet (pattern PQSupportOn)
|
||||
import qualified Simplex.Messaging.Crypto.Ratchet as CR
|
||||
@@ -150,29 +149,28 @@ smpProxyTests = do
|
||||
xdescribe "stress test 10k" $ do
|
||||
let deliver nAgents nMsgs = agentDeliverMessagesViaProxyConc (replicate nAgents [srv1]) (map bshow [1 :: Int .. nMsgs])
|
||||
it "25 agents, 300 pairs, 17 messages" . oneServer . withNumCapabilities 4 $ deliver 25 17
|
||||
describe "client usage statistics" $ do
|
||||
it "should count proxy commands to other servers" $
|
||||
proxyUsageStats [] [("PRXY", "2"), ("PRXY_new", "1"), ("PRXY_connected", "1"), ("PFWD", "3"), ("PFWD_other", "2"), ("PFWD_failed", "1")]
|
||||
it "should count proxy commands to own servers" $
|
||||
proxyUsageStats ["127.0.0.1"] [("PRXY", "2"), ("PRXY_own", "2"), ("PFWD", "3"), ("PFWD_own", "2"), ("PFWD_failed", "1")]
|
||||
describe "client usage statistics" $
|
||||
it "should count proxy commands" proxyUsageStats
|
||||
where
|
||||
proxyUsageStats :: [ByteString] -> [(ByteString, ByteString)] -> AStoreType -> IO ()
|
||||
proxyUsageStats ownDomains counts msType =
|
||||
proxyUsageStats :: AStoreType -> IO ()
|
||||
proxyUsageStats msType =
|
||||
twoServers_ proxyCfg' (cfgMS msType) testProxyCounts msType
|
||||
where
|
||||
proxyCfg' = updateCfg (proxyCfgMS msType) $ \cfg_ ->
|
||||
cfg_
|
||||
{ usageStatsPeriod = Just 300,
|
||||
controlPort = Just testControlPort,
|
||||
controlPortUserAuth = Just testControlPortUserAuth,
|
||||
smpAgentCfg = (smpAgentCfg cfg_) {ownServerDomains = ownDomains}
|
||||
controlPortUserAuth = Just testControlPortUserAuth
|
||||
}
|
||||
testProxyCounts = do
|
||||
deliverMessageViaProxy (SMPServer testHost2 testPort testKeyHash) (SMPServer testHost2 testPort2 testKeyHash) C.SEd448 "hello 1" "hello 2"
|
||||
withControlPort $ \h -> do
|
||||
controlPortCommand h ("auth " <> SMP.unBasicAuth testControlPortUserAuth) 1 `shouldReturn` ["Current role is CPRUser"]
|
||||
controlPortCommand h "usage PRXY_failed" 1 `shouldReturn` ["address,previous,current"]
|
||||
forM_ counts $ \(counter, n) ->
|
||||
controlPortCommand h ("usage " <> counter) 2 `shouldReturn` ["address,previous,current", "127.0.0.1,0," <> n]
|
||||
counts :: [(ByteString, ByteString)]
|
||||
counts = [("other", "2"), ("PRXY_new", "1"), ("PFWD", "3"), ("PFWD_failed", "1")]
|
||||
oneServer test msType = withSmpServerConfigOn (transport @TLS) (updateCfg (proxyCfgMS msType) $ \cfg_ -> cfg_ {msgQueueQuota = 128, maxJournalMsgCount = 256}) testPort $ const test
|
||||
twoServers test msType = twoServers_ (proxyCfgMS msType) (proxyCfgMS msType) test msType
|
||||
twoServersFirstProxy test msType = twoServers_ (proxyCfgMS msType) (updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {msgQueueQuota = 128, maxJournalMsgCount = 256}) test msType
|
||||
|
||||
@@ -350,7 +350,7 @@ testFileUsageStats fsType =
|
||||
controlPortCommand h ("usage " <> counter) 2 `shouldReturn` ["address,previous,current", "127.0.0.1,0," <> n]
|
||||
where
|
||||
counts :: [(ByteString, ByteString)]
|
||||
counts = [("connections", "1"), ("FNEW", "2"), ("FPUT", "2"), ("FGET", "2"), ("recipients", "2"), ("upload_kb", "128"), ("download_kb", "256")]
|
||||
counts = [("connections", "1"), ("FNEW", "2"), ("other", "4"), ("recipients", "2"), ("upload_kb", "128"), ("download_kb", "256")]
|
||||
|
||||
testFileBasicAuth :: Bool -> Maybe BasicAuth -> Maybe BasicAuth -> Bool -> AFStoreType -> IO ()
|
||||
testFileBasicAuth allowNewFiles newFileBasicAuth clntAuth success fsType =
|
||||
|
||||
Reference in new issue
Block a user