You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR actually implements the #with method for checking out a
connection directly to a particular Redis node. This allows you to
perform transactional commands directly against the underlying
`RedisClient` instance, so `#watch`, `#multi`, etc all work.
This does NOT yet implement any kind of validation for the commands
issued. If you issue commands involving keys not owned by the node, you
will receive `Redis::CommandError`'s. A future PR will add client-side
validation to hopefully make it possible to catch problems related to
transaction misuse earlier in development. There's a pending test for
this.
This PR also does not yet implement `RedisClient::Cluster#multi` in
terms of `#with`. That will also come in a future PR.
Pinned connections are aware of redirections and node failures like ordinary calls to `RedisClient::Cluster`, but because
230
+
you may have written non-idempotent code inside your block, the block is not automatically retried if e.g. the slot
231
+
it is operating on moves to a different node. If you want this, you can opt-in to retries by passing nonzero
232
+
`retry_count` to `#with`.
233
+
```ruby
234
+
cli.with(slot_key:'{key}', retry_count:1) do |conn|
235
+
conn.call('GET', '{key}1')
236
+
#=> "value1"
237
+
# Now, some changes in cluster topology mean that {key} is moved to a different node!
238
+
conn.call('GET', '{key}2')
239
+
#=> MOVED 9039 127.0.0.1:16381 (RedisClient::CommandError)
240
+
# Luckily, the block will get retried (once) and so both GETs will be re-executed on the newly-discovered
241
+
# correct node.
242
+
end
243
+
```
244
+
245
+
Because `RedisClient` from the redis-client gem implements `#with` as simply `yield self` and ignores all of its
246
+
arguments, it's possible to write code which is compatible with both redis-client and redis-cluster-client; the `#with`
247
+
call will pin the connection to a slot when using clustering, or be a no-op when not.
248
+
171
249
## ACL
172
250
The cluster client internally calls [COMMAND](https://redis.io/commands/command/) and [CLUSTER NODES](https://redis.io/commands/cluster-nodes/) commands to operate correctly.
0 commit comments