packages feed

pgqueuer-hs-0.1.0.0 / src / PGQueuer / Worker / Backoff.hs

{- |Module      : PGQueuer.Worker.BackoffDescription : Retry backoff and jitter calculations.Copyright   : (c) Tushar Adhatrao, 2026License     : MITMaintainer  : tusharadhatrao@gmail.comStability   : experimentalThis module provides data types and functions to calculate backoff delaysand apply jitter for retrying failed jobs.-}module PGQueuer.Worker.Backoff  ( BackoffStrategy (..)  , Jitter (..)  , calculateBackoff  , applyJitter  ) whereimport Data.Time.Clock (NominalDiffTime)import System.Random (randomRIO)import PGQueuer.Types (BackoffStrategy (..), Jitter (..)){- | Calculate the base backoff delay before applying jitter.The attempt parameter should be the number of attempts already made (e.g., if failing on the first attempt, attempts=1).-}calculateBackoff :: BackoffStrategy -> Int -> NominalDiffTimecalculateBackoff (Constant delay) _ = delaycalculateBackoff (Linear base cap) attempts =  let calculated = base * fromIntegral attempts   in min cap calculatedcalculateBackoff (Exponential base cap) attempts =  -- For exponential, commonly delay = base * 2^(attempts - 1)  let calculated = base * (2 ^ (attempts - 1))   in min cap calculated-- | Apply jitter to a calculated delay.applyJitter :: Jitter -> NominalDiffTime -> IO NominalDiffTimeapplyJitter NoJitter delay = return delayapplyJitter FullJitter delay = do  let maxMs = round ((realToFrac delay :: Double) * 1000) :: Int  if maxMs <= 0    then return delay    else do      jitterMs <- randomRIO (0, maxMs)      return $ realToFrac (fromIntegral jitterMs :: Double) / 1000applyJitter EqualJitter delay = do  let halfDelay = delay / 2      maxMs = round ((realToFrac halfDelay :: Double) * 1000) :: Int  if maxMs <= 0    then return delay    else do      jitterMs <- randomRIO (0, maxMs)      return $ halfDelay + (realToFrac (fromIntegral jitterMs :: Double) / 1000)