Class: Sphinx::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sphinx/server.rb

Overview

Represents an instance of searchd server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sphinx, host, port, path) ⇒ Server

Creates a new instance of Server.

Parameters:

  • sphinx – an instance of Sphinx::Client.

  • host – name of host where search is running (if path is not specified).

  • port – searchd port (if path is not specified).

  • path – an absolute path to the UNIX socket.


22
23
24
25
26
27
28
29
# File 'lib/sphinx/server.rb', line 22

def initialize(sphinx, host, port, path)
  @sphinx = sphinx
  @host = host
  @port = port
  @path = path

  @socket = nil
end

Instance Attribute Details

#hostObject (readonly)

The host the Sphinx server is running on


6
7
8
# File 'lib/sphinx/server.rb', line 6

def host
  @host
end

#pathObject (readonly)

The path to UNIX socket where Sphinx server is running on


12
13
14
# File 'lib/sphinx/server.rb', line 12

def path
  @path
end

#portObject (readonly)

The port the Sphinx server is listening on


9
10
11
# File 'lib/sphinx/server.rb', line 9

def port
  @port
end

Instance Method Details

#close_persistent!Object

Closes persistent socket.


107
108
109
# File 'lib/sphinx/server.rb', line 107

def close_persistent!
  free_socket(@socket, true)
end

#free_socket(socket, force = false) ⇒ Object

Closes previously opened socket.

Pass socket retrieved with get_socket method when finished work. It does not close persistent sockets, but if really you need to do it, pass true as force parameter value.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sphinx/server.rb', line 74

def free_socket(socket, force = false)
  # Socket has not been open
  return false if socket.nil?

  # Do we try to close persistent socket?
  if socket == @socket
    # do not close it if not forced
    if force
      @socket.close unless @socket.closed?
      @socket = nil
      true
    else
      false
    end
  else
    # Just close this socket
    socket.close unless socket.closed?
    true
  end
end

#get_socket(&block) ⇒ Object

Gets the opened socket to the server.

You can pass a block to make any connection establishing related things, like protocol version interchange. They will be treated as a part of connection routine, so connection timeout will include them.

In case of connection error, SphinxConnectError exception will be raised.

Method returns opened socket, so do not forget to close it using free_socket method. Make sure you will close socket in case of any emergency.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sphinx/server.rb', line 42

def get_socket(&block)
  if persistent?
    yield @socket
    @socket
  else
    socket = nil
    Sphinx::safe_execute(@sphinx.timeout) do
      socket = establish_connection

      # Do custom initialization
      yield socket if block_given?
    end
    socket
  end
rescue SocketError, SystemCallError, IOError, EOFError, ::Timeout::Error, ::Errno::EPIPE => e
  # Close previously opened socket (in case of it has been really opened)
  free_socket(socket, true)

  error = "connection to #{to_s} failed ("
  if e.kind_of?(SystemCallError)
    error << "errno=#{e.class::Errno}, "
  end
  error << "msg=#{e.message})"
  raise Sphinx::SphinxConnectError, error
end

#inspectObject

Returns a string representation of the sphinx server object.


124
125
126
# File 'lib/sphinx/server.rb', line 124

def inspect
  "<Sphinx::Server: %s%s>" % [to_s, @socket ? '; persistent' : '']
end

#make_persistent!(socket) ⇒ Object

Makes specified socket persistent.

Previous persistent socket will be closed as well.


98
99
100
101
102
103
104
# File 'lib/sphinx/server.rb', line 98

def make_persistent!(socket)
  unless socket == @socket
    close_persistent!
    @socket = socket
  end
  @socket
end

#persistent?Boolean

Gets a value indicating whether server has persistent socket associated.

Returns:

  • (Boolean)

112
113
114
# File 'lib/sphinx/server.rb', line 112

def persistent?
  !@socket.nil?
end

#to_sObject

Returns a string representation of the sphinx server object.


118
119
120
# File 'lib/sphinx/server.rb', line 118

def to_s
  @path || "#{@host}:#{@port}"
end