Class: Dalli::Server::KSocket

Inherits:
Socket
  • Object
show all
Defined in:
lib/dalli/socket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/dalli/socket.rb', line 2

def options
  @options
end

Class Method Details

.open(host, port, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dalli/socket.rb', line 4

def self.open(host, port, options = {})
  # All this ugly code to ensure proper Socket connect timeout
  addr = Socket.getaddrinfo(host, nil)
  sock = new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
  sock.options = { :host => host, :port => port }.merge(options)
  begin
    sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
  rescue Errno::EINPROGRESS
    resp = IO.select(nil, [sock], nil, sock.options[:timeout])
    begin
      sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
    rescue Errno::EISCONN
    end
  end
  sock
end

Instance Method Details

#readfull(count) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dalli/socket.rb', line 21

def readfull(count)
  value = ''
  begin
    loop do
      value << sysread(count - value.bytesize)
      break if value.bytesize == count
    end
  rescue Errno::EAGAIN, Errno::EWOULDBLOCK
    if IO.select([self], nil, nil, options[:timeout])
      retry
    else
      raise Timeout::Error, "IO timeout: #{options.inspect}"
    end
  end
  value
end