Revert "batch message writes"

This reverts commit 61fb3c3b9e.
This commit is contained in:
Evgeny Poberezkin
2025-09-07 23:10:28 +01:00
parent 61fb3c3b9e
commit a9d3831ed4
+4 -13
View File
@@ -2113,28 +2113,19 @@ saveServerMessages drainMsgs = \case
exportMessages :: MsgStoreClass s => Bool -> s -> FilePath -> Bool -> IO ()
exportMessages tty ms f drainMsgs = do
logNote $ "saving messages to file " <> T.pack f
mVar <- newIORef []
liftIO $ withFile f WriteMode $ \h ->
tryAny (unsafeWithAllMsgQueues tty True ms $ saveQueueMsgs h mVar) >>= \case
Right (Sum total) -> do
qMsgs <- readIORef mVar
unless (null qMsgs) $ saveMessages h qMsgs
logNote $ "messages saved: " <> tshow total
tryAny (unsafeWithAllMsgQueues tty True ms $ saveQueueMsgs h) >>= \case
Right (Sum total) -> logNote $ "messages saved: " <> tshow total
Left e -> do
logError $ "error exporting messages: " <> tshow e
exitFailure
where
saveQueueMsgs h mVar q = do
saveQueueMsgs h q = do
msgs <-
unsafeRunStore q "saveQueueMsgs" $
getQueueMessages_ drainMsgs q =<< getMsgQueue ms q False
unless (null msgs) $ do
qMsgs <- ((recipientId q, msgs) :) <$> readIORef mVar
if length qMsgs `mod` 100 > 0
then writeIORef mVar qMsgs
else writeIORef mVar [] >> saveMessages h qMsgs
unless (null msgs) $ BLD.hPutBuilder h $! encodeMessages (recipientId q) msgs
pure $ Sum $ length msgs
saveMessages h = BLD.hPutBuilder h . mconcat . map (uncurry encodeMessages) . reverse
encodeMessages rId = mconcat . map (\msg -> BLD.byteString (strEncode $ MLRv3 rId msg) <> BLD.char8 '\n')
processServerMessages :: forall s'. StartOptions -> M s' (Maybe MessageStats)