Module: ThinkingSphinx::Connection

Defined in:
lib/thinking_sphinx/connection.rb

Defined Under Namespace

Classes: Client, JRuby, MRI

Constant Summary collapse

MAXIMUM_RETRIES =
3

Class Method Summary collapse

Class Method Details

.clearObject



19
20
21
# File 'lib/thinking_sphinx/connection.rb', line 19

def self.clear
  @pool = nil
end

.connection_classObject



23
24
25
26
27
# File 'lib/thinking_sphinx/connection.rb', line 23

def self.connection_class
  return ThinkingSphinx::Connection::JRuby if RUBY_PLATFORM == 'java'

  ThinkingSphinx::Connection::MRI
end

.newObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/thinking_sphinx/connection.rb', line 6

def self.new
  configuration = ThinkingSphinx::Configuration.instance

  options = {
    :host      => configuration.searchd.address,
    :port      => configuration.searchd.mysql41,
    :socket    => configuration.searchd.socket,
    :reconnect => true
  }.merge(configuration.settings['connection_options'] || {})

  connection_class.new options
end

.persistent=(persist) ⇒ Object



64
65
66
# File 'lib/thinking_sphinx/connection.rb', line 64

def self.persistent=(persist)
  @persistent = persist
end

.persistent?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/thinking_sphinx/connection.rb', line 60

def self.persistent?
  @persistent
end

.poolObject



29
30
31
32
33
34
# File 'lib/thinking_sphinx/connection.rb', line 29

def self.pool
  @pool ||= Innertube::Pool.new(
    Proc.new { ThinkingSphinx::Connection.new },
    Proc.new { |connection| connection.close! }
  )
end

.takeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/thinking_sphinx/connection.rb', line 36

def self.take
  retries  = 0
  original = nil
  begin
    pool.take do |connection|
      begin
        yield connection
      rescue ThinkingSphinx::QueryExecutionError, connection.base_error => error
        original = ThinkingSphinx::SphinxError.new_from_mysql error
        retries += MAXIMUM_RETRIES if original.is_a?(ThinkingSphinx::QueryError)
        raise Innertube::Pool::BadResource
      end
    end
  rescue Innertube::Pool::BadResource
    retries += 1
    raise original unless retries < MAXIMUM_RETRIES

    ActiveSupport::Notifications.instrument(
      "message.thinking_sphinx", :message => "Retrying query \"#{original.statement}\" after error: #{original.message}"
    )
    retry
  end
end