Class: ZabbixManager::HttpTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_manager/http_transport.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
60
DEFAULT_KEEP_ALIVE_TIMEOUT =
30
NETWORK_ERRORS =
[
  Timeout::Error, EOFError, IOError, SystemCallError, SocketError, OpenSSL::SSL::SSLError,
  Net::ProtocolError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HttpTransport

解析连接、超时和代理配置,并准备持久会话。



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zabbix_manager/http_transport.rb', line 21

def initialize(options)
  @options = options
  @uri = parse_uri(options.fetch(:url))
  @timeout = positive_number(options.fetch(:timeout, DEFAULT_TIMEOUT), :timeout)
  @open_timeout = positive_number(options.fetch(:open_timeout, @timeout), :open_timeout)
  @read_timeout = positive_number(options.fetch(:read_timeout, @timeout), :read_timeout)
  @write_timeout = positive_number(options.fetch(:write_timeout, @timeout), :write_timeout)
  @keep_alive_timeout = positive_number(
    options.fetch(:keep_alive_timeout, DEFAULT_KEEP_ALIVE_TIMEOUT), :keep_alive_timeout
  )
  @proxy_uri = proxy_uri(options)
  @mutex = Mutex.new
  @http = nil
  @pid = nil
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



18
19
20
# File 'lib/zabbix_manager/http_transport.rb', line 18

def uri
  @uri
end

Instance Method Details

#closeObject

显式关闭当前持久连接。



59
60
61
62
# File 'lib/zabbix_manager/http_transport.rb', line 59

def close
  @mutex.synchronize { disconnect }
  true
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

返回不包含代理凭据和请求配置的传输层摘要

Returns:

  • (String)


73
74
75
# File 'lib/zabbix_manager/http_transport.rb', line 73

def inspect
  "#<#{self.class} url=#{safe_url.inspect} persistent=true>"
end

#request(body, bearer_token: nil) ⇒ Object

串行发送请求,失败时关闭连接但不自动重放写操作。



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zabbix_manager/http_transport.rb', line 38

def request(body, bearer_token: nil)
  @mutex.synchronize do
    response = active_http.request(build_request(body, bearer_token))
    unless response.is_a?(Net::HTTPSuccess)
      raise TransportError, "HTTP Error: #{response.code} on #{safe_url}"
    end

    response.body
  rescue TransportError
    disconnect
    raise
  rescue *NETWORK_ERRORS => e
    disconnect
    raise TransportError, "#{e.class}: request failed for #{safe_url}"
  rescue StandardError
    disconnect
    raise
  end
end

#safe_urlObject

返回不包含 URL 凭据和查询串的安全地址。



65
66
67
68
# File 'lib/zabbix_manager/http_transport.rb', line 65

def safe_url
  port = @uri.port == @uri.default_port ? nil : ":#{@uri.port}"
  "#{@uri.scheme}://#{@uri.host}#{port}#{@uri.path}"
end