Class: SolidRedis::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_redis/client.rb

Direct Known Subclasses

Subscription

Defined Under Namespace

Classes: Pipeline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/solid_redis/client.rb', line 10

def initialize(config, name: nil)
  @config = config
  @name = name&.to_s
  @socket = nil
  @reader = nil
  @target = nil
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/solid_redis/client.rb', line 8

def config
  @config
end

Instance Method Details

#blocking_call(timeout, *command) ⇒ Object

Runs a blocking command such as BLPOP, BRPOP, BZPOPMIN or XREAD BLOCK.

timeout is the number of seconds Redis was asked to block for; the socket read timeout becomes timeout plus the configured read timeout. Pass nil or 0 when Redis blocks indefinitely: the read then waits forever. The command is never retried after a connection error because the element may already have been consumed.



36
37
38
# File 'lib/solid_redis/client.rb', line 36

def blocking_call(timeout, *command)
  blocking_call_v(timeout, command)
end

#blocking_call_v(timeout, command) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/solid_redis/client.rb', line 40

def blocking_call_v(timeout, command)
  with_reconnect { connect unless connected? }

  read_timeout = timeout && timeout.positive? ? timeout + @target.read_timeout : nil
  begin
    write(RESP.encode(command))
    @reader.with_timeout(read_timeout) { @reader.read }
  rescue ConnectionError, IO::WaitReadable, IO::WaitWritable, SystemCallError => error
    close
    config.reset if config.sentinel?
    config.notify(:connection_error, error.class.name, error.message)
    raise error if error.is_a?(Error)

    raise ConnectionError, error.message, cause: error
  end
end

#call(*command) ⇒ Object



18
19
20
# File 'lib/solid_redis/client.rb', line 18

def call(*command)
  call_v(command)
end

#call_v(command) ⇒ Object



22
23
24
25
26
27
# File 'lib/solid_redis/client.rb', line 22

def call_v(command)
  with_reconnect do
    write(RESP.encode(command))
    @reader.read
  end
end

#close ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/solid_redis/client.rb', line 77

def close
  socket = @socket
  target = @target
  @socket = @reader = @target = nil
  socket&.close
  config.notify(:disconnected, target.server_url) if socket && target
  self
rescue IOError
  self
end

#connected? ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/solid_redis/client.rb', line 73

def connected?
  @socket && !@socket.closed?
end

#pipelined(exception: true) {|pipeline| ... } ⇒ Object

Yields:

  • (pipeline)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/solid_redis/client.rb', line 57

def pipelined(exception: true)
  pipeline = Pipeline.new
  yield pipeline
  return [] if pipeline.commands.empty?

  with_reconnect do
    write(pipeline.commands.map { |command| RESP.encode(command) }.join)
    results = pipeline.commands.map { @reader.read(exception: false) }
    if exception && (error = results.find { |result| result.is_a?(CommandError) })
      raise error
    end

    results
  end
end

#server_url ⇒ Object



88
89
90
# File 'lib/solid_redis/client.rb', line 88

def server_url
  config.server_url
end