diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-02-09 16:57:26 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-02-09 16:57:26 +0000 |
commit | 6a88498bf9175276aaf09976dfd19f312454fc05 (patch) | |
tree | bd1822bd970033a35e39e3ed8e1d12ae6bcd815a | |
parent | bb43a85dc855198746a901e8900cef184a0edcab (diff) | |
parent | b963018eec171be99a2658b6ea38ba9f6cb5cb55 (diff) | |
download | gitlab-shell-6a88498bf9175276aaf09976dfd19f312454fc05.tar.gz |
Merge branch 'net-read-timeout' into 'master'
Increase HTTP timeout and log request durations
On some GitLab deployments internal API calls regularly take more than
60 seconds (the default HTTP read timeout of Ruby's Net::HTTP). Until
we understand the cause of this slowness, by raising the client
timeout in gitlab-shell we can at least spare end users having to
retry their `git pull` or `git push`.
See merge request !37
-rw-r--r-- | config.yml.example | 1 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 26 | ||||
-rw-r--r-- | spec/gitlab_net_spec.rb | 2 |
3 files changed, 22 insertions, 7 deletions
diff --git a/config.yml.example b/config.yml.example index a842ea2..eff354d 100644 --- a/config.yml.example +++ b/config.yml.example @@ -17,6 +17,7 @@ gitlab_url: "http://localhost:8080" # See installation.md#using-https for additional HTTPS configuration details. http_settings: +# read_timeout: 300 # user: someone # password: somepass # ca_file: /etc/ssl/cert.pem diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 6f47938..71e113b 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -10,6 +10,9 @@ require_relative 'httpunix' class GitlabNet class ApiUnreachableError < StandardError; end + CHECK_TIMEOUT = 5 + READ_TIMEOUT = 300 + def check_access(cmd, repo, actor, changes) project_name = repo.gsub("'", "") project_name = project_name.gsub(/\.git\Z/, "") @@ -50,7 +53,7 @@ class GitlabNet end def check - get("#{host}/check") + get("#{host}/check", read_timeout: CHECK_TIMEOUT) end protected @@ -63,13 +66,15 @@ class GitlabNet "#{config.gitlab_url}/api/v3/internal" end - def http_client_for(uri) + def http_client_for(uri, options={}) if uri.is_a?(URI::HTTPUNIX) http = Net::HTTPUNIX.new(uri.hostname) else http = Net::HTTP.new(uri.host, uri.port) end + http.read_timeout = options[:read_timeout] || read_timeout + if uri.is_a?(URI::HTTPS) http.use_ssl = true http.cert_store = cert_store @@ -92,19 +97,24 @@ class GitlabNet request end - def request(method, url, params = {}) + def request(method, url, params = {}, options={}) $logger.debug "Performing #{method.to_s.upcase} #{url}" uri = URI.parse(url) - http = http_client_for(uri) + http = http_client_for(uri, options) request = http_request_for(method, uri, params) begin + start_time = Time.new response = http.start { http.request(request) } rescue => e $logger.warn "Failed to connect to internal API <#{method.to_s.upcase} #{url}>: #{e.inspect}" raise ApiUnreachableError + ensure + $logger.info do + sprintf('%s %s %0.5f', method.to_s.upcase, url, Time.new - start_time) + end end if response.code == "200" @@ -116,8 +126,8 @@ class GitlabNet response end - def get(url) - request(:get, url) + def get(url, options={}) + request(:get, url, {}, options) end def post(url, params) @@ -144,4 +154,8 @@ class GitlabNet def secret_token @secret_token ||= File.read config.secret_file end + + def read_timeout + config.http_settings['read_timeout'] || READ_TIMEOUT + end end diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index 3c2cea2..2d9b544 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -143,7 +143,7 @@ describe GitlabNet, vcr: true do subject { gitlab_net.send :http_client_for, URI('https://localhost/') } before do gitlab_net.stub! :cert_store - gitlab_net.send(:config).http_settings.stub(:[]).with('self_signed_cert') { true } + gitlab_net.send(:config).stub(:http_settings) { {'self_signed_cert' => true} } end its(:verify_mode) { should eq(OpenSSL::SSL::VERIFY_NONE) } |