Skip to content

Commit

Permalink
feat: support IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Dec 2, 2023
1 parent ef39647 commit 891bd6b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public interface IMemcachedClientConfiguration

bool UseSslStream { get; }

bool UseIPv6 { get; }

bool SuppressException { get; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public MemcachedClientConfiguration(
}

UseSslStream = options.UseSslStream;
UseIPv6 = options.UseIPv6;
SuppressException = options.SuppressException;

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

if (address == null)
{
Expand Down Expand Up @@ -348,7 +349,7 @@ IServerPool IMemcachedClientConfiguration.CreatePool()
}

public bool UseSslStream { get; private set; }

public bool UseIPv6 { get; private set; }
public bool SuppressException { get; private set; }

#endregion
Expand Down
2 changes: 2 additions & 0 deletions src/Enyim.Caching/Configuration/MemcachedClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>

public bool UseSslStream { get; set; }

public bool UseIPv6 { get; set; }

public bool SuppressException { get; set; } = true;

public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Enyim.Caching/Memcached/DefaultServerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public DefaultServerPool(

protected virtual IMemcachedNode CreateNode(EndPoint endpoint)
{
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream);
return new MemcachedNode(endpoint, _configuration.SocketPool, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
}

private void rezCallback(object state)
Expand Down
9 changes: 6 additions & 3 deletions src/Enyim.Caching/Memcached/MemcachedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public class MemcachedNode : IMemcachedNode
private SemaphoreSlim poolInitSemaphore = new SemaphoreSlim(1, 1);
private readonly TimeSpan _initPoolTimeout;
private bool _useSslStream;
private bool _useIPv6;

public MemcachedNode(
EndPoint endpoint,
ISocketPoolConfiguration socketPoolConfig,
ILogger logger,
bool useSslStream)
bool useSslStream,
bool useIPv6)
{
_endPoint = endpoint;
_useSslStream = useSslStream;
Expand All @@ -62,6 +64,7 @@ public MemcachedNode(

_logger = logger;
_internalPoolImpl = new InternalPoolImpl(this, socketPoolConfig, _logger);
_useIPv6 = useIPv6;
}

public event Action<IMemcachedNode> Failed;
Expand Down Expand Up @@ -856,7 +859,7 @@ protected internal virtual PooledSocket CreateSocket()
{
try
{
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream);
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
ps.Connect();
return ps;
}
Expand All @@ -872,7 +875,7 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
{
try
{
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream);
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger, _useSslStream, _useIPv6);
await ps.ConnectAsync();
return ps;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Enyim.Caching/Memcached/PooledSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ public partial class PooledSocket : IDisposable

private bool _isAlive;
private bool _useSslStream;
private bool _useIPv6;
private Socket _socket;
private readonly EndPoint _endpoint;
private readonly int _connectionTimeout;

private NetworkStream _inputStream;
private SslStream _sslStream;

public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream)
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger, bool useSslStream, bool useIPv6)
{
_logger = logger;
_isAlive = true;
_useSslStream = useSslStream;
_useIPv6 = useIPv6;

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var socket = new Socket(useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
socket.NoDelay = true;

Expand Down Expand Up @@ -540,10 +542,10 @@ private IPEndPoint GetIPEndPoint(EndPoint endpoint)
{
var dnsEndPoint = (DnsEndPoint)endpoint;
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
ip.AddressFamily == AddressFamily.InterNetwork);
if (address == null)
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", endpoint));
return new IPEndPoint(address, dnsEndPoint.Port);
ip.AddressFamily == (_useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork));
return address == null
? throw new ArgumentException(string.Format("Could not resolve host '{0}'.", endpoint))
: new IPEndPoint(address, dnsEndPoint.Port);
}
else if (endpoint is IPEndPoint)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public BinaryNode(
ISocketPoolConfiguration config,
ISaslAuthenticationProvider authenticationProvider,
ILogger logger,
bool useSslStream)
: base(endpoint, config, logger, useSslStream)
bool useSslStream,
bool useIPv6)
: base(endpoint, config, logger, useSslStream, useIPv6)
{
_authenticationProvider = authenticationProvider;
_logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion src/Enyim.Caching/Memcached/Protocol/Binary/BinaryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public BinaryPool(IMemcachedClientConfiguration configuration, ILogger logger)

protected override IMemcachedNode CreateNode(EndPoint endpoint)
{
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream);
return new BinaryNode(endpoint, _configuration.SocketPool, _authenticationProvider, _logger, _configuration.UseSslStream, _configuration.UseIPv6);
}

private static ISaslAuthenticationProvider GetProvider(IMemcachedClientConfiguration configuration)
Expand Down

0 comments on commit 891bd6b

Please sign in to comment.