6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wreqでSSL通信が遅い場合の対処法

Posted at

wreq はデフォルトで http-client-tls を使ってSSL通信するのだけど、これが結構遅くてビビった1

そこで、 tls パッケージではなく OpenSSL の Haskell バインディングである HsOpenSSL に差し替えてみたら、手元で 40 秒かかっていたバッチの実行時間が 30 秒まで短くなった。 25% の改善。

差し替えるには、 HsOpenSSL パッケージと http-client-openssl パッケージへの依存を *.cabal へ追加して、

import qualified Network.Wreq as Wreq

main :: IO ()
main = do
  res <- Wreq.get uri
  ...

的な部分を、

import Control.Lens ((&), (.~))
import qualified Network.HTTP.Client.OpenSSL as COpenSSL
import qualified Network.Wreq as Wreq
import qualified OpenSSL
import OpenSSL.Session (context)

opts :: Wreq.Options
opts = Wreq.defaults
     & Wreq.manager .~ Left (COpenSSL.opensslManagerSettings context)

main :: IO ()
main = OpenSSL.withOpenSSL $ do
  res <- Wreq.getWith opts uri
  ...

としてやるとよい。ってことは、 wreqのドキュメント にさらっと書いてあった。

  1. プロファイルしてみた感じ Data.ByteString.Base64 周りで時間喰ってそう

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?