lib/zold/http.rb in zold-0.14.41 vs lib/zold/http.rb in zold-0.14.42

- old
+ new

@@ -20,10 +20,11 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'rainbow' require 'uri' +require 'timeout' require 'net/http' require_relative 'backtrace' require_relative 'version' require_relative 'type' @@ -52,14 +53,14 @@ # HTTP header we add, in order to inform the node about our # protocol. PROTOCOL_HEADER = 'X-Zold-Protocol' # Read timeout in seconds - READ_TIMEOUT = 32 + READ_TIMEOUT = 4 # Connect timeout in seconds - CONNECT_TIMEOUT = 8 + CONNECT_TIMEOUT = 4 # @todo #98:30m/DEV The following two statements are seen as issues by rubocop # raising a Lint/AmbiguousBlockAssociation offense. It is somthing # that could be solved by changing the TargetRubyVersion in .rubocop.yml # that is already taken care of in another issue. I am leaving a todo @@ -74,11 +75,13 @@ http.use_ssl = uri.scheme == 'https' http.read_timeout = Http::READ_TIMEOUT http.open_timeout = Http::CONNECT_TIMEOUT path = uri.path path += '?' + uri.query if uri.query - http.request_get(path, headers) + Timeout.timeout(Http::READ_TIMEOUT + Http::CONNECT_TIMEOUT) do + http.request_get(path, headers) + end rescue StandardError => e Error.new(e) end def put(body) @@ -86,16 +89,18 @@ http.use_ssl = uri.scheme == 'https' http.read_timeout = Http::READ_TIMEOUT http.open_timeout = Http::CONNECT_TIMEOUT path = uri.path path += '?' + uri.query if uri.query - http.request_put( - path, body, - headers.merge( - 'Content-Type': 'text/plain', - 'Content-Length': body.length.to_s + Timeout.timeout(Http::READ_TIMEOUT + Http::CONNECT_TIMEOUT) do + http.request_put( + path, body, + headers.merge( + 'Content-Type': 'text/plain', + 'Content-Length': body.length.to_s + ) ) - ) + end rescue StandardError => e Error.new(e) end private