Skip to content

Commit

Permalink
adding username and laddr
Browse files Browse the repository at this point in the history
  • Loading branch information
atakavci committed Jul 3, 2024
1 parent 3e7f12b commit f1db9a0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 65 deletions.
75 changes: 67 additions & 8 deletions src/StackExchange.Redis/APITypes/ClientKillFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net;
using System;
using System.Collections.Generic;
using System.Net;

namespace StackExchange.Redis;

Expand Down Expand Up @@ -52,7 +54,7 @@ public ClientKillFilter() { }
/// Sets client id filter.
/// </summary>
/// <param name="id">Id of the client to kill.</param>
public ClientKillFilter WithId(long id)
public ClientKillFilter WithId(long? id)
{
Id = id;
return this;
Expand All @@ -62,7 +64,7 @@ public ClientKillFilter WithId(long id)
/// Sets client type filter.
/// </summary>
/// <param name="clientType">The type of the client.</param>
public ClientKillFilter WithClientType(ClientType clientType)
public ClientKillFilter WithClientType(ClientType? clientType)
{
ClientType = clientType;
return this;
Expand All @@ -72,7 +74,7 @@ public ClientKillFilter WithClientType(ClientType clientType)
/// Sets the username filter.
/// </summary>
/// <param name="username">Authenticated ACL username.</param>
public ClientKillFilter WithUsername(string username)
public ClientKillFilter WithUsername(string? username)
{
Username = username;
return this;
Expand All @@ -82,7 +84,7 @@ public ClientKillFilter WithUsername(string username)
/// Set the endpoint filter.
/// </summary>
/// <param name="endpoint">The endpoint to kill.</param>
public ClientKillFilter WithEndpoint(EndPoint endpoint)
public ClientKillFilter WithEndpoint(EndPoint? endpoint)
{
Endpoint = endpoint;
return this;
Expand All @@ -92,7 +94,7 @@ public ClientKillFilter WithEndpoint(EndPoint endpoint)
/// Set the server endpoint filter.
/// </summary>
/// <param name="serverEndpoint">The server endpoint to kill.</param>
public ClientKillFilter WithServerEndpoint(EndPoint serverEndpoint)
public ClientKillFilter WithServerEndpoint(EndPoint? serverEndpoint)
{
ServerEndpoint = serverEndpoint;
return this;
Expand All @@ -102,7 +104,7 @@ public ClientKillFilter WithServerEndpoint(EndPoint serverEndpoint)
/// Set the skipMe filter (whether to skip the current connection).
/// </summary>
/// <param name="skipMe">Whether to skip the current connection.</param>
public ClientKillFilter WithSkipMe(bool skipMe)
public ClientKillFilter WithSkipMe(bool? skipMe)
{
SkipMe = skipMe;
return this;
Expand All @@ -112,9 +114,66 @@ public ClientKillFilter WithSkipMe(bool skipMe)
/// Set the MaxAgeInSeconds filter.
/// </summary>
/// <param name="maxAgeInSeconds">Age of connection in seconds</param>
public ClientKillFilter WithMaxAgeInSeconds(long maxAgeInSeconds)
public ClientKillFilter WithMaxAgeInSeconds(long? maxAgeInSeconds)
{
MaxAgeInSeconds = maxAgeInSeconds;
return this;
}

internal List<RedisValue> ToList(bool withReplicaCommands)
{
var parts = new List<RedisValue>(15)
{
RedisLiterals.KILL
};
if (Id != null)
{
parts.Add(RedisLiterals.ID);
parts.Add(Id.Value);
}
if (ClientType != null)
{
parts.Add(RedisLiterals.TYPE);
switch (ClientType.Value)
{
case Redis.ClientType.Normal:
parts.Add(RedisLiterals.normal);
break;
case Redis.ClientType.Replica:
parts.Add(withReplicaCommands ? RedisLiterals.replica : RedisLiterals.slave);
break;
case Redis.ClientType.PubSub:
parts.Add(RedisLiterals.pubsub);
break;
default:
throw new ArgumentOutOfRangeException(nameof(ClientType));
}
}
if (Username != null)
{
parts.Add(RedisLiterals.USERNAME);
parts.Add(Username);
}
if (Endpoint != null)
{
parts.Add(RedisLiterals.ADDR);
parts.Add((RedisValue)Format.ToString(Endpoint));
}
if (ServerEndpoint != null)
{
parts.Add(RedisLiterals.LADDR);
parts.Add((RedisValue)Format.ToString(ServerEndpoint));
}
if (SkipMe != null)
{
parts.Add(RedisLiterals.SKIPME);
parts.Add(SkipMe.Value ? RedisLiterals.yes : RedisLiterals.no);
}
if (MaxAgeInSeconds != null)
{
parts.Add(RedisLiterals.MAXAGE);
parts.Add(MaxAgeInSeconds);
}
return parts;
}
}
14 changes: 7 additions & 7 deletions src/StackExchange.Redis/PublicAPI/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ StackExchange.Redis.ClientKillFilter.MaxAgeInSeconds.get -> long?
StackExchange.Redis.ClientKillFilter.ServerEndpoint.get -> System.Net.EndPoint?
StackExchange.Redis.ClientKillFilter.SkipMe.get -> bool?
StackExchange.Redis.ClientKillFilter.Username.get -> string?
StackExchange.Redis.ClientKillFilter.WithClientType(StackExchange.Redis.ClientType clientType) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithEndpoint(System.Net.EndPoint! endpoint) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithId(long id) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithMaxAgeInSeconds(long maxAgeInSeconds) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithServerEndpoint(System.Net.EndPoint! serverEndpoint) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithSkipMe(bool skipMe) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithUsername(string! username) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithClientType(StackExchange.Redis.ClientType? clientType) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithEndpoint(System.Net.EndPoint? endpoint) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithId(long? id) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithMaxAgeInSeconds(long? maxAgeInSeconds) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithServerEndpoint(System.Net.EndPoint? serverEndpoint) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithSkipMe(bool? skipMe) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientKillFilter.WithUsername(string? username) -> StackExchange.Redis.ClientKillFilter!
StackExchange.Redis.ClientType
StackExchange.Redis.ClientType.Normal = 0 -> StackExchange.Redis.ClientType
StackExchange.Redis.ClientType.PubSub = 2 -> StackExchange.Redis.ClientType
Expand Down
2 changes: 2 additions & 0 deletions src/StackExchange.Redis/RedisLiterals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static readonly RedisValue
IDLETIME = "IDLETIME",
KEEPTTL = "KEEPTTL",
KILL = "KILL",
LADDR = "LADDR",
LATEST = "LATEST",
LEFT = "LEFT",
LEN = "LEN",
Expand Down Expand Up @@ -138,6 +139,7 @@ public static readonly RedisValue
STATS = "STATS",
STORE = "STORE",
TYPE = "TYPE",
USERNAME = "USERNAME",
WEIGHTS = "WEIGHTS",
WITHMATCHLEN = "WITHMATCHLEN",
WITHSCORES = "WITHSCORES",
Expand Down
55 changes: 7 additions & 48 deletions src/StackExchange.Redis/RedisServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,73 +65,32 @@ public Task ClientKillAsync(EndPoint endpoint, CommandFlags flags = CommandFlags

public long ClientKill(long? id = null, ClientType? clientType = null, EndPoint? endpoint = null, bool skipMe = true, CommandFlags flags = CommandFlags.None)
{
var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, null, flags);
var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, flags);
return ExecuteSync(msg, ResultProcessor.Int64);
}

public Task<long> ClientKillAsync(long? id = null, ClientType? clientType = null, EndPoint? endpoint = null, bool skipMe = true, CommandFlags flags = CommandFlags.None)
{
var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, null, flags);
var msg = GetClientKillMessage(endpoint, id, clientType, skipMe, flags);
return ExecuteAsync(msg, ResultProcessor.Int64);
}

public long ClientKill(ClientKillFilter filter, CommandFlags flags = CommandFlags.None)
{
var msg = GetClientKillMessage(filter.Endpoint, filter.Id, filter.ClientType, filter.SkipMe, filter.MaxAgeInSeconds, flags);
var msg = Message.Create(-1, flags, RedisCommand.CLIENT, filter.ToList(Features.ReplicaCommands));
return ExecuteSync(msg, ResultProcessor.Int64);
}

public Task<long> ClientKillAsync(ClientKillFilter filter, CommandFlags flags = CommandFlags.None)
{
var msg = GetClientKillMessage(filter.Endpoint, filter.Id, filter.ClientType, filter.SkipMe, filter.MaxAgeInSeconds, flags);
var msg = Message.Create(-1, flags, RedisCommand.CLIENT, filter.ToList(Features.ReplicaCommands));
return ExecuteAsync(msg, ResultProcessor.Int64);
}

private Message GetClientKillMessage(EndPoint? endpoint, long? id, ClientType? clientType, bool? skipMe, long? maxAgeInSeconds, CommandFlags flags)
private Message GetClientKillMessage(EndPoint? endpoint, long? id, ClientType? clientType, bool? skipMe, CommandFlags flags)
{
var parts = new List<RedisValue>(9)
{
RedisLiterals.KILL
};
if (id != null)
{
parts.Add(RedisLiterals.ID);
parts.Add(id.Value);
}
if (clientType != null)
{
parts.Add(RedisLiterals.TYPE);
switch (clientType.Value)
{
case ClientType.Normal:
parts.Add(RedisLiterals.normal);
break;
case ClientType.Replica:
parts.Add(Features.ReplicaCommands ? RedisLiterals.replica : RedisLiterals.slave);
break;
case ClientType.PubSub:
parts.Add(RedisLiterals.pubsub);
break;
default:
throw new ArgumentOutOfRangeException(nameof(clientType));
}
}
if (endpoint != null)
{
parts.Add(RedisLiterals.ADDR);
parts.Add((RedisValue)Format.ToString(endpoint));
}
if (skipMe != null)
{
parts.Add(RedisLiterals.SKIPME);
parts.Add(skipMe.Value ? RedisLiterals.yes : RedisLiterals.no);
}
if (maxAgeInSeconds != null)
{
parts.Add(RedisLiterals.MAXAGE);
parts.Add(maxAgeInSeconds);
}
return Message.Create(-1, flags, RedisCommand.CLIENT, parts);
var args = new ClientKillFilter().WithId(id).WithClientType(clientType).WithEndpoint(endpoint).WithSkipMe(skipMe).ToList(Features.ReplicaCommands);
return Message.Create(-1, flags, RedisCommand.CLIENT, args);
}

public ClientInfo[] ClientList(CommandFlags flags = CommandFlags.None)
Expand Down
23 changes: 21 additions & 2 deletions tests/StackExchange.Redis.Tests/ClientKillTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -44,4 +44,23 @@ public void ClientKillWithMaxAge()
long result = server.ClientKill(filter, CommandFlags.DemandMaster);
Assert.Equal(1, result);
}

[Fact]
public void TestClientKillMessageWithAllArguments()
{
long id = 101;
ClientType type = ClientType.Normal;
string userName = "user1";
EndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
EndPoint serverEndpoint = new IPEndPoint(IPAddress.Parse("198.0.0.1"), 6379); ;
bool skipMe = true;
long maxAge = 102;

var filter = new ClientKillFilter().WithId(id).WithClientType(type).WithUsername(userName).WithEndpoint(endpoint).WithServerEndpoint(serverEndpoint).WithSkipMe(skipMe).WithMaxAgeInSeconds(maxAge);
List<RedisValue> expected = new List<RedisValue>()
{
"KILL", "ID", "101", "TYPE", "normal", "USERNAME", "user1", "ADDR", "127.0.0.1:1234", "LADDR", "198.0.0.1:6379", "SKIPME", "yes", "MAXAGE", "102"
};
Assert.Equal(expected, filter.ToList(true));
}
}

0 comments on commit f1db9a0

Please sign in to comment.