Class: Traject::SolrPool::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/traject/solr_pool/connection.rb

Overview

Reusable persistent-connection seam over http_connection_pool. Owns origin binding and pool_options construction; exposes post/get that borrow a pooled HTTP::Session. A future reader can reuse this unchanged.

Defined Under Namespace

Classes: Options

Constant Summary collapse

TRANSPORT_ERRORS =

Transport-level failures that mean the server is unreachable. A returned HTTP response of ANY status (2xx/4xx/5xx) is NOT in this set — it proves the server answered. Kept independent of the writer's skippable list.

[
  HTTP::TimeoutError,
  HttpConnectionPool::TimeoutError,
  HTTP::ConnectionError,
  SocketError,
  Errno::ECONNREFUSED
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin:, pool_size:, pool_timeout: nil, **opts) ⇒ Connection

Returns a new instance of Connection.



34
35
36
37
38
39
40
41
42
# File 'lib/traject/solr_pool/connection.rb', line 34

def initialize(origin:, pool_size:, pool_timeout: nil, **opts)
  @origin       = origin
  @pool_size    = pool_size
  @pool_timeout = pool_timeout
  @pool_options = Options.new(headers: opts.fetch(:headers, {}),
                              auth: opts[:auth],
                              timeout: opts[:timeout]).to_pool_opts
  @adapter      = build_adapter
end

Instance Attribute Details

#originObject (readonly)

Returns the value of attribute origin.



32
33
34
# File 'lib/traject/solr_pool/connection.rb', line 32

def origin
  @origin
end

Instance Method Details

#get(path, params: {}, timeout: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/traject/solr_pool/connection.rb', line 48

def get(path, params: {}, timeout: nil)
  @adapter.with_connection do |conn|
    client = timeout ? conn.timeout(timeout) : conn
    if params.empty?
      client.get(path)
    else
      client.get(path, params: params)
    end
  end
end

#head(path, timeout: nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/traject/solr_pool/connection.rb', line 59

def head(path, timeout: nil)
  @adapter.with_connection do |conn|
    client = timeout ? conn.timeout(timeout) : conn
    client.head(path)
  end
end

#inspectObject Also known as: to_s

Metadata only: never print header values or the auth credential.



86
87
88
89
# File 'lib/traject/solr_pool/connection.rb', line 86

def inspect
  "#<#{self.class} origin=#{@origin} pool_size=#{@pool_size} " \
    "options=[#{@pool_options.keys.join(', ')}]>"
end

#ping(path, timeout: nil) ⇒ Object

Raw health probe: returns the HTTP::Response (any status) so callers can inspect it. Raises on transport failure. HEAD has no body to drain, so it leaves the pooled persistent socket clean for reuse.



69
70
71
# File 'lib/traject/solr_pool/connection.rb', line 69

def ping(path, timeout: nil)
  head(path, timeout: timeout)
end

#post(path, body:) ⇒ Object



44
45
46
# File 'lib/traject/solr_pool/connection.rb', line 44

def post(path, body:)
  @adapter.with_connection { |conn| conn.post(path, body: body) }
end

#ready?(path, timeout: nil) ⇒ Boolean

Liveness predicate. Any HTTP response means reachable -> true. Only a transport failure means down -> false. Never raises.

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/traject/solr_pool/connection.rb', line 75

def ready?(path, timeout: nil)
  !ping(path, timeout: timeout).nil?
rescue *TRANSPORT_ERRORS
  false
end

#releaseObject



81
82
83
# File 'lib/traject/solr_pool/connection.rb', line 81

def release
  @adapter.release_connection_pool
end