Skip to content

Commit 1920f5a

Browse files
authored
add support for parsing and formatting unix domain sockets (#1624)
1 parent 6409b9d commit 1920f5a

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

src/StackExchange.Redis/EndPointCollection.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,14 @@ internal void SetDefaultPorts(int defaultPort)
115115
{
116116
for (int i = 0; i < Count; i++)
117117
{
118-
var endpoint = this[i];
119-
var dns = endpoint as DnsEndPoint;
120-
if (dns?.Port == 0)
118+
switch (this[i])
121119
{
122-
this[i] = new DnsEndPoint(dns.Host, defaultPort, dns.AddressFamily);
123-
continue;
124-
}
125-
var ip = endpoint as IPEndPoint;
126-
if (ip?.Port == 0)
127-
{
128-
this[i] = new IPEndPoint(ip.Address, defaultPort);
129-
continue;
120+
case DnsEndPoint dns when dns.Port == 0:
121+
this[i] = new DnsEndPoint(dns.Host, defaultPort, dns.AddressFamily);
122+
break;
123+
case IPEndPoint ip when ip.Port == 0:
124+
this[i] = new IPEndPoint(ip.Address, defaultPort);
125+
break;
130126
}
131127
}
132128
}

src/StackExchange.Redis/Format.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Buffers.Text;
44
using System.Globalization;
55
using System.Net;
6+
using System.Net.Sockets;
67
using System.Runtime.InteropServices;
78
using System.Text;
89

@@ -78,17 +79,21 @@ internal static string ToString(object value)
7879

7980
internal static string ToString(EndPoint endpoint)
8081
{
81-
if (endpoint is DnsEndPoint dns)
82-
{
83-
if (dns.Port == 0) return dns.Host;
84-
return dns.Host + ":" + Format.ToString(dns.Port);
85-
}
86-
if (endpoint is IPEndPoint ip)
82+
switch (endpoint)
8783
{
88-
if (ip.Port == 0) return ip.Address.ToString();
89-
return ip.Address + ":" + Format.ToString(ip.Port);
84+
case DnsEndPoint dns:
85+
if (dns.Port == 0) return dns.Host;
86+
return dns.Host + ":" + Format.ToString(dns.Port);
87+
case IPEndPoint ip:
88+
if (ip.Port == 0) return ip.Address.ToString();
89+
return ip.Address + ":" + Format.ToString(ip.Port);
90+
#if UNIX_SOCKET
91+
case UnixDomainSocketEndPoint uds:
92+
return "!" + uds.ToString();
93+
#endif
94+
default:
95+
return endpoint?.ToString() ?? "";
9096
}
91-
return endpoint?.ToString() ?? "";
9297
}
9398

9499
internal static string ToStringHostOnly(EndPoint endpoint)
@@ -233,6 +238,16 @@ internal static EndPoint TryParseEndPoint(string addressWithPort)
233238
string portPart = null;
234239
if (string.IsNullOrEmpty(addressWithPort)) return null;
235240

241+
if (addressWithPort[0]=='!')
242+
{
243+
if (addressWithPort.Length == 1) return null;
244+
245+
#if UNIX_SOCKET
246+
return new UnixDomainSocketEndPoint(addressWithPort.Substring(1));
247+
#else
248+
throw new PlatformNotSupportedException("Unix domain sockets require .NET Core 3 or above");
249+
#endif
250+
}
236251
var lastColonIndex = addressWithPort.LastIndexOf(':');
237252
if (lastColonIndex > 0)
238253
{

src/StackExchange.Redis/StackExchange.Redis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<SignAssembly>true</SignAssembly>
1111
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1212
<DefineConstants Condition="'$(TargetFramework)' != 'net461'">$(DefineConstants);VECTOR_SAFE</DefineConstants>
13+
<DefineConstants Condition="'$(TargetFramework)' != 'net461' and '$(TargetFramework)' != 'net472' and '$(TargetFramework)' != 'netstandard2.0'">$(DefineConstants);UNIX_SOCKET</DefineConstants>
1314
</PropertyGroup>
1415

1516
<ItemGroup>

tests/StackExchange.Redis.Tests/Config.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ public void ConfigurationOptionsIPv6Parsing(string configString, AddressFamily f
142142
Assert.Equal(port, ep.Port);
143143
}
144144

145+
[Fact]
146+
public void CanParseAndFormatUnixDomainSocket()
147+
{
148+
const string ConfigString = "!/some/path,allowAdmin=True";
149+
#if NET472
150+
var ex = Assert.Throws<PlatformNotSupportedException>(() => ConfigurationOptions.Parse(ConfigString));
151+
Assert.Equal("Unix domain sockets require .NET Core 3 or above", ex.Message);
152+
#else
153+
var config = ConfigurationOptions.Parse(ConfigString);
154+
Assert.True(config.AllowAdmin);
155+
var ep = Assert.IsType<UnixDomainSocketEndPoint>(Assert.Single(config.EndPoints));
156+
Assert.Equal("/some/path", ep.ToString());
157+
Assert.Equal(ConfigString, config.ToString());
158+
#endif
159+
}
160+
145161
[Fact]
146162
public void TalkToNonsenseServer()
147163
{

tests/StackExchange.Redis.Tests/StackExchange.Redis.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030

3131
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
3232
<Reference Include="Microsoft.CSharp" />
33-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
33+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
3434
</ItemGroup>
3535
</Project>

0 commit comments

Comments
 (0)