mechanize open_timeout,keep_alive(),idle_timeout()

http://mechanize.rubyforge.org/Mechanize.html

idle_timeout()
Connections that have not been used in this many seconds will be reset.
idle_timeout=(idle_timeout)
Sets the idle timeout to idle_timeout. The default timeout is 5 seconds. If you experience “too many connection resets”, reducing this value may help.
デフォルトの維持時間は5秒。設定秒数を小さくすると "too many connection resets" のエラーが起こる頻度を抑制することができるが、通信効率は下がる。秒数を大きくする(というか、そのままにする)と特定のサーバではエラーが出まくりになりますが、その他のサーバでは通信効率がよくなります。通信先のサーバの設定によるようなので、エラーが出る場合にのみ設定を変更するようにしてください。



keep_alive() click to toggle source
Are HTTP/1.1 keep-alive connections enabled?
keep_alive=(enable)
Disable HTTP/1.1 keep-alive connections if enable is set to false. If you are experiencing “too many connection resets” errors setting this to false will eliminate them.
You should first investigate reducing idle_timeout.


open_timeout()
Length of time to wait until a connection is opened in seconds
open_timeout=(open_timeout)
Sets the connection open timeout to open_timeout
read_timeout()
Length of time to wait for data from the server
read_timeout=(read_timeout)
Sets the timeout for each chunk of data read from the server to read_timeout. A single request may read many chunks of data.


retry_change_requests=(retry_change_requests)
When setting retry_change_requests to true you are stating that, for all the URLs you access with mechanize, making POST and other non-idempotent requests is safe and will not cause data duplication or other harmful results.
If you are experiencing “too many connection resets” errors you should instead investigate reducing the #idle_timeout or disabling #keep_alive connections.






                                                                                                                              • -

http://seesaawiki.jp/ruby_mechanize/d/Mechanize

open_timeout=(sec)

内部で使用される Net::HTTP#open_timeout の秒数を Integer で設定します。
agent.open_timeout = 5 # 5sec

設定を行わなかった場合の内部値は nil で、TCPSocket.open が利用している C ライブラリの接続タイムアウトを待ちます。なお、TCPSocket.open を timeout(sec){ ... } で包んでいるだけなので、DNS 解決(gethostbyname)などのタイムアウトを制御することはできません。
DNS 解決のタイムアウトを制御したい場合は Ruby 実装を使用させるために require 'resolv-replace' した上で open_timeout= を設定してください。
resolv-replace 下でタイムアウトを迎えた場合は Ruby 標準の例外 Timeout::Error が発生します。open timeout と read timeout を発生する例外で区別することはできません。$!.backtrace を正規表現でマッチさせるなどの小細工が必要になります。

...
require 'resolv-replace'
...
agent.open_timeout = 3
agent.read_timeout = 10
begin
agent.get(uri)
rescue TimeoutError => ex
case ex.backtrace
when /protocol.rb/
warn "read timenout: 処理サーバが落ちてます"
else
warn "open timenout: 回線が抜けてるかDNSエラーです"
end
end
end

DNS は引けるしリクエストも送れるがサーバからの返事が来ない、という時のタイムアウトの設定は #read_timeout= で行います。
open_timeout

ユーザーが #open_timeout= で設定した接続待ち病数を Integer で返します。設定を行っていないときの返り値は nil です。
read_timeout=(sec)

内部で使用される Net::HTTP#read_timeout の秒数を Integer で設定します。
agent.read_timeout = 5 # 5sec
設定を行わなかった場合の内部値は 60 で、リクエスト送信後 60 秒以内にサーバからレスポンスがない場合は Ruby 標準の例外 Timeout::Error を発生させます。
そもそもサーバが見つからないときのタイムアウトを設定したい、という場合は #open_timeout= を参照してください。
read_timeout

ユーザーが #read_timeout= で設定した送信レスポンス待ち秒数を Integer で返します。設定を行っていないときの返り値は nil です。