Module: Redisbetween::ClientPatch

Defined in:
lib/redisbetween.rb

Constant Summary collapse

UNSUPPORTED_COMMANDS =
Set.new([
  :auth,
  :blpop,
  :brpop,
  :bzpopmax,
  :bzpopmin,
  :select,
  :wait,
  :xread,
  :xreadgroup,
])

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redisbetween_enabledObject (readonly)

Returns the value of attribute redisbetween_enabled.



13
14
15
# File 'lib/redisbetween.rb', line 13

def redisbetween_enabled
  @redisbetween_enabled
end

Instance Method Details

#_rb_wrapped_read(wrap) ⇒ Object

the proxy sends back nil values as placeholders for the signals, so discard them



89
90
91
92
93
94
# File 'lib/redisbetween.rb', line 89

def _rb_wrapped_read(wrap)
  read if wrap
  res = yield
  read if wrap
  res
end

#_rb_wrapped_write(wrap) ⇒ Object



82
83
84
85
86
# File 'lib/redisbetween.rb', line 82

def _rb_wrapped_write(wrap)
  write([:get, PIPELINE_START_SIGNAL]) if wrap
  yield
  write([:get, PIPELINE_END_SIGNAL]) if wrap
end

#initialize(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redisbetween.rb', line 15

def initialize(options = {})
  @redisbetween_enabled = !!options[:convert_to_redisbetween_socket]
  @handle_unsupported_redisbetween_command = options[:handle_unsupported_redisbetween_command]
  if @redisbetween_enabled
    @handle_unsupported_redisbetween_command ||= ->(cmd) { puts "redisbetween: unsupported #{cmd}" }
  end

  if redisbetween_enabled
    if options[:url]
      u = URI(options[:url])
      if u.scheme != 'unix'
        path = u.path.empty? ? nil : u.path.delete_prefix('/')
        u.path = Redisbetween.socket_path(options[:convert_to_redisbetween_socket], u.host, u.port, path)
        u.host = nil
        u.port = nil
        u.scheme = 'unix'
        options[:url] = u.to_s
      end
    elsif options[:host] && options[:port] && options[:scheme] != 'unix'
      path = Redisbetween.socket_path(options[:convert_to_redisbetween_socket], options[:host], options[:port])
      [:port, :host, :scheme].each { |k| options[k] = nil }
      options[:url] = "unix:#{path}"
    end
  end
  super(options)
end

#process(commands) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/redisbetween.rb', line 54

def process(commands)
  @handle_unsupported_redisbetween_command&.call("multi without a block") if commands == [[:multi]]

  logging(commands) do
    ensure_connected do
      wrap = commands.size > 1 && redisbetween_enabled

      _rb_wrapped_write(wrap) do
        commands.each do |command|
          if UNSUPPORTED_COMMANDS.member?(command.first)
            @handle_unsupported_redisbetween_command&.call(command.first.to_s)
          end
          if command_map[command.first]
            command = command.dup
            command[0] = command_map[command.first]
          end

          write(command)
        end
      end

      _rb_wrapped_read(wrap) do
        yield if block_given?
      end
    end
  end
end