Skip to content

Commit 4386dd8

Browse files
authored
smp server: option to expire messages when new message is sent (now off by default) (#1625)
* smp server: option to expire messages when new message is sent (now off by default) * fix test
1 parent 50ddd63 commit 4386dd8

File tree

7 files changed

+13
-9
lines changed

7 files changed

+13
-9
lines changed

src/Simplex/Messaging/Protocol.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ instance Encoding BrokerErrorType where
20222022
RESPONSE e -> "RESPONSE " <> smpEncode e
20232023
UNEXPECTED e -> "UNEXPECTED " <> smpEncode e
20242024
TRANSPORT e -> "TRANSPORT " <> smpEncode e
2025-
NETWORK e -> "NETWORK" -- TODO once all upgrade: "NETWORK " <> smpEncode e
2025+
NETWORK _e -> "NETWORK" -- TODO once all upgrade: "NETWORK " <> smpEncode e
20262026
TIMEOUT -> "TIMEOUT"
20272027
HOST -> "HOST"
20282028
NO_SERVICE -> "NO_SERVICE"
@@ -2042,7 +2042,7 @@ instance StrEncoding BrokerErrorType where
20422042
RESPONSE e -> "RESPONSE " <> encodeUtf8 (T.pack e)
20432043
UNEXPECTED e -> "UNEXPECTED " <> encodeUtf8 (T.pack e)
20442044
TRANSPORT e -> "TRANSPORT " <> smpEncode e
2045-
NETWORK e -> "NETWORK" -- TODO once all upgrade: "NETWORK " <> strEncode e
2045+
NETWORK _e -> "NETWORK" -- TODO once all upgrade: "NETWORK " <> strEncode e
20462046
TIMEOUT -> "TIMEOUT"
20472047
HOST -> "HOST"
20482048
NO_SERVICE -> "NO_SERVICE"

src/Simplex/Messaging/Server.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,10 +1848,10 @@ client
18481848
Right body -> do
18491849
when (isJust (queueData qr) && isSecuredMsgQueue qr) $ void $ liftIO $
18501850
deleteQueueLinkData (queueStore ms) q
1851-
ServerConfig {messageExpiration, msgIdBytes} <- asks config
1851+
ServerConfig {messageExpiration, expireMessagesOnSend, msgIdBytes} <- asks config
18521852
msgId <- randomId' msgIdBytes
18531853
msg_ <- liftIO $ runExceptT $ do
1854-
expireMessages messageExpiration stats
1854+
when expireMessagesOnSend $ mapM_ (expireMessages stats) messageExpiration
18551855
msg <- liftIO $ mkMessage msgId body
18561856
writeMsg ms q True msg
18571857
case msg_ of
@@ -1875,9 +1875,9 @@ client
18751875
msgTs <- getSystemTime
18761876
pure $! Message msgId msgTs msgFlags body
18771877

1878-
expireMessages :: Maybe ExpirationConfig -> ServerStats -> ExceptT ErrorType IO ()
1879-
expireMessages msgExp stats = do
1880-
deleted <- maybe (pure 0) (deleteExpiredMsgs ms q <=< liftIO . expireBeforeEpoch) msgExp
1878+
expireMessages :: ServerStats -> ExpirationConfig -> ExceptT ErrorType IO ()
1879+
expireMessages stats msgExp = do
1880+
deleted <- deleteExpiredMsgs ms q =<< liftIO (expireBeforeEpoch msgExp)
18811881
liftIO $ when (deleted > 0) $ atomicModifyIORef'_ (msgExpired stats) (+ deleted)
18821882

18831883
-- The condition for delivery of the message is:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ data ServerConfig s = ServerConfig
153153
-- | time after which the messages can be removed from the queues and check interval, seconds
154154
messageExpiration :: Maybe ExpirationConfig,
155155
expireMessagesOnStart :: Bool,
156+
expireMessagesOnSend :: Bool,
156157
-- | interval of inactivity after which journal queue is closed
157158
idleQueueInterval :: Int64,
158159
-- | notification expiration interval (seconds)

src/Simplex/Messaging/Server/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
428428
{ ttl = 86400 * readIniDefault defMsgExpirationDays "STORE_LOG" "expire_messages_days" ini
429429
},
430430
expireMessagesOnStart = fromMaybe True $ iniOnOff "STORE_LOG" "expire_messages_on_start" ini,
431+
expireMessagesOnSend = fromMaybe True $ iniOnOff "STORE_LOG" "expire_messages_on_send" ini,
431432
idleQueueInterval = defaultIdleQueueInterval,
432433
notificationExpiration =
433434
defaultNtfExpiration

src/Simplex/Messaging/Server/Main/Init.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ iniFileContent cfgPath logPath opts host basicAuth controlPortPwds =
8787
<> ("restore_messages: " <> onOff enableStoreLog <> "\n\n")
8888
<> "# Messages and notifications expiration periods.\n"
8989
<> ("expire_messages_days: " <> tshow defMsgExpirationDays <> "\n")
90-
<> "expire_messages_on_start: on\n"
90+
<> "expire_messages_on_start: on\n\
91+
\expire_messages_on_send: off\n"
9192
<> ("expire_ntfs_hours: " <> tshow defNtfExpirationHours <> "\n\n")
9293
<> "# Log daily server statistics to CSV file\n"
9394
<> ("log_stats: " <> onOff logStats <> "\n\n")

tests/SMPClient.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ cfgMS msType = withStoreCfg (testServerStoreConfig msType) $ \serverStoreCfg ->
229229
dailyBlockQueueQuota = 20,
230230
messageExpiration = Just defaultMessageExpiration,
231231
expireMessagesOnStart = True,
232+
expireMessagesOnSend = False,
232233
idleQueueInterval = defaultIdleQueueInterval,
233234
notificationExpiration = defaultNtfExpiration,
234235
inactiveClientExpiration = Just defaultInactiveClientExpiration,

tests/ServerTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ testMsgExpireOnSend =
12051205
it "should expire messages that are not received before messageTTL on SEND" $ \(ATransport (t :: TProxy c 'TServer), msType) -> do
12061206
g <- C.newRandom
12071207
(sPub, sKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g
1208-
let cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {messageExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 10000}}
1208+
let cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {expireMessagesOnSend = True, messageExpiration = Just ExpirationConfig {ttl = 1, checkInterval = 10000}}
12091209
withSmpServerConfigOn (ATransport t) cfg' testPort $ \_ ->
12101210
testSMPClient @c $ \sh -> do
12111211
(sId, rId, rKey, dhShared) <- testSMPClient @c $ \rh -> createAndSecureQueue rh sPub

0 commit comments

Comments
 (0)