EC2で最初の接続だけ異常に重く、一度接続すると、サクサク動く。
何故だ?curlで計測したところ、以下の様に最初の接続に130秒もかかっている。
$ curl ‘https://*.com/login’ -H ‘Accept-Encoding: gzip, deflate, sdch’ ept-Language: en-US,en;q=0.8,ja;q=0.6′ -H ‘Upgrade-Insecure-Requests: 1’ -H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36’ -H ‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8’ -H ‘Connection: keep-alive’ –compressed -o /dev/null -w  “%{time_starttransfer}\n” -s
130.982826
最初、メモリが問題かと思って、増設し、さらにswapもつけたが、それでも変わらない。
$ df
ファイルシス   1K-ブロック     使用  使用可 使用% マウント位置
devtmpfs            492676        0  492676    0% /dev
tmpfs               503444        0  503444    0% /dev/shm
tmpfs               503444      524  502920    1% /run
tmpfs               503444        0  503444    0% /sys/fs/cgroup
/dev/xvda1        18862060 13096136 5765924   70% /
tmpfs               100692        0  100692    0% /run/user/1000
$ free
              total        used        free      shared  buff/cache   available
Mem:        1006892      713376       72396         352      221120      154016
Swap:       1048572         768     1047804
明らかにおかしいと思ったら、2つのsubnetのうち1つがinternet gatewayにアタッチされていなかったのが原因。
AWS Elastic Load Balancing: Seeing extremely long initial connection time
https://stackoverflow.com/questions/35523421/aws-elastic-load-balancing-seeing-extremely-long-initial-connection-time
ALBだとAZで2つのsubnetの設定が必要だが、今回はインスタンスは1つしか使わないので、internet gatewayには1つしかアタッチしてなかった。もう一つのpublic subnetをigwにアタッチしたところ、0.63msに改善
$ curl ‘https://*.com/login’ -H ‘Accept-Encoding: gzip, deflate, sdch’ -H ‘Accept-Language: en-US,en;q=0.8,ja;q=0.6’ -H ‘Upgrade-Insecure-Requests: 1’ -H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36’ -H ‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8’ -H ‘Connection: keep-alive’ –compressed -o /dev/null -w  “%{time_starttransfer}\n” -s
0.638270
メモリの問題じゃなかったああああああああああああああああああ 
なんてこったい
 
					 
