0

I would like to reuse a HTTPS connection for many requests to keep latency as low as possible for every request to the same host. Using the haskell package req, if I send too many requests in a short period of time then it seems I might be reaching a connection limit for the Manager and a new connection is started, showing higher latency. I can't change the connection limit in the req package for the Manager to fix this. Instead the package documentation recommends to use withReqManager to reuse connections. But i cant comprehend how to use this function. Could someone explain to me how to use it to always reuse a connection for an explicit series of requests, please?

Another example where the connection is not reused is when too much time passes between requests to the same host. When i delay a request for 5 seconds the connection is reused but if i delay for 60 seconds it is not reused. If someone could provide an example using withReqManger to reuse the connection for every time i run ttst I'd really appreciate it.

lttest :: IO ()
lttest = do ttst
            threadDelay 5000000
            ttst
            threadDelay 60000000
            ttst 
       where ttst = do metm <- getCurrentTime
                       runReq defaultHttpConfig { httpConfigCheckResponse = \_ _ _ -> Nothing } $ do
                        v <- req GET (https "ifconfig.me") (NoReqBody) lbsResponse mempty
                        liftIO $ print (responseBody v :: Data.ByteString.Lazy.ByteString) 
                       metm2 <- getCurrentTime
                       print (diffUTCTime metm2 metm)

Edit: I think i may have found a way to ensure that the connection is reused, but it requires a small hack and not using the req package. As @WillemVanOnsem commented, the server will likely close the connection after some time. So I have to send a dummy request every few seconds to the same host to keep the connection alive. But then I still need to find a way to keep the connection alive from having many requests being sent over a small period. The wreq package has a module called Network.Wreq.Session. This module allows you to initialize a Session and do all of your requests on the same connection by passing the same Session to each request. So far this seems to be working. An important note is that your dummy request or any other request for that matter using the same Session should not occur at the same time. If they ever do occur at the same time, the connection wont be reused that time.

Anon
  • 375
  • 1
  • 7
  • Likely the connection will be closed at the server's end after waiting for a certain amount of time. – Willem Van Onsem Jul 27 '22 at 21:11
  • @WillemVanOnsem okay i can send a dummy request every few seconds to keep the server from doing that. But even then i still have the issue where if I send many requests, every second instead of every 10 seconds for example, that the connection is not always reused. Do you have any advice to address that? – Anon Jul 27 '22 at 21:17

0 Answers0