mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-07-11 22:49:14 +00:00
f0b7a4be73
* smp server: messaging services (#1565)
* smp server: refactor message delivery to always respond SOK to subscriptions
* refactor ntf subscribe
* cancel subscription thread and reduce service subscription count when queue is deleted
* subscribe rcv service, deliver sent messages to subscribed service
* subscribe rcv service to messages (TODO delivery on subscription)
* WIP
* efficient initial delivery of messages to subscribed service
* test: delivery to client with service certificate
* test: upgrade/downgrade to/from service subscriptions
* remove service association from agent API, add per-user flag to use the service
* agent client (WIP)
* service certificates in the client
* rfc about drift detection, and SALL to mark end of message delivery
* fix test
* fix test
* add function for postgresql message storage
* update migration
* servers: maintain xor-hash of all associated queue IDs in PostgreSQL (#1668)
* servers: maintain xor-hash of all associated queue IDs in PostgreSQL (#1615)
* ntf server: maintain xor-hash of all associated queue IDs via PostgreSQL triggers
* smp server: xor hash with triggers
* fix sql and using pgcrypto extension in tests
* track counts and hashes in smp/ntf servers via triggers, smp server stats for service subscription, update SMP protocol to pass expected count and hash in SSUB/NSSUB commands
* agent migrations with functions/triggers
* remove agent triggers
* try tracking service subs in the agent (WIP, does not compile)
* Revert "try tracking service subs in the agent (WIP, does not compile)"
This reverts commit 59e908100d.
* comment
* agent database triggers
* service subscriptions in the client
* test / fix client services
* update schema
* fix postgres migration
* update schema
* move schema test to the end
* use static function with SQLite to avoid dynamic wrapper
* agent: fail when per-connection transport isolation is used with services (#1670)
* agent: service subscription events (#1671)
* agent: use server keyhash when loading service record
* agent: process queue/service associations with delayed subscription results
* agent: service subscription events
* agent: finalize initial service subscriptions, remove associations on service ID changes (#1672)
* agent: remove service/queue associations when service ID changes
* agent: check that service ID in NEW response matches session ID in transport session
* agent subscription WIP
* test
* comment
* enable tests
* update queries
* agent: option to add SQLite aggregates to DB connection (#1673)
* agent: add build_relations_vector function to sqlite
* update aggregate
* use static aggregate
* remove relations
---------
Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
* add test, treat BAD_SERVICE as temp error, only remove queue associations on service errors
* add packZipWith for backward compatibility with GHC 8.10.7
---------
Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
* servers: service stats and logging, allow services without option (removed), report errors during service message delivery, remove threads when service subscription ended (#1676)
* smp server: always allow services without option
* smp server: maintain IDs hash in session subscription states
* smp server: service message delivery error handling
* ntf server: log subscription count and hash differences
* smp server: remove delivery threads when service subscription ended/client disconnected
* agent: remove service queue association when service ID changed, process ENDS event, test migrating to/from service (#1677)
* agent: remove service queue association when service ID changed
* agent: process ENDS event
* agent: send service subscription error event
* agent: test migrating to/from service subscriptions, fixes
* agent: always remove service when disabled, fix service subscriptions
* ntf server: use different client certs for each SMP server, remove support for store log (#1681)
* ntf server: remove support for store log
* ntf server: use different client certificates for each SMP server
* smp protocol: fix encoding for SOKS/ENDS responses (#1683)
* agent: create user with option to enable client service (#1684)
* agent: create user with option to enable client service
* handle HTTP2 errors
* do not catch async exceptions
* agent: minor fixes
* docs: update protocol (#1705)
* docs: agent threat model
* update protocol docs
* update RFCs (#1730)
* update RFCs
* update
* update overview
* update terminology
* original language in threat model
---------
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
* docs: fix minor issues in protocols
* docs: add e2e encrypted message wire encoding to PQDR spec
* docs: add missing encodings and other protocol corrections
* docs: move implemented rfcs
* smp: service fixes (#1737)
* smp: deliver service subscription to correct client
* tests: more resilient to concurrency
* optimize PostgreSQL query
* fix service re-association after server "downgrade"
* correctly handle service removed from server (and ID changed)
* remove unused
---------
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
* prometheus: fix metrics names (#1747)
* test: rcv service re-association on restart (#1746)
* agent: correct log message
* docs: update whitepaper
* smp: fix messaging client service issues (#1751)
* services: fix minor issues
* fix accounting for subscribed service queues, add prometheus stats
* fix uncorrelated subquery
* fix potential race condition when inserting service defensively, as it is also prevented by how client is created
---------
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
* agent: refactor cleanup if no pending subs (#1757)
* smp server: batch processing of subscription messages (#1753)
* smp server: batch processing of subscription messages
* refactor
* empty line
* fix
---------
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
* smp: batch queue association updates on subscriptions (#1760)
* smp: batch queue association updates on subscriptions
* refactor to fused batching
* simpler
* batch assoc functions
* clean up
* fix
---------
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
* agent: use primary key index in setRcvServiceAssocs (#1783)
* agent: use primary key index in setRcvServiceAssocs
Previous WHERE rcv_id = ? did not match the (host, port, rcv_id)
primary key prefix and fell back to a table scan via
idx_rcv_queues_client_notice_id. With ~390k rows per queue, each
update in a 1350-row batch scanned the whole table, yielding ~290s
per batch and a multi-hour rcv-services migration.
* agent: pass SMPServer explicitly to setRcvServiceAssocs
Avoid extracting host/port from the first queue inside setRcvServiceAssocs.
The caller already has SMPServer in scope (from tSess) and the call chain
is short, so threading it through is simpler than inspecting the list.
Removes the empty-list guard from setRcvServiceAssocs (it remains in
processRcvServiceAssocs).
---------
Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
Co-authored-by: sh <37271604+shumvgolove@users.noreply.github.com>
430 lines
18 KiB
Haskell
430 lines
18 KiB
Haskell
{-# LANGUAGE BangPatterns #-}
|
|
{-# LANGUAGE DataKinds #-}
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
{-# LANGUAGE GADTs #-}
|
|
{-# LANGUAGE InstanceSigs #-}
|
|
{-# LANGUAGE LambdaCase #-}
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RankNTypes #-}
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
{-# LANGUAGE TupleSections #-}
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
module Simplex.Messaging.Server.QueueStore.STM
|
|
( STMQueueStore (..),
|
|
STMService (..),
|
|
foldRcvServiceQueues,
|
|
setStoreLog,
|
|
withLog',
|
|
readQueueRecIO,
|
|
setStatus,
|
|
)
|
|
where
|
|
|
|
import qualified Control.Exception as E
|
|
import Control.Logger.Simple
|
|
import Control.Monad
|
|
import Data.Bifunctor (first)
|
|
import Data.Bitraversable (bimapM)
|
|
import Data.Functor (($>))
|
|
import Data.Int (Int64)
|
|
import Data.List (partition)
|
|
import Data.List.NonEmpty (NonEmpty)
|
|
import qualified Data.Map.Strict as M
|
|
import Data.Set (Set)
|
|
import qualified Data.Set as S
|
|
import Data.Text (Text)
|
|
import qualified Data.X509.Validation as XV
|
|
import Simplex.Messaging.Protocol
|
|
import Simplex.Messaging.Server.QueueStore
|
|
import Simplex.Messaging.Server.QueueStore.Types
|
|
import Simplex.Messaging.Server.StoreLog
|
|
import Simplex.Messaging.SystemTime
|
|
import Simplex.Messaging.TMap (TMap)
|
|
import qualified Simplex.Messaging.TMap as TM
|
|
import Simplex.Messaging.Transport (SMPServiceRole (..))
|
|
import Simplex.Messaging.Util (anyM, ifM, tshow, ($>>), ($>>=), (<$$), (<$$>))
|
|
import System.IO
|
|
import UnliftIO.STM
|
|
|
|
data STMQueueStore q = STMQueueStore
|
|
{ queues :: TMap RecipientId q,
|
|
senders :: TMap SenderId RecipientId,
|
|
notifiers :: TMap NotifierId RecipientId,
|
|
services :: TMap ServiceId STMService,
|
|
serviceCerts :: TMap CertFingerprint ServiceId,
|
|
links :: TMap LinkId RecipientId,
|
|
storeLog :: TVar (Maybe (StoreLog 'WriteMode))
|
|
}
|
|
|
|
data STMService = STMService
|
|
{ serviceRec :: ServiceRec,
|
|
serviceRcvQueues :: TVar (Set RecipientId, IdsHash),
|
|
serviceNtfQueues :: TVar (Set NotifierId, IdsHash)
|
|
}
|
|
|
|
setStoreLog :: STMQueueStore q -> StoreLog 'WriteMode -> IO ()
|
|
setStoreLog st sl = atomically $ writeTVar (storeLog st) (Just sl)
|
|
|
|
instance StoreQueueClass q => QueueStoreClass q (STMQueueStore q) where
|
|
type QueueStoreCfg (STMQueueStore q) = ()
|
|
|
|
newQueueStore :: () -> IO (STMQueueStore q)
|
|
newQueueStore _ = do
|
|
queues <- TM.emptyIO
|
|
senders <- TM.emptyIO
|
|
notifiers <- TM.emptyIO
|
|
services <- TM.emptyIO
|
|
serviceCerts <- TM.emptyIO
|
|
links <- TM.emptyIO
|
|
storeLog <- newTVarIO Nothing
|
|
pure STMQueueStore {queues, senders, notifiers, links, services, serviceCerts, storeLog}
|
|
|
|
closeQueueStore :: STMQueueStore q -> IO ()
|
|
closeQueueStore STMQueueStore {queues, senders, notifiers, storeLog} = do
|
|
readTVarIO storeLog >>= mapM_ closeStoreLog
|
|
atomically $ TM.clear queues
|
|
atomically $ TM.clear senders
|
|
atomically $ TM.clear notifiers
|
|
|
|
loadedQueues = queues
|
|
{-# INLINE loadedQueues #-}
|
|
compactQueues _ = pure 0
|
|
{-# INLINE compactQueues #-}
|
|
|
|
getEntityCounts :: STMQueueStore q -> IO EntityCounts
|
|
getEntityCounts st = do
|
|
queueCount <- M.size <$> readTVarIO (queues st)
|
|
notifierCount <- M.size <$> readTVarIO (notifiers st)
|
|
ss <- readTVarIO (services st)
|
|
rcvServiceQueuesCount <- serviceQueuesCount serviceRcvQueues ss
|
|
ntfServiceQueuesCount <- serviceQueuesCount serviceNtfQueues ss
|
|
pure
|
|
EntityCounts
|
|
{ queueCount,
|
|
notifierCount,
|
|
rcvServiceCount = serviceCount SRMessaging ss,
|
|
ntfServiceCount = serviceCount SRNotifier ss,
|
|
rcvServiceQueuesCount,
|
|
ntfServiceQueuesCount
|
|
}
|
|
where
|
|
serviceCount role = M.foldl' (\ !n s -> if serviceRole (serviceRec s) == role then n + 1 else n) 0
|
|
serviceQueuesCount serviceSel = foldM (\n s -> (n +) . S.size . fst <$> readTVarIO (serviceSel s)) 0
|
|
|
|
addQueue_ :: STMQueueStore q -> (RecipientId -> QueueRec -> IO q) -> RecipientId -> QueueRec -> IO (Either ErrorType q)
|
|
addQueue_ st mkQ rId qr@QueueRec {senderId = sId, notifier, queueData, rcvServiceId} = do
|
|
sq <- mkQ rId qr
|
|
add sq $>> withLog "addStoreQueue" st (\s -> logCreateQueue s rId qr) $> Right sq
|
|
where
|
|
STMQueueStore {queues, senders, notifiers, links} = st
|
|
add q = atomically $ ifM hasId (pure $ Left DUPLICATE_) $ Right () <$ do
|
|
TM.insert rId q queues
|
|
TM.insert sId rId senders
|
|
forM_ notifier $ \NtfCreds {notifierId = nId, ntfServiceId} -> do
|
|
TM.insert nId rId notifiers
|
|
mapM_ (addServiceQueue st serviceNtfQueues nId) ntfServiceId
|
|
forM_ queueData $ \(lnkId, _) -> TM.insert lnkId rId links
|
|
mapM_ (addServiceQueue st serviceRcvQueues rId) rcvServiceId
|
|
hasId = anyM [TM.member rId queues, TM.member sId senders, hasNotifier, hasLink]
|
|
hasNotifier = maybe (pure False) (\NtfCreds {notifierId} -> TM.member notifierId notifiers) notifier
|
|
hasLink = maybe (pure False) (\(lnkId, _) -> TM.member lnkId links) queueData
|
|
|
|
getQueue_ :: QueueParty p => STMQueueStore q -> (Bool -> RecipientId -> QueueRec -> IO q) -> SParty p -> QueueId -> IO (Either ErrorType q)
|
|
getQueue_ st _ party qId =
|
|
maybe (Left AUTH) Right <$> case party of
|
|
SRecipient -> TM.lookupIO qId queues
|
|
SSender -> TM.lookupIO qId senders $>>= (`TM.lookupIO` queues)
|
|
SNotifier -> TM.lookupIO qId notifiers $>>= (`TM.lookupIO` queues)
|
|
SSenderLink -> TM.lookupIO qId links $>>= (`TM.lookupIO` queues)
|
|
where
|
|
STMQueueStore {queues, senders, notifiers, links} = st
|
|
|
|
getQueues_ :: BatchParty p => STMQueueStore q -> (Bool -> RecipientId -> QueueRec -> IO q) -> SParty p -> [QueueId] -> IO [Either ErrorType q]
|
|
getQueues_ st _ party qIds = case party of
|
|
SRecipient -> do
|
|
qs <- readTVarIO queues
|
|
pure $ map (get qs) qIds
|
|
SNotifier -> do
|
|
ns <- readTVarIO notifiers
|
|
qs <- readTVarIO queues
|
|
pure $ map (get qs <=< get ns) qIds
|
|
where
|
|
STMQueueStore {queues, notifiers} = st
|
|
get :: M.Map QueueId a -> QueueId -> Either ErrorType a
|
|
get m = maybe (Left AUTH) Right . (`M.lookup` m)
|
|
|
|
getQueueLinkData :: STMQueueStore q -> q -> LinkId -> IO (Either ErrorType QueueLinkData)
|
|
getQueueLinkData _ q lnkId = atomically $ readQueueRec (queueRec q) $>>= pure . getData
|
|
where
|
|
getData qr = case queueData qr of
|
|
Just (lnkId', d) | lnkId' == lnkId -> Right d
|
|
_ -> Left AUTH
|
|
|
|
addQueueLinkData :: STMQueueStore q -> q -> LinkId -> QueueLinkData -> IO (Either ErrorType ())
|
|
addQueueLinkData st sq lnkId d =
|
|
atomically (readQueueRec qr $>>= add)
|
|
$>> withLog "addQueueLinkData" st (\s -> logCreateLink s rId lnkId d)
|
|
where
|
|
rId = recipientId sq
|
|
qr = queueRec sq
|
|
add q = case queueData q of
|
|
Nothing -> addLink
|
|
Just (lnkId', d') | lnkId' == lnkId && fst d' == fst d -> addLink
|
|
_ -> pure $ Left AUTH
|
|
where
|
|
addLink = do
|
|
let !q' = q {queueData = Just (lnkId, d)}
|
|
writeTVar qr $ Just q'
|
|
TM.insert lnkId rId $ links st
|
|
pure $ Right ()
|
|
|
|
deleteQueueLinkData :: STMQueueStore q -> q -> IO (Either ErrorType ())
|
|
deleteQueueLinkData st sq =
|
|
withQueueRec qr delete
|
|
$>> withLog "deleteQueueLinkData" st (`logDeleteLink` recipientId sq)
|
|
where
|
|
qr = queueRec sq
|
|
delete q = forM (queueData q) $ \(lnkId, _) -> do
|
|
TM.delete lnkId $ links st
|
|
writeTVar qr $ Just q {queueData = Nothing}
|
|
|
|
updateKeys :: STMQueueStore q -> q -> NonEmpty RcvPublicAuthKey -> IO (Either ErrorType ())
|
|
updateKeys st sq rKeys =
|
|
withQueueRec qr update
|
|
$>> withLog "updateKeys" st (\s -> logUpdateKeys s (recipientId sq) rKeys)
|
|
where
|
|
qr = queueRec sq
|
|
update q = writeTVar qr $ Just q {recipientKeys = rKeys}
|
|
|
|
secureQueue :: STMQueueStore q -> q -> SndPublicAuthKey -> IO (Either ErrorType ())
|
|
secureQueue st sq sKey =
|
|
atomically (readQueueRec qr $>>= secure)
|
|
$>> withLog "secureQueue" st (\s -> logSecureQueue s (recipientId sq) sKey)
|
|
where
|
|
qr = queueRec sq
|
|
secure q = case senderKey q of
|
|
Just k -> pure $ if sKey == k then Right () else Left AUTH
|
|
Nothing -> do
|
|
writeTVar qr $ Just q {senderKey = Just sKey}
|
|
pure $ Right ()
|
|
|
|
addQueueNotifier :: STMQueueStore q -> q -> NtfCreds -> IO (Either ErrorType (Maybe NtfCreds))
|
|
addQueueNotifier st sq ntfCreds@NtfCreds {notifierId = nId} =
|
|
atomically (readQueueRec qr $>>= add)
|
|
$>>= \nc_ -> nc_ <$$ withLog "addQueueNotifier" st (\s -> logAddNotifier s rId ntfCreds)
|
|
where
|
|
rId = recipientId sq
|
|
qr = queueRec sq
|
|
STMQueueStore {notifiers} = st
|
|
add q = ifM (TM.member nId notifiers) (pure $ Left DUPLICATE_) $ do
|
|
nc_ <- forM (notifier q) $ \nc -> nc <$ removeNotifier st nc
|
|
let !q' = q {notifier = Just ntfCreds}
|
|
writeTVar qr $ Just q'
|
|
TM.insert nId rId notifiers
|
|
pure $ Right nc_
|
|
|
|
deleteQueueNotifier :: STMQueueStore q -> q -> IO (Either ErrorType (Maybe NtfCreds))
|
|
deleteQueueNotifier st sq =
|
|
withQueueRec qr delete
|
|
$>>= (<$$ withLog "deleteQueueNotifier" st (`logDeleteNotifier` recipientId sq))
|
|
where
|
|
qr = queueRec sq
|
|
delete q = forM (notifier q) $ \nc -> do
|
|
removeNotifier st nc
|
|
writeTVar qr $ Just q {notifier = Nothing}
|
|
pure nc
|
|
|
|
suspendQueue :: STMQueueStore q -> q -> IO (Either ErrorType ())
|
|
suspendQueue st sq =
|
|
setStatus (queueRec sq) EntityOff
|
|
$>> withLog "suspendQueue" st (`logSuspendQueue` recipientId sq)
|
|
|
|
blockQueue :: STMQueueStore q -> q -> BlockingInfo -> IO (Either ErrorType ())
|
|
blockQueue st sq info =
|
|
setStatus (queueRec sq) (EntityBlocked info)
|
|
$>> withLog "blockQueue" st (\sl -> logBlockQueue sl (recipientId sq) info)
|
|
|
|
unblockQueue :: STMQueueStore q -> q -> IO (Either ErrorType ())
|
|
unblockQueue st sq =
|
|
setStatus (queueRec sq) EntityActive
|
|
$>> withLog "unblockQueue" st (`logUnblockQueue` recipientId sq)
|
|
|
|
updateQueueTime :: STMQueueStore q -> q -> SystemDate -> IO (Either ErrorType QueueRec)
|
|
updateQueueTime st sq t = withQueueRec qr update $>>= log'
|
|
where
|
|
qr = queueRec sq
|
|
update q@QueueRec {updatedAt}
|
|
| updatedAt == Just t = pure (q, False)
|
|
| otherwise =
|
|
let !q' = q {updatedAt = Just t}
|
|
in writeTVar qr (Just q') $> (q', True)
|
|
log' (q, changed)
|
|
| changed = q <$$ withLog "updateQueueTime" st (\sl -> logUpdateQueueTime sl (recipientId sq) t)
|
|
| otherwise = pure $ Right q
|
|
|
|
deleteStoreQueue :: STMQueueStore q -> q -> IO (Either ErrorType QueueRec)
|
|
deleteStoreQueue st sq =
|
|
withQueueRec qr delete
|
|
$>>= (<$$ withLog "deleteStoreQueue" st (`logDeleteQueue` rId))
|
|
where
|
|
rId = recipientId sq
|
|
qr = queueRec sq
|
|
delete q@QueueRec {senderId, rcvServiceId} = do
|
|
writeTVar qr Nothing
|
|
TM.delete senderId $ senders st
|
|
mapM_ (removeServiceQueue st serviceRcvQueues rId) rcvServiceId
|
|
mapM_ (removeNotifier st) $ notifier q
|
|
pure q
|
|
|
|
getCreateService :: STMQueueStore q -> ServiceRec -> IO (Either ErrorType ServiceId)
|
|
getCreateService st sr@ServiceRec {serviceId = newSrvId, serviceRole, serviceCertHash = XV.Fingerprint fp} =
|
|
TM.lookupIO fp serviceCerts
|
|
>>= maybe
|
|
(atomically $ TM.lookup fp serviceCerts >>= maybe newService checkService)
|
|
(atomically . checkService)
|
|
$>>= \(serviceId, new) ->
|
|
if new
|
|
then serviceId <$$ withLog "getCreateService" st (`logNewService` sr)
|
|
else pure $ Right serviceId
|
|
where
|
|
STMQueueStore {services, serviceCerts} = st
|
|
checkService sId =
|
|
TM.lookup sId services >>= \case
|
|
Just STMService {serviceRec = ServiceRec {serviceId, serviceRole = role}}
|
|
| role == serviceRole -> pure $ Right (serviceId, False)
|
|
| otherwise -> pure $ Left $ SERVICE
|
|
Nothing -> newService_
|
|
newService = ifM (TM.member newSrvId services) (pure $ Left DUPLICATE_) newService_
|
|
newService_ = do
|
|
TM.insertM newSrvId newSTMService services
|
|
TM.insert fp newSrvId serviceCerts
|
|
pure $ Right (newSrvId, True)
|
|
newSTMService = do
|
|
serviceRcvQueues <- newTVar (S.empty, mempty)
|
|
serviceNtfQueues <- newTVar (S.empty, mempty)
|
|
pure STMService {serviceRec = sr, serviceRcvQueues, serviceNtfQueues}
|
|
|
|
setQueueService :: (PartyI p, ServiceParty p) => STMQueueStore q -> q -> SParty p -> Maybe ServiceId -> IO (Either ErrorType ())
|
|
setQueueService st sq party serviceId =
|
|
atomically (readQueueRec qr $>>= setService)
|
|
$>> withLog "setQueueService" st (\sl -> logQueueService sl rId party serviceId)
|
|
where
|
|
qr = queueRec sq
|
|
rId = recipientId sq
|
|
setService :: QueueRec -> STM (Either ErrorType ())
|
|
setService q@QueueRec {rcvServiceId = prevSrvId} = case party of
|
|
SRecipientService
|
|
| prevSrvId == serviceId -> pure $ Right ()
|
|
| otherwise -> do
|
|
updateServiceQueues serviceRcvQueues rId prevSrvId
|
|
let !q' = Just q {rcvServiceId = serviceId}
|
|
writeTVar qr q' $> Right ()
|
|
SNotifierService -> case notifier q of
|
|
Nothing -> pure $ Left AUTH
|
|
Just nc@NtfCreds {notifierId = nId, ntfServiceId = prevNtfSrvId}
|
|
| prevNtfSrvId == serviceId -> pure $ Right ()
|
|
| otherwise -> do
|
|
let !q' = Just q {notifier = Just nc {ntfServiceId = serviceId}}
|
|
updateServiceQueues serviceNtfQueues nId prevNtfSrvId
|
|
writeTVar qr q' $> Right ()
|
|
updateServiceQueues :: (STMService -> TVar (Set QueueId, IdsHash)) -> QueueId -> Maybe ServiceId -> STM ()
|
|
updateServiceQueues serviceSel qId prevSrvId = do
|
|
mapM_ (removeServiceQueue st serviceSel qId) prevSrvId
|
|
mapM_ (addServiceQueue st serviceSel qId) serviceId
|
|
|
|
setQueueServices st party serviceId qs = Right . M.fromList <$> mapM setQueue qs
|
|
where
|
|
setQueue sq = (recipientId sq,) <$> setQueueService st sq party serviceId
|
|
|
|
getQueueNtfServices :: STMQueueStore q -> [(NotifierId, a)] -> IO (Either ErrorType ([(Maybe ServiceId, [(NotifierId, a)])], [(NotifierId, a)]))
|
|
getQueueNtfServices st ntfs = do
|
|
ss <- readTVarIO (services st)
|
|
(ssNtfs, noServiceNtfs) <- if M.null ss then pure ([], ntfs) else foldM addService ([], ntfs) (M.assocs ss)
|
|
ns <- readTVarIO (notifiers st)
|
|
let (ntfs', deleteNtfs) = partition (\(nId, _) -> M.member nId ns) noServiceNtfs
|
|
ssNtfs' = (Nothing, ntfs') : ssNtfs
|
|
pure $ Right (ssNtfs', deleteNtfs)
|
|
where
|
|
addService (ssNtfs, ntfs') (serviceId, s) = do
|
|
(snIds, _) <- readTVarIO $ serviceNtfQueues s
|
|
let (sNtfs, restNtfs) = partition (\(nId, _) -> S.member nId snIds) ntfs'
|
|
pure ((Just serviceId, sNtfs) : ssNtfs, restNtfs)
|
|
|
|
getServiceQueueCountHash :: (PartyI p, ServiceParty p) => STMQueueStore q -> SParty p -> ServiceId -> IO (Either ErrorType (Int64, IdsHash))
|
|
getServiceQueueCountHash st party serviceId =
|
|
TM.lookupIO serviceId (services st) >>=
|
|
maybe (pure $ Left AUTH) (fmap (Right . first (fromIntegral . S.size)) . readTVarIO . serviceSel)
|
|
where
|
|
serviceSel :: STMService -> TVar (Set QueueId, IdsHash)
|
|
serviceSel = case party of
|
|
SRecipientService -> serviceRcvQueues
|
|
SNotifierService -> serviceNtfQueues
|
|
|
|
foldRcvServiceQueues :: StoreQueueClass q => STMQueueStore q -> ServiceId -> (a -> (q, QueueRec) -> IO a) -> a -> IO a
|
|
foldRcvServiceQueues st serviceId f acc =
|
|
TM.lookupIO serviceId (services st) >>= \case
|
|
Nothing -> pure acc
|
|
Just s ->
|
|
readTVarIO (serviceRcvQueues s)
|
|
>>= foldM (\a -> get >=> maybe (pure a) (f a)) acc . fst
|
|
where
|
|
get rId = TM.lookupIO rId (queues st) $>>= \q -> (q,) <$$> readTVarIO (queueRec q)
|
|
|
|
withQueueRec :: TVar (Maybe QueueRec) -> (QueueRec -> STM a) -> IO (Either ErrorType a)
|
|
withQueueRec qr a = atomically $ readQueueRec qr >>= mapM a
|
|
|
|
setStatus :: TVar (Maybe QueueRec) -> ServerEntityStatus -> IO (Either ErrorType ())
|
|
setStatus qr status =
|
|
atomically $ stateTVar qr $ \case
|
|
Just q -> (Right (), Just q {status})
|
|
Nothing -> (Left AUTH, Nothing)
|
|
|
|
addServiceQueue :: STMQueueStore q -> (STMService -> TVar (Set QueueId, IdsHash)) -> QueueId -> ServiceId -> STM ()
|
|
addServiceQueue = setServiceQueues_ S.insert
|
|
{-# INLINE addServiceQueue #-}
|
|
|
|
removeServiceQueue :: STMQueueStore q -> (STMService -> TVar (Set QueueId, IdsHash)) -> QueueId -> ServiceId -> STM ()
|
|
removeServiceQueue = setServiceQueues_ S.delete
|
|
{-# INLINE removeServiceQueue #-}
|
|
|
|
setServiceQueues_ :: (QueueId -> Set QueueId -> Set QueueId) -> STMQueueStore q -> (STMService -> TVar (Set QueueId, IdsHash)) -> QueueId -> ServiceId -> STM ()
|
|
setServiceQueues_ updateSet st serviceSel qId serviceId =
|
|
TM.lookup serviceId (services st) >>= mapM_ (\v -> modifyTVar' (serviceSel v) update)
|
|
where
|
|
update (s, idsHash) =
|
|
let !s' = updateSet qId s
|
|
!idsHash' = queueIdHash qId <> idsHash
|
|
in (s', idsHash')
|
|
|
|
removeNotifier :: STMQueueStore q -> NtfCreds -> STM ()
|
|
removeNotifier st NtfCreds {notifierId = nId, ntfServiceId} = do
|
|
TM.delete nId $ notifiers st
|
|
mapM_ (removeServiceQueue st serviceNtfQueues nId) ntfServiceId
|
|
|
|
readQueueRec :: TVar (Maybe QueueRec) -> STM (Either ErrorType QueueRec)
|
|
readQueueRec qr = maybe (Left AUTH) Right <$> readTVar qr
|
|
{-# INLINE readQueueRec #-}
|
|
|
|
readQueueRecIO :: TVar (Maybe QueueRec) -> IO (Either ErrorType QueueRec)
|
|
readQueueRecIO qr = maybe (Left AUTH) Right <$> readTVarIO qr
|
|
{-# INLINE readQueueRecIO #-}
|
|
|
|
withLog' :: Text -> TVar (Maybe (StoreLog 'WriteMode)) -> (StoreLog 'WriteMode -> IO ()) -> IO (Either ErrorType ())
|
|
withLog' name sl action =
|
|
readTVarIO sl
|
|
>>= maybe (pure $ Right ()) (E.try . E.uninterruptibleMask_ . action >=> bimapM logErr pure)
|
|
where
|
|
logErr :: E.SomeException -> IO ErrorType
|
|
logErr e = logError ("STORE: " <> err) $> STORE err
|
|
where
|
|
err = name <> ", withLog, " <> tshow e
|
|
|
|
withLog :: Text -> STMQueueStore q -> (StoreLog 'WriteMode -> IO ()) -> IO (Either ErrorType ())
|
|
withLog name = withLog' name . storeLog
|
|
{-# INLINE withLog #-}
|