Module: ThinkingSphinx::Connection

Defined in:
lib/thinking_sphinx/connection.rb

Defined Under Namespace

Classes: JRuby, MRI

Class Method Summary collapse

Class Method Details

.connection_classObject



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

def self.connection_class
  raise "Sphinx's MySQL protocol does not work with JDBC." if RUBY_PLATFORM == 'java'
  return ThinkingSphinx::Connection::JRuby if RUBY_PLATFORM == 'java'

  ThinkingSphinx::Connection::MRI
end

.newObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/thinking_sphinx/connection.rb', line 2

def self.new
  configuration = ThinkingSphinx::Configuration.instance
  # If you use localhost, MySQL insists on a socket connection, but Sphinx
  # requires a TCP connection. Using 127.0.0.1 fixes that.
  address = configuration.searchd.address || '127.0.0.1'
  address = '127.0.0.1' if address == 'localhost'

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

  connection_class.new address, options[:port], options
end

.poolObject



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

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

.takeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/thinking_sphinx/connection.rb', line 31

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

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