Skip to content

Commit b9ac235

Browse files
committed
Merge pull request #514 from badboy/setname
Set client name on connect based on user-specified id
2 parents a6de56a + d2b7766 commit b9ac235

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

lib/redis.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ def self.current=(redis)
2626

2727
include MonitorMixin
2828

29+
# Create a new client instance
30+
#
31+
# @param [Hash] options
32+
# @option options [String] :url (value of the environment variable REDIS_URL) a Redis URL, for a TCP connection: `redis://:[password]@[hostname]:[port]/[db]` (password, port and database are optional), for a unix socket connection: `unix://[path to Redis socket]`. This overrides all other options.
33+
# @option options [String] :host ("127.0.0.1") server hostname
34+
# @option options [Fixnum] :port (6379) server port
35+
# @option options [String] :path path to server socket (overrides host and port)
36+
# @option options [Float] :timeout (5.0) timeout in seconds
37+
# @option options [Float] :connect_timeout (same as timeout) timeout for initial connect in seconds
38+
# @option options [String] :password Password to authenticate against server
39+
# @option options [Fixnum] :db (0) Database to select after initial connect
40+
# @option options [Symbol] :driver Driver to use, currently supported: `:ruby`, `:hiredis`, `:synchrony`
41+
# @option options [String] :id ID for the client connection, assigns name to current connection by sending `CLIENT SETNAME`
42+
# @option options [Hash, Fixnum] :tcp_keepalive Keepalive values, if Fixnum `intvl` and `probe` are calculated based on the value, if Hash `time`, `intvl` and `probes` can be specified as a Fixnum
43+
# @option options [Fixnum] :reconnect_attempts Number of attempts trying to connect
44+
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
45+
# @option options [Array] :sentinels List of sentinels to contact
46+
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
47+
#
48+
# @return [Redis] a new client instance
2949
def initialize(options = {})
3050
@options = options.dup
3151
@original_client = @client = Client.new(options)
@@ -398,7 +418,7 @@ def dump(key)
398418
# @param [String] key
399419
# @param [String] ttl
400420
# @param [String] serialized_value
401-
# @return `"OK"`
421+
# @return [String] `"OK"`
402422
def restore(key, ttl, serialized_value)
403423
synchronize do |client|
404424
client.call([:restore, key, ttl, serialized_value])
@@ -703,7 +723,7 @@ def set(key, value, options = {})
703723
# @param [String] key
704724
# @param [Fixnum] ttl
705725
# @param [String] value
706-
# @return `"OK"`
726+
# @return [String] `"OK"`
707727
def setex(key, ttl, value)
708728
synchronize do |client|
709729
client.call([:setex, key, ttl, value.to_s])
@@ -715,7 +735,7 @@ def setex(key, ttl, value)
715735
# @param [String] key
716736
# @param [Fixnum] ttl
717737
# @param [String] value
718-
# @return `"OK"`
738+
# @return [String] `"OK"`
719739
def psetex(key, ttl, value)
720740
synchronize do |client|
721741
client.call([:psetex, key, ttl, value.to_s])
@@ -740,7 +760,7 @@ def setnx(key, value)
740760
# # => "OK"
741761
#
742762
# @param [Array<String>] args array of keys and values
743-
# @return `"OK"`
763+
# @return [String] `"OK"`
744764
#
745765
# @see #mapped_mset
746766
def mset(*args)
@@ -756,7 +776,7 @@ def mset(*args)
756776
# # => "OK"
757777
#
758778
# @param [Hash] hash keys mapping to values
759-
# @return `"OK"`
779+
# @return [String] `"OK"`
760780
#
761781
# @see #mset
762782
def mapped_mset(hash)
@@ -981,7 +1001,7 @@ def llen(key)
9811001
# Prepend one or more values to a list, creating the list if it doesn't exist
9821002
#
9831003
# @param [String] key
984-
# @param [String, Array] string value, or array of string values to push
1004+
# @param [String, Array] value string value, or array of string values to push
9851005
# @return [Fixnum] the length of the list after the push operation
9861006
def lpush(key, value)
9871007
synchronize do |client|
@@ -1910,7 +1930,7 @@ def hsetnx(key, field, value)
19101930
#
19111931
# @param [String] key
19121932
# @param [Array<String>] attrs array of fields and values
1913-
# @return `"OK"`
1933+
# @return [String] `"OK"`
19141934
#
19151935
# @see #mapped_hmset
19161936
def hmset(key, *attrs)
@@ -1926,8 +1946,8 @@ def hmset(key, *attrs)
19261946
# # => "OK"
19271947
#
19281948
# @param [String] key
1929-
# @param [Hash] a non-empty hash with fields mapping to values
1930-
# @return `"OK"`
1949+
# @param [Hash] hash a non-empty hash with fields mapping to values
1950+
# @return [String] `"OK"`
19311951
#
19321952
# @see #hmset
19331953
def mapped_hmset(key, hash)
@@ -2250,7 +2270,7 @@ def exec
22502270
#
22512271
# Only call this method when `#multi` was called **without** a block.
22522272
#
2253-
# @return `"OK"`
2273+
# @return [String] `"OK"`
22542274
#
22552275
# @see #multi
22562276
# @see #exec
@@ -2398,7 +2418,7 @@ def _scan(command, cursor, args, options = {}, &block)
23982418
# redis.scan(4, :match => "key:1?")
23992419
# # => ["92", ["key:13", "key:18"]]
24002420
#
2401-
# @param [String, Integer] cursor: the cursor of the iteration
2421+
# @param [String, Integer] cursor the cursor of the iteration
24022422
# @param [Hash] options
24032423
# - `:match => String`: only return keys matching the pattern
24042424
# - `:count => Integer`: return count keys at most per iteration
@@ -2438,7 +2458,7 @@ def scan_each(options={}, &block)
24382458
# @example Retrieve the first batch of key/value pairs in a hash
24392459
# redis.hscan("hash", 0)
24402460
#
2441-
# @param [String, Integer] cursor: the cursor of the iteration
2461+
# @param [String, Integer] cursor the cursor of the iteration
24422462
# @param [Hash] options
24432463
# - `:match => String`: only return keys matching the pattern
24442464
# - `:count => Integer`: return count keys at most per iteration
@@ -2476,7 +2496,7 @@ def hscan_each(key, options={}, &block)
24762496
# @example Retrieve the first batch of key/value pairs in a hash
24772497
# redis.zscan("zset", 0)
24782498
#
2479-
# @param [String, Integer] cursor: the cursor of the iteration
2499+
# @param [String, Integer] cursor the cursor of the iteration
24802500
# @param [Hash] options
24812501
# - `:match => String`: only return keys matching the pattern
24822502
# - `:count => Integer`: return count keys at most per iteration
@@ -2515,7 +2535,7 @@ def zscan_each(key, options={}, &block)
25152535
# @example Retrieve the first batch of keys in a set
25162536
# redis.sscan("set", 0)
25172537
#
2518-
# @param [String, Integer] cursor: the cursor of the iteration
2538+
# @param [String, Integer] cursor the cursor of the iteration
25192539
# @param [Hash] options
25202540
# - `:match => String`: only return keys matching the pattern
25212541
# - `:count => Integer`: return count keys at most per iteration

lib/redis/client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def connect
9494
establish_connection
9595
call [:auth, password] if password
9696
call [:select, db] if db != 0
97+
call [:client, :setname, @options[:id]] if @options[:id]
9798
@connector.check(self)
9899
end
99100

test/connection_handling_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ def test_auth
1717
end
1818
end
1919

20+
def test_id
21+
commands = {
22+
:client => lambda { |cmd, name| $name = [cmd, name]; "+OK" },
23+
:ping => lambda { "+PONG" },
24+
}
25+
26+
redis_mock(commands, :id => "client-name") do |redis|
27+
assert_equal "PONG", redis.ping
28+
end
29+
30+
assert_equal ["setname","client-name"], $name
31+
end
32+
2033
def test_ping
2134
assert_equal "PONG", r.ping
2235
end

0 commit comments

Comments
 (0)