mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-08-29 09:58:41 +00:00
diff against agent subs
This commit is contained in:
@@ -84,12 +84,14 @@ module Simplex.Messaging.Agent.Client
|
||||
agentClientStore,
|
||||
agentDRG,
|
||||
getAgentSubscriptions,
|
||||
diffSubscriptions,
|
||||
slowNetworkConfig,
|
||||
protocolClientError,
|
||||
Worker (..),
|
||||
SessionVar (..),
|
||||
SubscriptionsInfo (..),
|
||||
SubInfo (..),
|
||||
SubscriptionsDiff (..),
|
||||
AgentOperation (..),
|
||||
AgentOpState (..),
|
||||
AgentState (..),
|
||||
@@ -157,6 +159,7 @@ import Data.ByteString.Base64
|
||||
import Data.ByteString.Char8 (ByteString)
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import Data.Either (partitionEithers)
|
||||
import Data.Foldable (fold)
|
||||
import Data.Functor (($>))
|
||||
import Data.Int (Int64)
|
||||
import Data.List (deleteFirstsBy, foldl', partition, (\\))
|
||||
@@ -1734,6 +1737,32 @@ getAgentSubscriptions c = do
|
||||
enc :: StrEncoding a => a -> Text
|
||||
enc = decodeLatin1 . strEncode
|
||||
|
||||
diffSubscriptions :: AgentClient -> IO SubscriptionsDiff
|
||||
diffSubscriptions AgentClient {smpClients, subscrConns} = do
|
||||
agentConns <- readTVarIO subscrConns
|
||||
clients <- readTVarIO smpClients
|
||||
clientSubscribed <- fmap (M.keysSet . fold) . forM (M.assocs clients) $ \((_, srv, _), SessionVar {sessionVar}) ->
|
||||
atomically (tryReadTMVar sessionVar) >>= \case
|
||||
Just (Right smp) -> readTVarIO (sentSubs smp)
|
||||
_ -> mempty <$ putStrLn ("no client for " <> show srv)
|
||||
print (agentConns, clientSubscribed, agentConns `S.intersection` clientSubscribed)
|
||||
pure
|
||||
SubscriptionsDiff
|
||||
{ inBoth = S.size $ agentConns `S.intersection` clientSubscribed,
|
||||
onlyInAgent = agentConns `notIn` clientSubscribed,
|
||||
onlyInClients = clientSubscribed `notIn` agentConns
|
||||
}
|
||||
where
|
||||
notIn :: Set ConnId -> Set ConnId -> [Text]
|
||||
notIn a b = map (decodeLatin1 . strEncode) . S.toList $ S.difference a b
|
||||
|
||||
data SubscriptionsDiff = SubscriptionsDiff
|
||||
{ inBoth :: Int,
|
||||
onlyInAgent :: [Text],
|
||||
onlyInClients :: [Text]
|
||||
}
|
||||
deriving (Show)
|
||||
|
||||
data AgentWorkersDetails = AgentWorkersDetails
|
||||
{ smpClients_ :: [Text],
|
||||
ntfClients_ :: [Text],
|
||||
@@ -1871,6 +1900,8 @@ $(J.deriveJSON defaultJSON ''SubInfo)
|
||||
|
||||
$(J.deriveJSON defaultJSON ''SubscriptionsInfo)
|
||||
|
||||
$(J.deriveJSON defaultJSON ''SubscriptionsDiff)
|
||||
|
||||
$(J.deriveJSON defaultJSON ''WorkersDetails)
|
||||
|
||||
$(J.deriveJSON defaultJSON ''WorkersSummary)
|
||||
|
||||
Reference in New Issue
Block a user