diff --git a/apps/smp-agent/Main.hs b/apps/smp-agent/Main.hs index a632c63f9..bb0685549 100644 --- a/apps/smp-agent/Main.hs +++ b/apps/smp-agent/Main.hs @@ -8,21 +8,10 @@ import Control.Logger.Simple import qualified Data.List.NonEmpty as L import Simplex.Messaging.Agent (runSMPAgent) import Simplex.Messaging.Agent.Env.SQLite -import Simplex.Messaging.Client (smpDefaultConfig) import Simplex.Messaging.Transport (TCP, Transport (..)) cfg :: AgentConfig -cfg = - AgentConfig - { tcpPort = "5224", - smpServers = L.fromList ["localhost:5223#bU0K+bRg24xWW//lS0umO1Zdw/SXqpJNtm1/RrPLViE="], - rsaKeySize = 2048 `div` 8, - connIdBytes = 12, - tbqSize = 16, - dbFile = "smp-agent.db", - dbPoolSize = 4, - smpCfg = smpDefaultConfig - } +cfg = defaultAgentConfig {smpServers = L.fromList ["localhost:5223#bU0K+bRg24xWW//lS0umO1Zdw/SXqpJNtm1/RrPLViE="]} logCfg :: LogConfig logCfg = LogConfig {lc_file = Nothing, lc_stderr = True} diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 53599d709..69a3864c6 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -270,33 +270,15 @@ newConn c connId viaInv connLevel = do addSubscription c rq connId' pure (connId', qInfo) -minute :: Int -minute = 60_000_000 - -onlineInterval :: RetryInterval -onlineInterval = - RetryInterval - { initialInterval = 1_000_000, - increaseAfter = minute, - maxInterval = 10 * minute - } - -resumeInterval :: RetryInterval -resumeInterval = - RetryInterval - { initialInterval = 5_000_000, - increaseAfter = 0, - maxInterval = 10 * minute - } - joinConn :: AgentMonad m => AgentClient -> ConnId -> SMPQueueInfo -> ConnInfo -> Maybe InvitationId -> Int -> m ConnId joinConn c connId qInfo cInfo viaInv connLevel = do (sq, senderKey, verifyKey) <- newSndQueue qInfo g <- asks idsDrg + cfg <- asks config let cData = ConnData {connId, viaInv, connLevel} connId' <- withStore $ \st -> createSndConn st g cData sq confirmQueue c sq senderKey cInfo - activateQueueJoining c connId' sq verifyKey onlineInterval + activateQueueJoining c connId' sq verifyKey $ retryInterval cfg pure connId' activateQueueJoining :: forall m. AgentMonad m => AgentClient -> ConnId -> SndQueue -> VerificationKey -> RetryInterval -> m () @@ -370,7 +352,7 @@ subscribeConnection' c connId = _ -> throwError $ INTERNAL "unexpected queue status" SomeConn _ (SndConnection _ sq) -> case status (sq :: SndQueue) of Confirmed -> withVerifyKey sq $ \verifyKey -> - activateQueueJoining c connId sq verifyKey resumeInterval + activateQueueJoining c connId sq verifyKey =<< resumeInterval Active -> throwError $ CONN SIMPLEX _ -> throwError $ INTERNAL "unexpected queue status" SomeConn _ (RcvConnection _ rq) -> subscribeQueue c rq connId @@ -381,8 +363,12 @@ subscribeConnection' c connId = in maybe err action . C.publicKey $ signKey sq activateSecuredQueue :: RcvQueue -> SndQueue -> C.PublicKey -> m () activateSecuredQueue rq sq verifyKey = do - activateQueueInitiating c connId sq verifyKey resumeInterval + activateQueueInitiating c connId sq verifyKey =<< resumeInterval subscribeQueue c rq connId + resumeInterval :: m RetryInterval + resumeInterval = do + r <- asks $ retryInterval . config + pure r {initialInterval = 5_000_000} -- | Send message to the connection (SEND command) in Reader monad sendMessage' :: forall m. AgentMonad m => AgentClient -> ConnId -> MsgBody -> m InternalId @@ -540,7 +526,8 @@ processSMPTransmission c@AgentClient {subQ} (srv, rId, cmd) = do withStore $ \st -> upgradeRcvConnToDuplex st connId sq confirmQueue c sq senderKey ownConnInfo withStore (`removeConfirmations` connId) - activateQueueInitiating c connId sq verifyKey onlineInterval + cfg <- asks config + activateQueueInitiating c connId sq verifyKey $ retryInterval cfg _ -> prohibited introMsg :: IntroId -> ConnInfo -> m () diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index b4ff3069a..9ffbd44e0 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -254,12 +254,6 @@ sendConfirmation c sq@SndQueue {server, sndId} senderKey cInfo = mkConfirmation :: SMPClient -> m MsgBody mkConfirmation smp = encryptAndSign smp sq . serializeSMPMessage $ SMPConfirmation senderKey cInfo -data RetryInterval = RetryInterval - { initialInterval :: Int, - increaseAfter :: Int, - maxInterval :: Int - } - sendHello :: forall m. AgentMonad m => AgentClient -> SndQueue -> VerificationKey -> RetryInterval -> m () sendHello c sq@SndQueue {server, sndId, sndPrivateKey} verifyKey RetryInterval {initialInterval, increaseAfter, maxInterval} = withLogSMP_ c server sndId "SEND (retrying)" $ \smp -> do diff --git a/src/Simplex/Messaging/Agent/Env/SQLite.hs b/src/Simplex/Messaging/Agent/Env/SQLite.hs index 5fbe69739..85877aa0d 100644 --- a/src/Simplex/Messaging/Agent/Env/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Env/SQLite.hs @@ -1,6 +1,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE NumericUnderscores #-} {-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-} module Simplex.Messaging.Agent.Env.SQLite where @@ -25,9 +26,38 @@ data AgentConfig = AgentConfig tbqSize :: Natural, dbFile :: FilePath, dbPoolSize :: Int, - smpCfg :: SMPClientConfig + smpCfg :: SMPClientConfig, + retryInterval :: RetryInterval } +minute :: Int +minute = 60_000_000 + +data RetryInterval = RetryInterval + { initialInterval :: Int, + increaseAfter :: Int, + maxInterval :: Int + } + +defaultAgentConfig :: AgentConfig +defaultAgentConfig = + AgentConfig + { tcpPort = "5224", + smpServers = undefined, + rsaKeySize = 2048 `div` 8, + connIdBytes = 12, + tbqSize = 16, + dbFile = "smp-agent.db", + dbPoolSize = 4, + smpCfg = smpDefaultConfig, + retryInterval = + RetryInterval + { initialInterval = 1_000_000, + increaseAfter = minute, + maxInterval = 10 * minute + } + } + data Env = Env { config :: AgentConfig, store :: SQLiteStore, diff --git a/tests/SMPAgentClient.hs b/tests/SMPAgentClient.hs index f30702cf6..af3093ca0 100644 --- a/tests/SMPAgentClient.hs +++ b/tests/SMPAgentClient.hs @@ -141,20 +141,18 @@ smpAgentTest3_1_1 test' = smpAgentTestN_1 3 _test cfg :: AgentConfig cfg = - AgentConfig + defaultAgentConfig { tcpPort = agentTestPort, smpServers = L.fromList ["localhost:5000#KXNE1m2E1m0lm92WGKet9CL6+lO742Vy5G6nsrkvgs8="], - rsaKeySize = 2048 `div` 8, - connIdBytes = 12, tbqSize = 1, dbFile = testDB, - dbPoolSize = 4, smpCfg = smpDefaultConfig { qSize = 1, defaultTransport = (testPort, transport @TCP), tcpTimeout = 500_000 - } + }, + retryInterval = (retryInterval defaultAgentConfig) {initialInterval = 50_000} } withSmpAgentThreadOn :: (MonadUnliftIO m, MonadRandom m) => ATransport -> (ServiceName, ServiceName, String) -> (ThreadId -> m a) -> m a