redis-rb/redis-client

Is there way to add some commands to connection prelude?

Closed this issue · 2 comments

Hello,

I'm implementing a cluster client with RedisClient. The cluster client needs to call the READONLY command at connection prelude if we use the scale reading feature of cluster mode. Is there way to add some commands to the prelude without method overriding?

def build_connection_prelude
prelude = []
prelude << if @password
["HELLO", "3", "AUTH", @username, @password]
else
["HELLO", "3"]
end
if @db && @db != 0
prelude << ["SELECT", @db.to_s]
end
prelude.freeze
end

def connect
connection = config.driver.new(
config,
connect_timeout: connect_timeout,
read_timeout: read_timeout,
write_timeout: write_timeout,
)
prelude = config.connection_prelude.dup
if id
prelude << ["CLIENT", "SETNAME", id.to_s]
end
# The connection prelude is deliberately not sent to Middlewares
if config.sentinel?
prelude << ["ROLE"]
role, = connection.call_pipelined(prelude, nil).last
config.check_role!(role)
else
connection.call_pipelined(prelude, nil)
end
connection
rescue CommandError => error
if error.message.include?("ERR unknown command `HELLO`")
raise UnsupportedServer,
"Your Redis server version is too old. redis-client requires Redis 6+. (#{config.server_url})"
else
raise
end
end

Not at the moment, but it could make sense to have a prelude: parameter for Config. Feel free to PR that.

But also subclassing Config may make sense, that's that I do for sentinel. But up to you.

Thank you for your quick reply. I'll try to override with subclassing because this issue is cluster mode matter.