manage logging via file handle

reason: `appendFile` combined with lazy evaluation lead to exhaustion of
open file descriptors, as each file is opened again for each write and
due to lazy evaluation is kept open multiple times.
This commit is contained in:
Trolli Schmittlauch 2020-09-11 14:04:35 +02:00
parent da579a0756
commit 1fc264a226

View file

@ -32,10 +32,11 @@ import Data.Time.Clock.POSIX
import Data.Typeable (Typeable)
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types as HTTPT
import System.IO
import System.Random
import Text.Read (readEither)
import Formatting (float, format, (%), fixed)
import Formatting (fixed, float, format, (%))
import qualified Network.Wai.Handler.Warp as Warp
import Servant
import Servant.Client
@ -66,6 +67,7 @@ data PostService d = PostService
, httpMan :: HTTP.Manager
, statsQueue :: TQueue StatsEvent
, loadStats :: TVar RelayStats
, logFileHandle :: Handle
}
deriving (Typeable)
@ -96,6 +98,7 @@ instance DHT d => Service PostService d where
httpMan' <- HTTP.newManager HTTP.defaultManagerSettings
statsQueue' <- newTQueueIO
loadStats' <- newTVarIO emptyStats
loggingFile <- openFile (confLogfilePath conf) WriteMode
let
thisService = PostService
{ serviceConf = conf
@ -110,12 +113,13 @@ instance DHT d => Service PostService d where
, httpMan = httpMan'
, statsQueue = statsQueue'
, loadStats = loadStats'
, logFileHandle = loggingFile
}
port' = fromIntegral (confServicePort conf)
warpSettings = Warp.setPort port' . Warp.setHost (fromString . confServiceHost $ conf) $ Warp.defaultSettings
-- log a start message, this also truncates existing files
TxtI.writeFile (confLogfilePath conf) $ Txt.unlines
[ "# Starting mock relay implementation\n"
TxtI.hPutStrLn loggingFile $ Txt.unlines
[ "# Starting mock relay implementation"
, "#relay receive rate ;relay delivery rate ;instance publish rate ;instance fetch rate"
]
-- Run 'concurrently_' from another thread to be able to return the
@ -816,8 +820,8 @@ evaluateStatsThread serv statsAcc = getPOSIXTime >>= loop
-- and now what? write a log to file
-- format: total relayReceiveRates;total relayDeliveryRates;postFetchRate;postPublishRate
-- later: current (reported) load, target load
TxtI.appendFile (confLogfilePath . serviceConf $ serv) $
format (fixed 20 % ";" % fixed 20 % ";" % fixed 20 % ";" % fixed 20 % "\n")
TxtI.hPutStrLn (logFileHandle serv) $
format (fixed 20 % ";" % fixed 20 % ";" % fixed 20 % ";" % fixed 20)
(sum . relayReceiveRates $ rateStats)
(sum . relayDeliveryRates $ rateStats)
(postPublishRate rateStats)