Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Enyim.Caching/Configuration/EndPointExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace Enyim.Caching.Configuration;
public static class EndPointExtensions
{
public static IPEndPoint GetIPEndPoint(this EndPoint endpoint, bool useIPv6)
{
if (endpoint is IPEndPoint ipEndPoint)
{
return ipEndPoint;
}
else if (endpoint is DnsEndPoint dnsEndPoint)
{
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
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
{
throw new Exception("Not supported EndPoint type");
}
}
}
23 changes: 4 additions & 19 deletions src/Enyim.Caching/Memcached/PooledSocket.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Enyim.Caching.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -430,15 +431,15 @@ public async Task ReadAsync(byte[] buffer, int offset, int count)
? _sslStream.ReadAsync(buffer, offset, shouldRead)
: _inputStream.ReadAsync(buffer, offset, shouldRead);
var timeoutTask = Task.Delay(_receiveTimeout);

if (await Task.WhenAny(readTask, timeoutTask).ConfigureAwait(false) == readTask)
{
int currentRead = await readTask.ConfigureAwait(false);
if (currentRead == count)
break;
if (currentRead < 1)
throw new IOException("The socket seems to be disconnected");

read += currentRead;
offset += currentRead;
shouldRead -= currentRead;
Expand Down Expand Up @@ -618,23 +619,7 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)

private IPEndPoint GetIPEndPoint(EndPoint endpoint)
{
if (endpoint is DnsEndPoint)
{
var dnsEndPoint = (DnsEndPoint)endpoint;
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
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)
{
return endpoint as IPEndPoint;
}
else
{
throw new Exception("Not supported EndPoint type");
}
return endpoint.GetIPEndPoint(_useIPv6);
}
}
}
Expand Down
39 changes: 24 additions & 15 deletions src/Enyim.Caching/Memcached/ServerStats.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Enyim.Caching.Configuration;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace Enyim.Caching.Memcached
Expand All @@ -17,6 +18,7 @@ public sealed class ServerStats
/// Defines a value which indicates that the statstics should be retrieved for all servers in the pool.
/// </summary>
public static readonly IPEndPoint All = new IPEndPoint(IPAddress.Any, 0);

#region [ readonly int[] Optable ]
// defines which values can be summed and which not
private static readonly int[] Optable =
Expand All @@ -25,6 +27,7 @@ public sealed class ServerStats
1, 1, 1, 1, 1, 1, 1, 1
};
#endregion

#region [ readonly string[] StatKeys ]
private static readonly string[] StatKeys =
{
Expand All @@ -47,11 +50,14 @@ public sealed class ServerStats
};
#endregion

private Dictionary<EndPoint, Dictionary<string, string>> _results;
private readonly Dictionary<EndPoint, Dictionary<string, string>> _results;

internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results)
private readonly bool _useIPv6;

internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results, bool useIPv6)
{
_results = results;
_useIPv6 = useIPv6;
}

/// <summary>
Expand All @@ -62,12 +68,14 @@ internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results)
/// <returns>The value of the specified stat item</returns>
public long GetValue(EndPoint server, StatItem item)
{
server = server.GetIPEndPoint(_useIPv6);

// asked for a specific server
if (server is not IPEndPoint || ((IPEndPoint)server).Address != IPAddress.Any)
{
// error check
string tmp = GetRaw(server, item);
if (String.IsNullOrEmpty(tmp))
if (string.IsNullOrEmpty(tmp))
throw new ArgumentException("Item was not found: " + item);

long value;
Expand Down Expand Up @@ -100,8 +108,9 @@ public long GetValue(EndPoint server, StatItem item)
/// <returns>The version of memcached</returns>
public Version GetVersion(EndPoint server)
{
server = server.GetIPEndPoint(_useIPv6);
string version = GetRaw(server, StatItem.Version);
if (String.IsNullOrEmpty(version))
if (string.IsNullOrEmpty(version))
throw new ArgumentException("No version found for the server " + server);

return new Version(version);
Expand All @@ -114,12 +123,13 @@ public Version GetVersion(EndPoint server)
/// <returns>A value indicating how long the server is running</returns>
public TimeSpan GetUptime(EndPoint server)
{
server = server.GetIPEndPoint(_useIPv6);
string uptime = GetRaw(server, StatItem.Uptime);
if (String.IsNullOrEmpty(uptime))
if (string.IsNullOrEmpty(uptime))
throw new ArgumentException("No uptime found for the server " + server);

long value;
if (!Int64.TryParse(uptime, out value))
if (!long.TryParse(uptime, out value))
throw new ArgumentException("Invalid uptime string was returned: " + uptime);

return TimeSpan.FromSeconds(value);
Expand All @@ -133,12 +143,11 @@ public TimeSpan GetUptime(EndPoint server)
/// <returns>The value of the stat item</returns>
public string GetRaw(EndPoint server, string key)
{
Dictionary<string, string> serverValues;
string retval;
server = server.GetIPEndPoint(_useIPv6);

if (_results.TryGetValue(server, out serverValues))
if (_results.TryGetValue(server, out Dictionary<string, string> serverValues))
{
if (serverValues.TryGetValue(key, out retval))
if (serverValues.TryGetValue(key, out string retval))
return retval;

if (_log.IsDebugEnabled)
Expand All @@ -161,17 +170,17 @@ public string GetRaw(EndPoint server, string key)
/// <returns>The value of the stat item</returns>
public string GetRaw(EndPoint server, StatItem item)
{
server = server.GetIPEndPoint(_useIPv6);

if ((int)item < StatKeys.Length && (int)item >= 0)
return GetRaw(server, StatKeys[(int)item]);

throw new ArgumentOutOfRangeException("item");
throw new ArgumentOutOfRangeException(nameof(item));
}

public IEnumerable<KeyValuePair<EndPoint, string>> GetRaw(string key)
{
string tmp;

return _results.Select(kvp => new KeyValuePair<EndPoint, string>(kvp.Key, kvp.Value.TryGetValue(key, out tmp) ? tmp : null)).ToList();
return _results.Select(kvp => new KeyValuePair<EndPoint, string>(kvp.Key, kvp.Value.TryGetValue(key, out string tmp) ? tmp : null)).ToList();
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient
/// Represents a value which indicates that an item should never expire.
/// </summary>
public static readonly TimeSpan Infinite = TimeSpan.Zero;
private ILogger<MemcachedClient> _logger;
private bool _suppressException;
private readonly ILogger<MemcachedClient> _logger;
private readonly bool _suppressException;
private readonly IMemcachedKeyTransformer _keyTransformer;
private readonly ITranscoder _transcoder;
private readonly bool _userIPv6;

private IServerPool _pool;
private IMemcachedKeyTransformer _keyTransformer;
private ITranscoder _transcoder;

public IStoreOperationResultFactory StoreOperationResultFactory { get; set; }
public IGetOperationResultFactory GetOperationResultFactory { get; set; }
Expand All @@ -48,6 +49,7 @@ public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfigurati
throw new ArgumentNullException(nameof(configuration));
}

_userIPv6 = configuration.UseIPv6;
_suppressException = configuration.SuppressException;
_keyTransformer = configuration.CreateKeyTransformer() ?? new DefaultKeyTransformer();
_transcoder = configuration.CreateTranscoder() ?? new DefaultTranscoder();
Expand Down Expand Up @@ -1199,7 +1201,7 @@ public ServerStats Stats(string type)
Task.WaitAll(tasks.ToArray());
}

return new ServerStats(results);
return new ServerStats(results, _userIPv6);
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Enyim.Caching/NullMemcachedClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached.Results.Factories;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached.Results.Factories;

namespace Enyim.Caching
{
Expand Down Expand Up @@ -200,7 +200,7 @@ public Task<bool> RemoveMultiAsync(params string[] keys)

public ServerStats Stats()
{
return new ServerStats(new Dictionary<EndPoint, Dictionary<string, string>>());
return new ServerStats(new Dictionary<EndPoint, Dictionary<string, string>>(), false);
}

public ServerStats Stats(string type)
Expand Down
Loading
Loading