Skip to content

Commit ec873e7

Browse files
authored
Merge pull request #206 from cnblogs/support-ipv6
feat: support IPv6
2 parents ef39647 + 891bd6b commit ec873e7

File tree

8 files changed

+26
-15
lines changed

8 files changed

+26
-15
lines changed

src/Enyim.Caching/Configuration/IMemcachedClientConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public interface IMemcachedClientConfiguration
4444

4545
bool UseSslStream { get; }
4646

47+
bool UseIPv6 { get; }
48+
4749
bool SuppressException { get; }
4850
}
4951
}

src/Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public MemcachedClientConfiguration(
121121
}
122122

123123
UseSslStream = options.UseSslStream;
124+
UseIPv6 = options.UseIPv6;
124125
SuppressException = options.SuppressException;
125126

126127
if (!string.IsNullOrEmpty(options.KeyTransformer))
@@ -201,7 +202,7 @@ private void ConfigureServers(MemcachedClientOptions options)
201202
if (!IPAddress.TryParse(server.Address, out var address))
202203
{
203204
address = Dns.GetHostAddresses(server.Address)
204-
.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork);
205+
.FirstOrDefault(i => i.AddressFamily == (options.UseIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork));
205206

206207
if (address == null)
207208
{
@@ -348,7 +349,7 @@ IServerPool IMemcachedClientConfiguration.CreatePool()
348349
}
349350

350351
public bool UseSslStream { get; private set; }
351-
352+
public bool UseIPv6 { get; private set; }
352353
public bool SuppressException { get; private set; }
353354

354355
#endregion

src/Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>
2323

2424
public bool UseSslStream { get; set; }
2525

26+
public bool UseIPv6 { get; set; }
27+
2628
public bool SuppressException { get; set; } = true;
2729

2830
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }

src/Enyim.Caching/Memcached/DefaultServerPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public DefaultServerPool(
5050

5151
protected virtual IMemcachedNode CreateNode(EndPoint endpoint)
5252
{
53-
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream);
53+
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
5454
}
5555

5656
private void rezCallback(object state)

src/Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ public class MemcachedNode : IMemcachedNode
3636
private SemaphoreSlim poolInitSemaphore = new SemaphoreSlim(1, 1);
3737
private readonly TimeSpan _initPoolTimeout;
3838
private bool _useSslStream;
39+
private bool _useIPv6;
3940

4041
public MemcachedNode(
4142
EndPoint endpoint,
4243
ISocketPoolConfiguration socketPoolConfig,
4344
ILogger logger,
44-
bool useSslStream)
45+
bool useSslStream,
46+
bool useIPv6)
4547
{
4648
_endPoint = endpoint;
4749
_useSslStream = useSslStream;
@@ -62,6 +64,7 @@ public MemcachedNode(
6264

6365
_logger = logger;
6466
_internalPoolImpl = new InternalPoolImpl(this, socketPoolConfig, _logger);
67+
_useIPv6 = useIPv6;
6568
}
6669

6770
public event Action<IMemcachedNode> Failed;
@@ -856,7 +859,7 @@ protected internal virtual PooledSocket CreateSocket()
856859
{
857860
try
858861
{
859-
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream);
862+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
860863
ps.Connect();
861864
return ps;
862865
}
@@ -872,7 +875,7 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
872875
{
873876
try
874877
{
875-
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream);
878+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
876879
await ps.ConnectAsync();
877880
return ps;
878881
}

src/Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@ public partial class PooledSocket : IDisposable
2020

2121
private bool _isAlive;
2222
private bool _useSslStream;
23+
private bool _useIPv6;
2324
private Socket _socket;
2425
private readonly EndPoint _endpoint;
2526
private readonly int _connectionTimeout;
2627

2728
private NetworkStream _inputStream;
2829
private SslStream _sslStream;
2930

30-
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream)
31+
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream, bool useIPv6)
3132
{
3233
_logger = logger;
3334
_isAlive = true;
3435
_useSslStream = useSslStream;
36+
_useIPv6 = useIPv6;
3537

36-
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
38+
var socket = new Socket(useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3739
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
3840
socket.NoDelay = true;
3941

@@ -540,10 +542,10 @@ private IPEndPoint GetIPEndPoint(EndPoint endpoint)
540542
{
541543
var dnsEndPoint = (DnsEndPoint)endpoint;
542544
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
543-
ip.AddressFamily == AddressFamily.InterNetwork);
544-
if (address == null)
545-
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", endpoint));
546-
return new IPEndPoint(address, dnsEndPoint.Port);
545+
ip.AddressFamily == (_useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork));
546+
return address == null
547+
? throw new ArgumentException(string.Format("Could not resolve host '{0}'.", endpoint))
548+
: new IPEndPoint(address, dnsEndPoint.Port);
547549
}
548550
else if (endpoint is IPEndPoint)
549551
{

src/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public BinaryNode(
2424
ISocketPoolConfiguration config,
2525
ISaslAuthenticationProvider authenticationProvider,
2626
ILogger logger,
27-
bool useSslStream)
28-
: base(endpoint, config, logger, useSslStream)
27+
bool useSslStream,
28+
bool useIPv6)
29+
: base(endpoint, config, logger, useSslStream, useIPv6)
2930
{
3031
_authenticationProvider = authenticationProvider;
3132
_logger = logger;

src/Enyim.Caching/Memcached/Protocol/Binary/BinaryPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public BinaryPool(IMemcachedClientConfiguration configuration, ILogger logger)
2929

3030
protected override IMemcachedNode CreateNode(EndPoint endpoint)
3131
{
32-
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream);
32+
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
3333
}
3434

3535
private static ISaslAuthenticationProvider GetProvider(IMemcachedClientConfiguration configuration)

0 commit comments

Comments
 (0)