Skip to content

Commit

Permalink
- ipv6 参数解析纠正 #228
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed Dec 25, 2019
1 parent 3ac6b93 commit 4ca1510
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultData
| name | <空> | 连接名称,可以使用 Client List 命令查看 |
| prefix | <空> | key前辍,所有方法都会附带此前辍,csredis.Set(prefix + "key", 111); |

> IPv6 格式:[fe80::b164:55b3:4b4f:7ce6%15]:6379
# 哨兵模式

```csharp
Expand Down
2 changes: 1 addition & 1 deletion src/CSRedisCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>CSRedisCore</AssemblyName>
<PackageId>CSRedisCore</PackageId>
<RootNamespace>CSRedisCore</RootNamespace>
<Version>3.2.1</Version>
<Version>3.3.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/csredis</PackageProjectUrl>
<Description>CSRedis 是 redis.io 官方推荐库,支持 redis-trib集群、哨兵、私有分区与连接池管理技术,简易 RedisHelper 静态类。</Description>
Expand Down
29 changes: 20 additions & 9 deletions src/RedisClientPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,45 @@ public class RedisClientPoolPolicy : IPolicy<RedisClient>

internal void SetHost(string host)
{
if (string.IsNullOrEmpty(host?.Trim())) {
_ip = "127.0.0.1";
_port = 6379;
return;
}
host = host.Trim();
var ipv6 = Regex.Match(host, @"^\[([^\]]+)\]\s*(:\s*(\d+))?$");
if (ipv6.Success) //ipv6+port 格式: [fe80::b164:55b3:4b4f:7ce6%15]:6379
{
_ip = ipv6.Groups[1].Value.Trim();
_port = int.TryParse(ipv6.Groups[3].Value, out var tryint) && tryint > 0 ? tryint : 6379;
return;
}
var spt = (host ?? "").Split(':');
if (spt.Length == 1)
if (spt.Length == 1) //ipv4 or domain
{
_ip = string.IsNullOrEmpty(spt[0].Trim()) == false ? spt[0].Trim() : "127.0.0.1";
_port = 6379;
return;
}
if (spt.Length == 2)
if (spt.Length == 2) //ipv4:port or domain:port
{
if (int.TryParse(spt.Last().Trim(), out var testPort2))
{
_ip = string.IsNullOrEmpty(spt[0].Trim()) == false ? spt[0].Trim() : "127.0.0.1";
_port = testPort2;
return;
}
else
{
_ip = host;
_port = 6379;
}
_ip = host;
_port = 6379;
return;
}
if (IPAddress.TryParse(host, out var tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6)
if (IPAddress.TryParse(host, out var tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6) //test ipv6
{
_ip = host;
_port = 6379;
return;
}
if (int.TryParse(spt.Last().Trim(), out var testPort))
if (int.TryParse(spt.Last().Trim(), out var testPort)) //test ipv6:port
{
var testHost = string.Join(":", spt.Where((a, b) => b < spt.Length - 1));
if (IPAddress.TryParse(testHost, out tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down
8 changes: 4 additions & 4 deletions test/CSRedisCore.Tests/CSRedisClientHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void HIncrBy() {
[Fact]
public void HIncrByFloat() {
Assert.True(rds.HMSet("TestHIncrByFloat", "null1", base.Null, "string1", base.String, "bytes1", base.Bytes, "class1", base.Class, "class1array", new[] { base.Class, base.Class }));
Assert.Equal(0.5, rds.HIncrByFloat("TestHIncrByFloat", "null1", 0.5));
Assert.Throws<CSRedis.RedisException>(() => rds.HIncrByFloat("TestHIncrByFloat", "string1", 1.5));
Assert.Equal(0.5m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 0.5m));
Assert.Throws<CSRedis.RedisException>(() => rds.HIncrByFloat("TestHIncrByFloat", "string1", 1.5m));
Assert.Throws<CSRedis.RedisException>(() => rds.HIncrByFloat("TestHIncrByFloat", "bytes1", 5));

Assert.Equal(3.8, rds.HIncrByFloat("TestHIncrByFloat", "null1", 3.3));
Assert.Equal(14.3, rds.HIncrByFloat("TestHIncrByFloat", "null1", 10.5));
Assert.Equal(3.8m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 3.3m));
Assert.Equal(14.3m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 10.5m));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/CSRedisCore.Tests/CSRedisClientStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void IncrBy() {
key = "TestIncrBy";
Assert.Equal(1, rds.IncrBy(key, 1));
Assert.Equal(11, rds.IncrBy(key, 10));
Assert.Equal(21.5, rds.IncrByFloat(key, 10.5));
Assert.Equal(21.5m, rds.IncrByFloat(key, 10.5m));
}

[Fact]
Expand Down

0 comments on commit 4ca1510

Please sign in to comment.