Skip to content

Instantly share code, notes, and snippets.

@DerekV
Last active August 29, 2015 14:20
Show Gist options
  • Save DerekV/6b80e487991dad9e5054 to your computer and use it in GitHub Desktop.
Save DerekV/6b80e487991dad9e5054 to your computer and use it in GitHub Desktop.
Do a network call at random intervals using a Poisson progression
import Network.HTTP
import Network.URI (parseURI)
import System.Random
import Control.Concurrent
typicalDelayInSecs = fromIntegral 5
rate = recip typicalDelayInSecs
poissonDelay rate x = - (log x) / rate
poissonProcess g r = map (poissonDelay r) (randoms g :: [Float])
doThenWait stuff delayInSecs = do
stuff
threadDelay delay
where
delay = 1000000 * delayInSecs
uri = case parseURI "http://requestb.in/uactskua" of
Just u -> u
mkPostRq body = Request { rqURI = uri
, rqMethod = POST
, rqHeaders = [ mkHeader HdrContentType "application/json"
, mkHeader HdrContentLength $ show . length $ body]
, rqBody = body
}
showStatus (2,_,_) = print "OK"
showStatus (_,_,_) = print "Fail"
makeCall = do
status <- simpleHTTP (mkPostRq "{}")
>>= getResponseCode
showStatus status
poissonSequenceInSecs g = map round $ poissonProcess g rate
callThenSleep = doThenWait makeCall
main:: IO ()
main = do
g <- newStdGen
let delays = poissonSequenceInSecs g
mapM_ callThenSleep delays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment