Created
August 12, 2015 08:12
-
-
Save Rydgel/938f8479f433accc9ca0 to your computer and use it in GitHub Desktop.
haskell async http requests example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS_GHC -Wall -O2 -threaded -with-rtsopts="-N" #-} | |
import Control.Concurrent | |
import Control.Concurrent.Async | |
import Control.Lens | |
import Control.Monad | |
import Data.ByteString.Lazy hiding (replicate) | |
import Data.List.Split | |
import Network.Wreq hiding (getWith) | |
import Network.Wreq.Session | |
data ThreadOptions = ThreadOptions { wreqOptions :: Options, interval :: Int } | |
data GlobalOptions = GlobalOptions { threadOptions :: ThreadOptions, threadCount :: Int } | |
defaultWreqOptions :: Options | |
defaultWreqOptions = defaults | |
defaultThreadOptions :: ThreadOptions | |
defaultThreadOptions = ThreadOptions { wreqOptions = defaultWreqOptions, interval = 0 } | |
defaultGlobalOptions :: GlobalOptions | |
defaultGlobalOptions = GlobalOptions { threadOptions = defaultThreadOptions, threadCount = 8 } | |
thread :: ThreadOptions -> [String] -> IO [ByteString] | |
thread options urls = withSession $ \session -> | |
mapM (\url -> const . flip (^.) responseBody <$> | |
getWith (wreqOptions options) session url | |
<*> threadDelay (interval options)) urls | |
threads :: GlobalOptions -> [String] -> IO [ByteString] | |
threads options urls = join <$> mapConcurrently (thread (threadOptions options)) (chunksOf (threadCount options) urls) | |
main :: IO () | |
main = do | |
contents <- threads defaultGlobalOptions (replicate 8 "https://httpbin.org/") | |
print contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment