客户端命令
"CACHING (YES|NO)", " Enable/disable tracking of the keys for next command in OPTIN/OPTOUT modes.", "GETREDIR", " Return the client ID we are redirecting to when tracking is enabled.", "GETNAME", " Return the name of the current connection.", "ID", " Return the ID of the current connection.", "INFO", " Return information about the current client connection.", "KILL <ip:port>", " Kill connection made from <ip:port>.", "KILL <option> <value> [<option> <value> [...]]", " Kill connections. Options are:", " * ADDR (<ip:port>|<unixsocket>:0)", " Kill connections made from the specified address", " * LADDR (<ip:port>|<unixsocket>:0)", " Kill connections made to specified local address", " * TYPE (normal|master|replica|pubsub)", " Kill connections by type.", " * USER <username>", " Kill connections authenticated by <username>.", " * SKIPME (YES|NO)", " Skip killing current connection (default: yes).", "LIST [options ...]", " Return information about client connections. Options:", " * TYPE (NORMAL|MASTER|REPLICA|PUBSUB)", " Return clients of specified type.", "UNPAUSE", " Stop the current client pause, resuming traffic.", "PAUSE <timeout> [WRITE|ALL]", " Suspend all, or just write, clients for <timout> milliseconds.", "REPLY (ON|OFF|SKIP)", " Control the replies sent to the current connection.", "SETNAME <name>", " Assign the name <name> to the current connection.", "UNBLOCK <clientid> [TIMEOUT|ERROR]", " Unblock the specified blocked client.", "TRACKING (ON|OFF) [REDIRECT <id>] [BCAST] [PREFIX <prefix> [...]]", " [OPTIN] [OPTOUT]", " Control server assisted client side caching.", "TRACKINGINFO", " Report tracking status for the current connection."
1、client list 查看客户端的信息
127.0.0.1:6379> client list id=4 addr=127.0.0.1:61349 fd=8 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=62490 events=r cmd=client user=default
flags 表示客户端类型 N-表示普通客户端, M 表示 master
- obl 表示固定缓冲区的长度
- oll 表示动态缓冲区长度
- omeom 代表使用的字节数
- events 表示事件类型(r/w)
- cmd 记录最后一次执行的命令
具体计算过程,见函数 catClientInfoString
2、info clients 查看所有的客户端
127.0.0.1:6379> info clients # Clients connected_clients:1 client_recent_max_input_buffer:16 client_recent_max_output_buffer:0 blocked_clients:0 tracking_clients:0 clients_in_timeout_table:0
connected_clients
: 代表当前 Redis 节点的客户端连接数,需要重点监控,一旦超过 maxclients , 新的客户单连接会被拒绝。
client_longest_ouput_list
: 当前所有输出缓冲区中队列对象个数的最大值。
client_biggest_input_buf
: 当前所有输入缓冲区中占用的最大容量。
blocked_clients
: 正在执行阻塞命令(例如:blpop
、brpop
、brpoplpush
) 的客户端个数
客户端关闭
1、调用 client kill 命令;
2、不符合规范的命令;
3、客户端超时间;
4、输入缓冲区超过阈值 1G、受参数 RedisServer
的 clent_max_querybuf_len
控制。
由于redis是单线程的,所以他不可能一直循环来检测客户端。其中客户端超时检测是在serverCron
的clientsCron
中进行,serverCron
是一个周期函数,每100ms执行一次。server.hz
表示serverCron
函数的调用频率,默认为10。 clientsCron函数中为了每秒钟都能循环一次所有客户端,所以每次循环次数为iterations = numclients/server.hz
。如果客户端多的话,可能会导致redis主线程的阻塞。因此,5.0引入了动态hz,见配置文件dynamic-hz,默认打开。
参考资料
- 《Redis 设计与实现》黄健宏