Commit 91419f24 authored by Joris's avatar Joris

Add noise to sleep duration

parent 317e7b1e
......@@ -24,6 +24,7 @@ Library
, text
, time
, wreq
, random
Exposed-modules:
FetchAd
......
......@@ -71,9 +71,10 @@ sleepUntilReady conf = do
Just d -> do
sleepSeconds d
Nothing ->
-- TODO 04/09/2019: Add noise
sleepSeconds . Conf.listenInterval $ conf
Nothing -> do
duration <- TimeUtils.addNoise (Conf.listenInterval conf) (Conf.listenIntervalNoise conf)
putStrLn . show $ duration
sleepSeconds duration
where
sleepSeconds =
threadDelay . (*) 1000000 . round
module Utils.Time
( getCurrentFormattedTime
, asleepDuration
, addNoise
) where
import Data.Text (Text)
......@@ -9,6 +10,7 @@ import Data.Time.Clock (DiffTime)
import qualified Data.Time.Clock as Clock
import qualified Data.Time.Format as Format
import qualified Data.Time.LocalTime as LocalTime
import qualified System.Random as Random
getCurrentFormattedTime :: IO Text
getCurrentFormattedTime = do
......@@ -27,3 +29,13 @@ asleepDuration from to t =
Nothing
where
day = (realToFrac Clock.nominalDay) :: DiffTime
-- |Add noise to a duration.
-- > t - (n / 2) <= addNoise t n <= t + (n / 2)
addNoise :: DiffTime -> DiffTime -> IO DiffTime
addNoise t n = do
r <- Random.randomIO :: IO Float
return $ t + (Clock.secondsToDiffTime . round $ (r * (fromIntegral . toSeconds $ n))) - n / 2
where
toSeconds dt = Clock.diffTimeToPicoseconds dt `div` 10^(12 :: Integer)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment