Class: Errdb::Server
- Inherits:
-
Object
- Object
- Errdb::Server
- Defined in:
- lib/errdb.rb
Constant Summary collapse
- RETRY_DELAY =
The amount of time to wait before attempting to re-establish a connection with a server that is marked dead.
30.0
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
The host the errdb server is running on.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
The port the errdb server is listening on.
-
#retry ⇒ Object
readonly
The time of next retry if the connection is dead.
-
#status ⇒ Object
readonly
A text status string describing the state of the server.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check whether the server connection is alive.
-
#close ⇒ Object
Close the connection to the errdb server targeted by this object.
- #connect_to(host, port, timeout = nil) ⇒ Object
-
#initialize(errdb, host, port = DEFAULT_PORT) ⇒ Server
constructor
Create a new Errdb::Server object for the errdb instance listening on the given host and port.
-
#inspect ⇒ Object
Return a string representation of the server object.
-
#mark_dead(error) ⇒ Object
Mark the server as dead and close its socket.
-
#socket ⇒ Object
Try to connect to the errdb server targeted by this object.
Constructor Details
#initialize(errdb, host, port = DEFAULT_PORT) ⇒ Server
Create a new Errdb::Server object for the errdb instance listening on the given host and port
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/errdb.rb', line 248 def initialize(errdb, host, port = DEFAULT_PORT) raise ArgumentError, "No host specified" if host.nil? or host.empty? raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero? @host = host @port = port.to_i @sock = nil @retry = nil @status = 'NOT CONNECTED' @timeout = errdb.timeout @logger = errdb.logger end |
Instance Attribute Details
#host ⇒ Object (readonly)
The host the errdb server is running on.
225 226 227 |
# File 'lib/errdb.rb', line 225 def host @host end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
242 243 244 |
# File 'lib/errdb.rb', line 242 def logger @logger end |
#port ⇒ Object (readonly)
The port the errdb server is listening on.
230 231 232 |
# File 'lib/errdb.rb', line 230 def port @port end |
#retry ⇒ Object (readonly)
The time of next retry if the connection is dead.
235 236 237 |
# File 'lib/errdb.rb', line 235 def retry @retry end |
#status ⇒ Object (readonly)
A text status string describing the state of the server.
240 241 242 |
# File 'lib/errdb.rb', line 240 def status @status end |
Instance Method Details
#alive? ⇒ Boolean
Check whether the server connection is alive. This will cause the socket to attempt to connect if it isn’t already connected and or if the server was previously marked as down and the retry time has been exceeded.
275 276 277 |
# File 'lib/errdb.rb', line 275 def alive? !!socket end |
#close ⇒ Object
Close the connection to the errdb server targeted by this object. The server is not considered dead.
339 340 341 342 343 344 |
# File 'lib/errdb.rb', line 339 def close @sock.close if @sock && !@sock.closed? @sock = nil @retry = nil @status = "NOT CONNECTED" end |
#connect_to(host, port, timeout = nil) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/errdb.rb', line 305 def connect_to(host, port, timeout=nil) sock = nil if timeout ErrdbTimer.timeout(timeout) do sock = TCPSocket.new(host, port) end else sock = TCPSocket.new(host, port) end io = Errdb::BufferedIO.new(sock) io.read_timeout = timeout # Getting reports from several customers, including 37signals, # that the non-blocking timeouts in 1.7.5 don't seem to be reliable. # It can't hurt to set the underlying socket timeout also, if possible. if timeout secs = Integer(timeout) usecs = Integer((timeout - secs) * 1_000_000) optval = [secs, usecs].pack("l_2") begin io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval rescue Exception => ex # Solaris, for one, does not like/support socket timeouts. @logger.info "[errdb-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.}" if @logger end end io end |
#inspect ⇒ Object
Return a string representation of the server object.
265 266 267 |
# File 'lib/errdb.rb', line 265 def inspect "<Errdb::Server: %s:%d (%s)>" % [@host, @port, @status] end |
#mark_dead(error) ⇒ Object
Mark the server as dead and close its socket.
349 350 351 352 353 354 355 356 |
# File 'lib/errdb.rb', line 349 def mark_dead(error) close @retry = Time.now + RETRY_DELAY reason = "#{error.class.name}: #{error.}" @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry @logger.info { @status } if @logger end |
#socket ⇒ Object
Try to connect to the errdb server targeted by this object. Returns the connected socket object on success or nil on failure.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/errdb.rb', line 283 def socket return @sock if @sock and not @sock.closed? @sock = nil # If the host was dead, don't retry for a while. return if @retry and @retry > Time.now # Attempt to connect if not already connected. begin @sock = connect_to(@host, @port, @timeout) @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 @retry = nil @status = 'CONNECTED' rescue SocketError, SystemCallError, IOError, Timeout::Error => err logger.warn { "Unable to open socket: #{err.class.name}, #{err.}" } if logger mark_dead err end return @sock end |