diff --git a/source/Network/Xmpp/Utilities.hs b/source/Network/Xmpp/Utilities.hs index 87d9a91..c383715 100644 --- a/source/Network/Xmpp/Utilities.hs +++ b/source/Network/Xmpp/Utilities.hs @@ -28,6 +28,7 @@ import Prelude import System.IO.Unsafe(unsafePerformIO) import qualified Text.XML.Stream.Render as TXSR import Text.XML.Unresolved as TXU +import System.Random -- | Apply f with the content of tv as state, restoring the original value when an -- exception occurs @@ -92,3 +93,13 @@ hostnameP = do if Text.length label + 1 + Text.length r > 255 then fail "Hostname too long." else return $ Text.concat [label, Text.pack ".", r] + +-- The number of seconds to wait before reconnection attempts in accordance with +-- the truncated binary exponential backoff algorithm. +waitingTimes :: IO [Int] +waitingTimes = do + wait <- randomRIO (1, 59) + waits <- Prelude.mapM (\n -> randomRIO (0, wait * n)) slotTimes + return (wait:waits) + where + slotTimes = [1, 3, 8, 15, 24, 35, 48, 63, 80, 99, 99, 99, 99, 99, 99]