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命令返回的一行(字段)相匹配。

  1. KMTest:0>CLIENT LIST
  2. "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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. "
  33. KMTest:0>CLIENT KILL 192.168.157.30:59282
  34. "OK"
  35. KMTest:0>CLIENT KILL 192.168.157.30:59283
  36. "OK"
  1. var redisConn = ConnectionMultiplexer.Connect(configurationOptions);
  2. redisConn.InternalError += OnInternalError;
  3. redisConn.ConnectionFailed += OnConnectionFailed;
  4. int failCount;
  5. /// <summary>
  6. /// Handles the <see cref="E:InternalError" /> event.
  7. /// </summary>
  8. /// <param name="sender">The sender.</param>
  9. /// <param name="e">The <see cref="InternalErrorEventArgs"/> instance containing the event data.</param>
  10. protected void OnInternalError(object sender, InternalErrorEventArgs e)
  11. {
  12. try
  13. {
  14. Interlocked.Increment(ref failCount);
  15. SysLogHelper.Error("OnInternalError", $"内部错误,internalError:{e.Origin + "," + EndPointCollection.ToString(e.EndPoint) + "/" + e.ConnectionType},failCount:{failCount}");
  16. }
  17. catch (Exception ex)
  18. {
  19. SysLogHelper.Error(ex);
  20. }
  21. }
  22. /// <summary>
  23. /// Handles the <see cref="E:ConnectionFailed" /> event.
  24. /// </summary>
  25. /// <param name="sender">The sender.</param>
  26. /// <param name="e">The <see cref="ConnectionFailedEventArgs"/> instance containing the event data.</param>
  27. protected void OnConnectionFailed(object sender, ConnectionFailedEventArgs e)
  28. {
  29. try
  30. {
  31. Interlocked.Increment(ref failCount);
  32. SysLogHelper.Error("OnConnectionFailed", $"连接失败,connectionFailed:{EndPointCollection.ToString(e.EndPoint) + "/" + e.ConnectionType + "e"},failCount:{failCount}" );
  33. }
  34. catch (Exception ex)
  35. {
  36. SysLogHelper.Error(ex);
  37. }
  38. }

image.png

新的格式

CLIENT KILL … … 。
在新的表格中,可以通过不同的属性来关闭客户端连接,而不是仅仅通过地址来关闭连接。以下是可用的过滤器。

CLIENT KILL ADDR ip:port. 这与旧的三参数行为完全相同。

CLIENT KILL LADDR ip:port. 关闭所有连接到指定本地(绑定)地址的客户端。

CLIENT KILL ID client-id. 允许通过其唯一的字段来关闭一个客户端连接。使用CLIENT LIST命令检索客户端。

CLIENT KILL TYPE type,其中typemaster 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 KILLRedis 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