redis / redis-rb

A Ruby client library for Redis
MIT License
3.97k stars 1.03k forks source link

Implement #watch and #multi specially for cluster-client #1256

Open KJTsanaktsidis opened 8 months ago

KJTsanaktsidis commented 8 months ago

This PR makes watch & multi work more or less the same way for clustering as they do for normal redis.

Since it's supposed to be valid to perform your multi call on the original redis object, like this:

redis.watch('key') do
  redis.multi do |tx|
    # tx is performed on the same connection as the watch
  end
end

we need to keeps some state in an ivar @active_watcher so we know to call MULTI on the same actual connection as WATCH (and appropriately fail if the keys got redirected or the node went down). This is technically threadsafe, because the watch/multi implementation is wrapped in the synchronize monitor; however, for good performance in multithreaded environments, you will most likely want to use a connection pool of Redis::Cluster instances.

(note - this will need https://github.com/redis-rb/redis-cluster-client/pull/339 to be merged in order to work. But "the tests pass on my machine, I promise" 😂 )