@@ -281,7 +281,7 @@ ntfServer cfg@NtfServerConfig {transports, transportConfig = tCfg, startOptions}
281281 getSMPServiceSubMetrics a sel subQueueCount = getSubMetrics_ a sel countSubs
282282 where
283283 countSubs :: (NtfSMPSubMetrics , S. Set Text ) -> (SMPServer , TVar (Maybe sub )) -> IO (NtfSMPSubMetrics , S. Set Text )
284- countSubs acc (srv, serviceSubs) = maybe acc ( subMetricsResult a acc srv . fromIntegral . subQueueCount) <$> readTVarIO serviceSubs
284+ countSubs acc (srv, serviceSubs) = subMetricsResult a acc srv . fromIntegral . maybe 0 subQueueCount <$> readTVarIO serviceSubs
285285
286286 getSMPSubMetrics :: SMPClientAgent 'Notifier -> (SMPClientAgent 'Notifier -> TMap SMPServer (TMap NotifierId a )) -> IO NtfSMPSubMetrics
287287 getSMPSubMetrics a sel = getSubMetrics_ a sel countSubs
@@ -305,11 +305,11 @@ ntfServer cfg@NtfServerConfig {transports, transportConfig = tCfg, startOptions}
305305 | isOwnServer a srv =
306306 let ! ownSrvSubs' = M. alter (Just . maybe cnt (+ cnt)) host ownSrvSubs
307307 metrics' = metrics {ownSrvSubs = ownSrvSubs'} :: NtfSMPSubMetrics
308- in (metrics', otherSrvs)
308+ in (metrics', otherSrvs)
309309 | cnt == 0 = acc
310310 | otherwise =
311311 let metrics' = metrics {otherSrvSubCount = otherSrvSubCount + cnt} :: NtfSMPSubMetrics
312- in (metrics', S. insert host otherSrvs)
312+ in (metrics', S. insert host otherSrvs)
313313 where
314314 NtfSMPSubMetrics {ownSrvSubs, otherSrvSubCount} = metrics
315315 host = safeDecodeUtf8 $ strEncode h
@@ -527,7 +527,7 @@ ntfSubscriber NtfSubscriber {smpAgent = ca@SMPClientAgent {msgQ, agentQ}} =
527527 NtfPushServer {pushQ} <- asks pushServer
528528 stats <- asks serverStats
529529 liftIO $ forever $ do
530- ((_, srv, _), _thVersion, sessionId, ts) <- atomically $ readTBQueue msgQ
530+ ((_, srv@ ( SMPServer (h :| _) _ _) , _), _thVersion, sessionId, ts) <- atomically $ readTBQueue msgQ
531531 forM ts $ \ (ntfId, t) -> case t of
532532 STUnexpectedError e -> logError $ " SMP client unexpected error: " <> tshow e -- uncorrelated response, should not happen
533533 STResponse {} -> pure () -- it was already reported as timeout error
@@ -538,9 +538,16 @@ ntfSubscriber NtfSubscriber {smpAgent = ca@SMPClientAgent {msgQ, agentQ}} =
538538 ntfTs <- getSystemTime
539539 updatePeriodStats (activeSubs stats) ntfId
540540 let newNtf = PNMessageData {smpQueue, ntfTs, nmsgNonce, encNMsgMeta}
541- ntfs_ <- addTokenLastNtf st newNtf
542- forM_ ntfs_ $ \ (tkn, lastNtfs) -> atomically $ writeTBQueue pushQ (tkn, PNMessage lastNtfs)
543- incNtfStat_ stats ntfReceived
541+ srvHost_ = if isOwnServer ca srv then Just (safeDecodeUtf8 $ strEncode h) else Nothing
542+ addTokenLastNtf st newNtf >>= \ case
543+ Right (tkn, lastNtfs) -> do
544+ atomically $ writeTBQueue pushQ (srvHost_, tkn, PNMessage lastNtfs)
545+ incNtfStat_ stats ntfReceived
546+ mapM_ (`incServerStat` ntfReceivedOwn stats) srvHost_
547+ Left AUTH -> do
548+ incNtfStat_ stats ntfReceivedAuth
549+ mapM_ (`incServerStat` ntfReceivedAuthOwn stats) srvHost_
550+ Left _ -> pure ()
544551 Right SMP. END ->
545552 whenM (atomically $ activeClientSession' ca sessionId srv) $
546553 void $ updateSrvSubStatus st smpQueue NSEnd
@@ -625,7 +632,7 @@ showServer' = decodeLatin1 . strEncode . host
625632
626633ntfPush :: NtfPushServer -> M ()
627634ntfPush s@ NtfPushServer {pushQ} = forever $ do
628- (tkn@ NtfTknRec {ntfTknId, token = t@ (DeviceToken pp _), tknStatus}, ntf) <- atomically (readTBQueue pushQ)
635+ (srvHost_, tkn@ NtfTknRec {ntfTknId, token = t@ (DeviceToken pp _), tknStatus}, ntf) <- atomically (readTBQueue pushQ)
629636 liftIO $ logDebug $ " sending push notification to " <> T. pack (show pp)
630637 st <- asks store
631638 case ntf of
@@ -644,8 +651,14 @@ ntfPush s@NtfPushServer {pushQ} = forever $ do
644651 PNMessage {} -> checkActiveTkn tknStatus $ do
645652 stats <- asks serverStats
646653 liftIO $ updatePeriodStats (activeTokens stats) ntfTknId
647- liftIO (deliverNotification st pp tkn ntf)
648- >>= incNtfStatT t . (\ case Left _ -> ntfFailed; Right () -> ntfDelivered)
654+ liftIO (deliverNotification st pp tkn ntf) >>= \ case
655+ Left _ -> do
656+ incNtfStatT t ntfFailed
657+ liftIO $ mapM_ (`incServerStat` ntfFailedOwn stats) srvHost_
658+ Right () -> do
659+ incNtfStatT t ntfDelivered
660+ liftIO $ mapM_ (`incServerStat` ntfDeliveredOwn stats) srvHost_
661+
649662 where
650663 checkActiveTkn :: NtfTknStatus -> M () -> M ()
651664 checkActiveTkn status action
@@ -686,7 +699,7 @@ periodicNtfsThread NtfPushServer {pushQ} = do
686699 liftIO $ forever $ do
687700 threadDelay interval
688701 now <- systemSeconds <$> getSystemTime
689- cnt <- withPeriodicNtfTokens st now $ \ tkn -> atomically $ writeTBQueue pushQ (tkn, PNCheckMessages )
702+ cnt <- withPeriodicNtfTokens st now $ \ tkn -> atomically $ writeTBQueue pushQ (Nothing , tkn, PNCheckMessages )
690703 logNote $ " Scheduled periodic notifications: " <> tshow cnt
691704
692705runNtfClientTransport :: Transport c => THandleNTF c 'TServer -> M ()
@@ -794,7 +807,7 @@ client NtfServerClient {rcvQ, sndQ} ns@NtfSubscriber {smpAgent = ca} NtfPushServ
794807 ts <- liftIO $ getSystemDate
795808 let tkn = mkNtfTknRec tknId newTkn srvDhPrivKey dhSecret regCode ts
796809 withNtfStore (`addNtfToken` tkn) $ \ _ -> do
797- atomically $ writeTBQueue pushQ (tkn, PNVerification regCode)
810+ atomically $ writeTBQueue pushQ (Nothing , tkn, PNVerification regCode)
798811 incNtfStatT token ntfVrfQueued
799812 incNtfStatT token tknCreated
800813 pure $ NRTknId tknId srvDhPubKey
@@ -810,7 +823,7 @@ client NtfServerClient {rcvQ, sndQ} ns@NtfSubscriber {smpAgent = ca} NtfPushServ
810823 | otherwise -> withNtfStore (\ st -> updateTknStatus st tkn NTRegistered ) $ \ _ -> sendVerification
811824 where
812825 sendVerification = do
813- atomically $ writeTBQueue pushQ (tkn, PNVerification tknRegCode)
826+ atomically $ writeTBQueue pushQ (Nothing , tkn, PNVerification tknRegCode)
814827 incNtfStatT token ntfVrfQueued
815828 pure $ NRTknId ntfTknId $ C. publicKey tknDhPrivKey
816829 TVFY code -- this allows repeated verification for cases when client connection dropped before server response
@@ -828,7 +841,7 @@ client NtfServerClient {rcvQ, sndQ} ns@NtfSubscriber {smpAgent = ca} NtfPushServ
828841 regCode <- getRegCode
829842 let tkn' = tkn {token = token', tknStatus = NTRegistered , tknRegCode = regCode}
830843 withNtfStore (`replaceNtfToken` tkn') $ \ _ -> do
831- atomically $ writeTBQueue pushQ (tkn', PNVerification regCode)
844+ atomically $ writeTBQueue pushQ (Nothing , tkn', PNVerification regCode)
832845 incNtfStatT token ntfVrfQueued
833846 incNtfStatT token tknReplaced
834847 pure NROk
0 commit comments