Skip to content

Commit 5884d2c

Browse files
committed
ntf server: use multiple APNS clients for each provider
1 parent 27a3738 commit 5884d2c

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/Simplex/Messaging/Notifications/Server.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,15 @@ showServer' :: SMPServer -> Text
641641
showServer' = decodeLatin1 . strEncode . host
642642

643643
pushNotification :: NtfPushServer -> Maybe T.Text -> OwnServer -> NtfTknRec -> PushNotification -> M ()
644-
pushNotification s srvHost_ isOwn tkn@NtfTknRec {ntfTknId, token = token@(DeviceToken pp _)} ntf =
644+
pushNotification s srvHost_ isOwn tkn@NtfTknRec {token = token@(DeviceToken pp _)} ntf =
645645
ifM
646646
(pushProviderAllowed token)
647-
(getOrCreatePushWorker s (srvHost_, pp, hash (unEntityId ntfTknId) `mod` pushWorkersPerServer) isOwn >>= atomically . (`writeTBQueue` (tkn, ntf)))
647+
(getOrCreatePushWorker s (srvHost_, pp, pushTokenShardId 8 tkn) isOwn >>= atomically . (`writeTBQueue` (tkn, ntf)))
648648
(logWarn "skipping disabled APNS test push provider")
649-
where
650-
pushWorkersPerServer = 8
649+
650+
pushTokenShardId :: Int -> NtfTknRec -> Int
651+
pushTokenShardId pushWorkersPerServer NtfTknRec {ntfTknId} =
652+
hash (unEntityId ntfTknId) `mod` pushWorkersPerServer
651653

652654
pushProviderAllowed :: DeviceToken -> M Bool
653655
pushProviderAllowed (DeviceToken PPApnsTest _) = asks (allowTestPushProvider . config)
@@ -707,7 +709,7 @@ runPushWorker s srvHost_ isOwn q = forever $ do
707709
| otherwise = liftIO $ logError "bad notification token status"
708710
deliverNotification :: NtfPostgresStore -> PushProvider -> NtfTknRec -> PushNotification -> IO (Either PushProviderError ())
709711
deliverNotification st pp tkn@NtfTknRec {ntfTknId} ntf' = do
710-
(deliver, clientVar) <- getPushClient s pp
712+
(deliver, clientVar) <- getPushClient s pp tokenShardId
711713
runExceptT (deliver tkn ntf') >>= \case
712714
Right _ -> pure $ Right ()
713715
Left e -> case e of
@@ -720,11 +722,12 @@ runPushWorker s srvHost_ isOwn q = forever $ do
720722
err e
721723
PPPermanentError -> err e
722724
where
725+
tokenShardId = pushTokenShardId 8 tkn
723726
retryDeliver :: PushClientVar -> Text -> IO (Either PushProviderError ())
724727
retryDeliver oldVar reason = do
725728
logWarn $ "retrying push (" <> tshow pp <> ", " <> tshow ntfTknId <> "): " <> reason
726-
atomically $ removeSessVar oldVar pp (pushClients s)
727-
(deliver, _) <- getPushClient s pp
729+
atomically $ removeSessVar oldVar (pp, tokenShardId) (pushClients s)
730+
(deliver, _) <- getPushClient s pp tokenShardId
728731
runExceptT (deliver tkn ntf') >>= \case
729732
Right _ -> pure $ Right ()
730733
Left e -> case e of

src/Simplex/Messaging/Notifications/Server/Env.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ module Simplex.Messaging.Notifications.Server.Env
3030
import Control.Concurrent (ThreadId)
3131
import qualified Control.Exception as E
3232
import Control.Logger.Simple
33-
import Control.Monad
3433
import Control.Monad.Except
3534
import Control.Monad.Trans.Except
3635
import Crypto.Random
37-
import Data.Functor (($>))
3836
import Data.Int (Int64)
3937
import Simplex.Messaging.Agent.RetryInterval
4038
import Data.List.NonEmpty (NonEmpty)
@@ -66,7 +64,6 @@ import Simplex.Messaging.Transport.Credentials (genCredentials, tlsCredentials)
6664
import Simplex.Messaging.Transport.Server (AddHTTP, ServerCredentials, TransportServerConfig, loadFingerprint, loadServerCredential)
6765
import Simplex.Messaging.Util (liftEitherWith, tshow)
6866
import Simplex.Messaging.Util ()
69-
import System.Exit (exitFailure)
7067
import System.Mem.Weak (Weak)
7168
import UnliftIO.STM
7269

@@ -177,7 +174,7 @@ data NtfPushServer = NtfPushServer
177174
{ pushWorkers :: TMap (Maybe T.Text, PushProvider, Int) PushWorkerVar, -- Int is the worker shard
178175
pushWorkerSeq :: TVar Int,
179176
pushQSize :: Natural,
180-
pushClients :: TMap PushProvider PushClientVar,
177+
pushClients :: TMap (PushProvider, Int) PushClientVar,
181178
pushClientSeq :: TVar Int,
182179
apnsConfig :: APNSPushClientConfig
183180
}
@@ -203,27 +200,27 @@ newNtfPushServer pushQSize apnsConfig = do
203200
-- | Single-flight access to the per-provider push client with bounded retry.
204201
-- The returned PushClientVar is the handle retryDeliver passes to removeSessVar to evict
205202
-- this specific instance before re-fetching.
206-
getPushClient :: NtfPushServer -> PushProvider -> IO (PushProviderClient, PushClientVar)
207-
getPushClient s@NtfPushServer {apnsConfig = APNSPushClientConfig {reconnectInterval}} pp =
203+
getPushClient :: NtfPushServer -> PushProvider -> Int -> IO (PushProviderClient, PushClientVar)
204+
getPushClient s@NtfPushServer {apnsConfig = APNSPushClientConfig {reconnectInterval}} pp tokenShardId =
208205
withRetryIntervalCount reconnectInterval $ \n _delay loop -> do
209206
ts <- getCurrentTime
210-
E.try (atomically (getSessVar (pushClientSeq s) pp (pushClients s) ts) >>= either (newPushClient s pp) waitForPushClient) >>= \case
207+
E.try (atomically (getSessVar (pushClientSeq s) (pp, tokenShardId) (pushClients s) ts) >>= either (newPushClient s pp tokenShardId) waitForPushClient) >>= \case
211208
Right result -> pure result
212209
Left e
213210
| n < 2 -> do
214211
logError $ "getPushClient error (" <> tshow pp <> "): " <> tshow (e :: E.SomeException)
215212
loop
216213
| otherwise -> E.throwIO e
217214

218-
newPushClient :: NtfPushServer -> PushProvider -> PushClientVar -> IO (PushProviderClient, PushClientVar)
219-
newPushClient NtfPushServer {pushClients, apnsConfig} pp v = do
215+
newPushClient :: NtfPushServer -> PushProvider -> Int -> PushClientVar -> IO (PushProviderClient, PushClientVar)
216+
newPushClient NtfPushServer {pushClients, apnsConfig} pp tokenShardId v = do
220217
r <- E.try $ case apnsProviderHost pp of
221218
Nothing -> pure $ \_ _ -> pure ()
222219
Just host -> apnsPushProviderClient <$> createAPNSPushClient host apnsConfig
223220
atomically $ do
224221
putTMVar (sessionVar v) r
225222
case r of
226-
Left _ -> removeSessVar v pp pushClients
223+
Left _ -> removeSessVar v (pp, tokenShardId) pushClients
227224
Right _ -> pure ()
228225
either E.throwIO (\c -> pure (c, v)) r
229226

0 commit comments

Comments
 (0)