Files
simplexmq/src/Simplex/Messaging/Server/Expiration.hs
T
Evgeny Poberezkin 9d83a9c017 configure message/file expiration time in INI file (#749)
* configure message/file expiration time in INI file

* correct comment

Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>

---------

Co-authored-by: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com>
2023-05-09 20:07:42 +01:00

29 lines
822 B
Haskell

{-# LANGUAGE NamedFieldPuns #-}
module Simplex.Messaging.Server.Expiration where
import Control.Monad.IO.Class
import Data.Int (Int64)
import Data.Time.Clock.System (SystemTime (..), getSystemTime)
data ExpirationConfig = ExpirationConfig
{ -- time after which the entity can be expired, seconds
ttl :: Int64,
-- interval to check expiration, seconds
checkInterval :: Int64
}
expireBeforeEpoch :: ExpirationConfig -> IO Int64
expireBeforeEpoch ExpirationConfig {ttl} = subtract ttl . systemSeconds <$> liftIO getSystemTime
showTTL :: Int64 -> String
showTTL s
| s' /= 0 = show s <> " seconds"
| ms' /= 0 = show ms <> " minutes"
| hs' /= 0 = show hs <> " hours"
| otherwise = show ds <> " days"
where
(ms, s') = s `divMod` 60
(hs, ms') = ms `divMod` 60
(ds, hs') = hs `divMod` 24