Class: Redis::Connection::Synchrony

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/redis/connection/synchrony.rb

Constant Summary

Constants included from CommandHelper

CommandHelper::COMMAND_DELIMITER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#build_command

Constructor Details

#initialize(connection) ⇒ Synchrony

Returns a new instance of Synchrony.



109
110
111
# File 'lib/redis/connection/synchrony.rb', line 109

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(config) ⇒ Object

Raises:

  • (Errno::ECONNREFUSED)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/redis/connection/synchrony.rb', line 79

def self.connect(config)
  if config[:scheme] == "unix"
    begin
      conn = EventMachine.connect_unix_domain(config[:path], RedisClient)
    rescue RuntimeError => e
      if e.message == "no connection"
        raise Errno::ECONNREFUSED
      else
        raise e
      end
    end
  elsif config[:scheme] == "rediss" || config[:ssl]
    raise NotImplementedError, "SSL not supported by synchrony driver"
  else
    conn = EventMachine.connect(config[:host], config[:port], RedisClient) do |c|
      c.pending_connect_timeout = [config[:connect_timeout], 0.1].max
    end
  end

  fiber = Fiber.current
  conn.callback { fiber.resume }
  conn.errback { fiber.resume :refused }

  raise Errno::ECONNREFUSED if Fiber.yield == :refused

  instance = new(conn)
  instance.timeout = config[:read_timeout]
  instance
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/redis/connection/synchrony.rb', line 113

def connected?
  @connection&.connected?
end

#disconnectObject



121
122
123
124
# File 'lib/redis/connection/synchrony.rb', line 121

def disconnect
  @connection.close_connection
  @connection = nil
end

#readObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/redis/connection/synchrony.rb', line 130

def read
  type, payload = @connection.read

  case type
  when :reply
    payload
  when :error
    raise payload
  when :timeout
    raise TimeoutError
  else
    raise "Unknown type #{type.inspect}"
  end
end

#timeout=(timeout) ⇒ Object



117
118
119
# File 'lib/redis/connection/synchrony.rb', line 117

def timeout=(timeout)
  @connection.timeout = timeout
end

#write(command) ⇒ Object



126
127
128
# File 'lib/redis/connection/synchrony.rb', line 126

def write(command)
  @connection.send(build_command(command))
end