Client Kill [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [USER username] [ADDR ip:port] [SKIPME yes/no] 。
从2.4.0开始提供。
时间复杂度。O(N) 其中N是客户端连接数
CLIENT KILL 命令关闭一个给定的客户端连接。此命令支持两种格式,旧格式。
旧格式
CLIENT KILL addr:port
应该与CLIENT LIST命令返回的一行(字段)相匹配。
KMTest:0>CLIENT LIST
"id=14579113 addr=192.168.157.30:52842 fd=19 name=iZudifgmd4674nZ age=40218 idle=3 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14579114 addr=192.168.157.30:52843 fd=21 name=iZudifgmd4674nZ age=40218 idle=3 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14206901 addr=192.168.157.30:63631 fd=15 name=iZudifgmd4674nZ age=467389 idle=50 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=10768358 addr=192.168.157.31:62420 fd=36 name=iZudifgmd4674mZ age=4310032 idle=21 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=setex type=user
id=14328422 addr=192.168.157.26:5959 fd=51 name=iZunrt805gkkdcZ age=327984 idle=7 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14328423 addr=192.168.157.26:5960 fd=52 name=iZunrt805gkkdcZ age=327984 idle=7 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=11869856 addr=192.168.157.30:49482 fd=24 name=iZudifgmd4674nZ age=3124622 idle=4 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14613532 addr=192.168.157.30:59168 fd=30 name=iZudifgmd4674nZ age=705 idle=43 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14613533 addr=192.168.157.30:59167 fd=31 name=iZudifgmd4674nZ age=705 idle=43 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14325252 addr=192.168.157.26:5207 fd=47 name=iZunrt805gkkdcZ age=331620 idle=44 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14325253 addr=192.168.157.26:5208 fd=48 name=iZunrt805gkkdcZ age=331620 idle=44 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14607778 addr=192.168.157.31:60365 fd=23 name=iZudifgmd4674mZ age=7303 idle=28 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14607779 addr=192.168.157.31:60366 fd=26 name=iZudifgmd4674mZ age=7303 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=setex type=user
id=11869857 addr=192.168.157.30:49483 fd=25 name=iZudifgmd4674nZ age=3124622 idle=4 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=10768359 addr=192.168.157.31:62419 fd=37 name=iZudifgmd4674mZ age=4310032 idle=37 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=11870236 addr=192.168.157.30:49600 fd=28 name=iZudifgmd4674nZ age=3124192 idle=34 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14328406 addr=192.168.157.26:5940 fd=45 name=iZunrt805gkkdcZ age=328000 idle=24 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14328407 addr=192.168.157.26:5941 fd=46 name=iZunrt805gkkdcZ age=328000 idle=24 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14614153 addr=192.168.157.30:59305 fd=27 name= age=6 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r traffic-control= cmd=client type=user
id=12821539 addr=127.0.0.1:53069 fd=20 name= age=2053413 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=config type=localhost
id=14614085 addr=192.168.157.30:59282 fd=32 name=KM009326 age=84 idle=23 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14614086 addr=192.168.157.30:59283 fd=33 name=KM009326 age=84 idle=23 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=11870237 addr=192.168.157.30:49601 fd=29 name=iZudifgmd4674nZ age=3124192 idle=34 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14169490 addr=192.168.157.30:55349 fd=35 name=iZudifgmd4674nZ age=510280 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r traffic-control= cmd=get type=user
id=14169491 addr=192.168.157.30:55350 fd=44 name=iZudifgmd4674nZ age=510280 idle=26 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14169292 addr=192.168.157.30:55255 fd=17 name=iZudifgmd4674nZ age=510505 idle=10 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14169293 addr=192.168.157.30:55254 fd=18 name=iZudifgmd4674nZ age=510505 idle=10 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14206900 addr=192.168.157.30:63630 fd=14 name=iZudifgmd4674nZ age=467389 idle=35 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
id=14320826 addr=192.168.157.26:4214 fd=13 name=iZunrt805gkkdcZ age=336694 idle=18 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=unsubscribe type=user
id=14320827 addr=192.168.157.26:4213 fd=43 name=iZunrt805gkkdcZ age=336694 idle=18 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r traffic-control= cmd=info type=user
"
KMTest:0>CLIENT KILL 192.168.157.30:59282
"OK"
KMTest:0>CLIENT KILL 192.168.157.30:59283
"OK"
var redisConn = ConnectionMultiplexer.Connect(configurationOptions);
redisConn.InternalError += OnInternalError;
redisConn.ConnectionFailed += OnConnectionFailed;
int failCount;
/// <summary>
/// Handles the <see cref="E:InternalError" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="InternalErrorEventArgs"/> instance containing the event data.</param>
protected void OnInternalError(object sender, InternalErrorEventArgs e)
{
try
{
Interlocked.Increment(ref failCount);
SysLogHelper.Error("OnInternalError", $"内部错误,internalError:{e.Origin + "," + EndPointCollection.ToString(e.EndPoint) + "/" + e.ConnectionType},failCount:{failCount}");
}
catch (Exception ex)
{
SysLogHelper.Error(ex);
}
}
/// <summary>
/// Handles the <see cref="E:ConnectionFailed" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ConnectionFailedEventArgs"/> instance containing the event data.</param>
protected void OnConnectionFailed(object sender, ConnectionFailedEventArgs e)
{
try
{
Interlocked.Increment(ref failCount);
SysLogHelper.Error("OnConnectionFailed", $"连接失败,connectionFailed:{EndPointCollection.ToString(e.EndPoint) + "/" + e.ConnectionType + "e"},failCount:{failCount}" );
}
catch (Exception ex)
{
SysLogHelper.Error(ex);
}
}
新的格式
CLIENT KILL … … 。
在新的表格中,可以通过不同的属性来关闭客户端连接,而不是仅仅通过地址来关闭连接。以下是可用的过滤器。
CLIENT KILL ADDR ip:port. 这与旧的三参数行为完全相同。
CLIENT KILL LADDR ip:port. 关闭所有连接到指定本地(绑定)地址的客户端。
CLIENT KILL ID client-id. 允许通过其唯一的字段来关闭一个客户端连接。使用CLIENT LIST命令检索客户端。
CLIENT KILL TYPE type,其中type是 master replica pubsub normal
中的一个。这将关闭指定类中所有客户端的连接。需要注意的是,被封入monitor命令的客户端被认为属于类.normal 。
CLIENT KILL USER username。关闭所有使用指定的ACL用户名进行认证的连接,但如果用户名没有映射到现有的ACL用户,则返回一个错误。
CLIENT KILL SKIPME yes/no。默认情况下,这个选项被设置为 no,也就是说,调用该命令的客户端连接不会被关闭,但是将这个选项设置为yes ,会导致调用该命令的客户端连接也被关闭。
可以同时提供多个过滤器。该命令将通过逻辑AND处理多个过滤器。例如
CLIENT KILL addr 127.0.0.1:12345 type pubsub
是有效的,并且将只关闭一个具有指定地址的pubsub客户端连接。这种包含多个过滤器的格式目前很少有用。
当使用新的形式时,命令不再返回或错误,而是返回被杀的客户端数量,可能是零。
CLIENT KILL和Redis Sentinel的关系
Redis Sentinel的最新版本(Redis 2.8.12或更高版本)使用CLIENT KILL来在实例重新配置时杀死客户端,以迫使客户端再次与一个Sentinel执行握手并更新其配置。
说明
由于Redis的单线程特性,不可能在客户端连接执行命令的时候关闭它。从客户端的角度来看,连接永远不可能在执行命令的过程中被关闭。然而,只有在发送下一条命令时,客户端才会注意到连接已被关闭(并导致网络错误)。
返回值
当以三个参数格式调用时。
简单的字符串回复:如果连接存在并且已经关闭OK。
当以过滤器/值格式调用时。
Integer回复:关闭的客户端链接数量。
历史记录
= 2.8.12: 增加了新的过滤格式。 = 2.8.12: option.ID。 = 3.2: 增加了TYPE option.master的类型。 = 5: 将TYPE改为.仍支持向后兼容。 = 6.2: option.LADDR
原文链接
https://redis.io/commands/client-kill https://www.redis.net.cn/order/3656.html