diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 92d794941..4064f8173 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -590,7 +590,7 @@ getPendingMsgQ c connId SndQueue {server, sndId} = do runSmpQueueMsgDelivery :: forall m. AgentMonad m => AgentClient -> ConnData -> SndQueue -> m () runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandshake} sq = do mq <- atomically $ getPendingMsgQ c connId sq - ri <- asks $ reconnectInterval . config + ri <- asks $ messageRetryInterval . config forever $ do atomically $ endAgentOperation c AOSndNetwork msgId <- atomically $ readTQueue mq @@ -621,12 +621,8 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh -- in duplexHandshake mode (v2) HELLO is only sent once, without retrying, -- because the queue must be secured by the time the confirmation or the first HELLO is received | duplexHandshake == Just True -> connErr - | otherwise -> do - helloTimeout <- asks $ helloTimeout . config - currentTime <- liftIO getCurrentTime - if diffUTCTime currentTime internalTs > helloTimeout - then connErr - else retrySending loop + | otherwise -> + ifM (msgExpired helloTimeout) connErr (retrySending loop) where connErr = case rq_ of -- party initiating connection @@ -635,10 +631,16 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} cData@ConnData {connId, duplexHandsh _ -> connError msgId NOT_ACCEPTED AM_REPLY_ -> notifyDel msgId $ ERR e AM_A_MSG_ -> notifyDel msgId $ MERR mId e - SMP (SMP.CMD _) -> notifyDel msgId err - SMP SMP.LARGE_MSG -> notifyDel msgId err - SMP {} -> notify err >> retrySending loop - _ -> retrySending loop + _ + | temporaryAgentError e -> do + let timeoutSel = if msgType == AM_HELLO_ then helloTimeout else messageTimeout + ifM (msgExpired timeoutSel) (notifyDel msgId err) (retrySending loop) + | otherwise -> notifyDel msgId err + where + msgExpired timeoutSel = do + msgTimeout <- asks $ timeoutSel . config + currentTime <- liftIO getCurrentTime + pure $ diffUTCTime currentTime internalTs > msgTimeout Right () -> do case msgType of AM_CONN_INFO -> do diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 3f55633a1..440c32cc4 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -29,7 +29,7 @@ module Simplex.Messaging.Agent.Client getSubscriptions, sendConfirmation, sendInvitation, - RetryInterval (..), + temporaryAgentError, secureQueue, enableQueueNotifications, disableQueueNotifications, diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 14a93092b..853419a33 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -69,6 +69,8 @@ data AgentConfig = AgentConfig smpCfg :: ProtocolClientConfig, ntfCfg :: ProtocolClientConfig, reconnectInterval :: RetryInterval, + messageRetryInterval :: RetryInterval, + messageTimeout :: NominalDiffTime, helloTimeout :: NominalDiffTime, ntfCron :: Word16, ntfWorkerDelay :: Int, @@ -85,12 +87,18 @@ data AgentConfig = AgentConfig defaultReconnectInterval :: RetryInterval defaultReconnectInterval = RetryInterval - { initialInterval = second, - increaseAfter = 10 * second, - maxInterval = 10 * second + { initialInterval = 1_000000, + increaseAfter = 10_000000, + maxInterval = 10_000000 + } + +defaultMessageRetryInterval :: RetryInterval +defaultMessageRetryInterval = + RetryInterval + { initialInterval = 1_000000, + increaseAfter = 10_000000, + maxInterval = 60_000000 } - where - second = 1_000_000 defaultAgentConfig :: AgentConfig defaultAgentConfig = @@ -104,6 +112,8 @@ defaultAgentConfig = smpCfg = defaultClientConfig {defaultTransport = (show defaultSMPPort, transport @TLS)}, ntfCfg = defaultClientConfig {defaultTransport = ("443", transport @TLS)}, reconnectInterval = defaultReconnectInterval, + messageRetryInterval = defaultMessageRetryInterval, + messageTimeout = 2 * nominalDay, helloTimeout = 2 * nominalDay, ntfCron = 20, -- minutes ntfWorkerDelay = 100000, -- microseconds