Skip to content

Commit 89179ea

Browse files
authored
Merge pull request #258 from cnblogs/get-stats-via-hostname
feat: get stats via hostname
2 parents f93de0b + 7c5401e commit 89179ea

File tree

6 files changed

+122
-110
lines changed

6 files changed

+122
-110
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Net.Sockets;
5+
6+
namespace Enyim.Caching.Configuration;
7+
public static class EndPointExtensions
8+
{
9+
public static IPEndPoint GetIPEndPoint(this EndPoint endpoint, bool useIPv6)
10+
{
11+
if (endpoint is IPEndPoint ipEndPoint)
12+
{
13+
return ipEndPoint;
14+
}
15+
else if (endpoint is DnsEndPoint dnsEndPoint)
16+
{
17+
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
18+
ip.AddressFamily == (useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork));
19+
return address == null
20+
? throw new ArgumentException(string.Format("Could not resolve host '{0}'.", endpoint))
21+
: new IPEndPoint(address, dnsEndPoint.Port);
22+
}
23+
else
24+
{
25+
throw new Exception("Not supported EndPoint type");
26+
}
27+
}
28+
}

src/Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Enyim.Caching.Configuration;
12
using Microsoft.Extensions.Logging;
23
using System;
34
using System.Collections.Generic;
@@ -430,15 +431,15 @@ public async Task ReadAsync(byte[] buffer, int offset, int count)
430431
? _sslStream.ReadAsync(buffer, offset, shouldRead)
431432
: _inputStream.ReadAsync(buffer, offset, shouldRead);
432433
var timeoutTask = Task.Delay(_receiveTimeout);
433-
434+
434435
if (await Task.WhenAny(readTask, timeoutTask).ConfigureAwait(false) == readTask)
435436
{
436437
int currentRead = await readTask.ConfigureAwait(false);
437438
if (currentRead == count)
438439
break;
439440
if (currentRead < 1)
440441
throw new IOException("The socket seems to be disconnected");
441-
442+
442443
read += currentRead;
443444
offset += currentRead;
444445
shouldRead -= currentRead;
@@ -618,23 +619,7 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
618619

619620
private IPEndPoint GetIPEndPoint(EndPoint endpoint)
620621
{
621-
if (endpoint is DnsEndPoint)
622-
{
623-
var dnsEndPoint = (DnsEndPoint)endpoint;
624-
var address = Dns.GetHostAddresses(dnsEndPoint.Host).FirstOrDefault(ip =>
625-
ip.AddressFamily == (_useIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork));
626-
return address == null
627-
? throw new ArgumentException(string.Format("Could not resolve host '{0}'.", endpoint))
628-
: new IPEndPoint(address, dnsEndPoint.Port);
629-
}
630-
else if (endpoint is IPEndPoint)
631-
{
632-
return endpoint as IPEndPoint;
633-
}
634-
else
635-
{
636-
throw new Exception("Not supported EndPoint type");
637-
}
622+
return endpoint.GetIPEndPoint(_useIPv6);
638623
}
639624
}
640625
}

src/Enyim.Caching/Memcached/ServerStats.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
using Enyim.Caching.Configuration;
12
using System;
2-
using System.Linq;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Net;
56

67
namespace Enyim.Caching.Memcached
@@ -17,6 +18,7 @@ public sealed class ServerStats
1718
/// Defines a value which indicates that the statstics should be retrieved for all servers in the pool.
1819
/// </summary>
1920
public static readonly IPEndPoint All = new IPEndPoint(IPAddress.Any, 0);
21+
2022
#region [ readonly int[] Optable ]
2123
// defines which values can be summed and which not
2224
private static readonly int[] Optable =
@@ -25,6 +27,7 @@ public sealed class ServerStats
2527
1, 1, 1, 1, 1, 1, 1, 1
2628
};
2729
#endregion
30+
2831
#region [ readonly string[] StatKeys ]
2932
private static readonly string[] StatKeys =
3033
{
@@ -47,11 +50,14 @@ public sealed class ServerStats
4750
};
4851
#endregion
4952

50-
private Dictionary<EndPoint, Dictionary<string, string>> _results;
53+
private readonly Dictionary<EndPoint, Dictionary<string, string>> _results;
5154

52-
internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results)
55+
private readonly bool _useIPv6;
56+
57+
internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results, bool useIPv6)
5358
{
5459
_results = results;
60+
_useIPv6 = useIPv6;
5561
}
5662

5763
/// <summary>
@@ -62,12 +68,14 @@ internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results)
6268
/// <returns>The value of the specified stat item</returns>
6369
public long GetValue(EndPoint server, StatItem item)
6470
{
71+
server = server.GetIPEndPoint(_useIPv6);
72+
6573
// asked for a specific server
6674
if (server is not IPEndPoint || ((IPEndPoint)server).Address != IPAddress.Any)
6775
{
6876
// error check
6977
string tmp = GetRaw(server, item);
70-
if (String.IsNullOrEmpty(tmp))
78+
if (string.IsNullOrEmpty(tmp))
7179
throw new ArgumentException("Item was not found: " + item);
7280

7381
long value;
@@ -100,8 +108,9 @@ public long GetValue(EndPoint server, StatItem item)
100108
/// <returns>The version of memcached</returns>
101109
public Version GetVersion(EndPoint server)
102110
{
111+
server = server.GetIPEndPoint(_useIPv6);
103112
string version = GetRaw(server, StatItem.Version);
104-
if (String.IsNullOrEmpty(version))
113+
if (string.IsNullOrEmpty(version))
105114
throw new ArgumentException("No version found for the server " + server);
106115

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

121131
long value;
122-
if (!Int64.TryParse(uptime, out value))
132+
if (!long.TryParse(uptime, out value))
123133
throw new ArgumentException("Invalid uptime string was returned: " + uptime);
124134

125135
return TimeSpan.FromSeconds(value);
@@ -133,12 +143,11 @@ public TimeSpan GetUptime(EndPoint server)
133143
/// <returns>The value of the stat item</returns>
134144
public string GetRaw(EndPoint server, string key)
135145
{
136-
Dictionary<string, string> serverValues;
137-
string retval;
146+
server = server.GetIPEndPoint(_useIPv6);
138147

139-
if (_results.TryGetValue(server, out serverValues))
148+
if (_results.TryGetValue(server, out Dictionary<string, string> serverValues))
140149
{
141-
if (serverValues.TryGetValue(key, out retval))
150+
if (serverValues.TryGetValue(key, out string retval))
142151
return retval;
143152

144153
if (_log.IsDebugEnabled)
@@ -161,17 +170,17 @@ public string GetRaw(EndPoint server, string key)
161170
/// <returns>The value of the stat item</returns>
162171
public string GetRaw(EndPoint server, StatItem item)
163172
{
173+
server = server.GetIPEndPoint(_useIPv6);
174+
164175
if ((int)item < StatKeys.Length && (int)item >= 0)
165176
return GetRaw(server, StatKeys[(int)item]);
166177

167-
throw new ArgumentOutOfRangeException("item");
178+
throw new ArgumentOutOfRangeException(nameof(item));
168179
}
169180

170181
public IEnumerable<KeyValuePair<EndPoint, string>> GetRaw(string key)
171182
{
172-
string tmp;
173-
174-
return _results.Select(kvp => new KeyValuePair<EndPoint, string>(kvp.Key, kvp.Value.TryGetValue(key, out tmp) ? tmp : null)).ToList();
183+
return _results.Select(kvp => new KeyValuePair<EndPoint, string>(kvp.Key, kvp.Value.TryGetValue(key, out string tmp) ? tmp : null)).ToList();
175184
}
176185
}
177186
}

src/Enyim.Caching/MemcachedClient.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient
2222
/// Represents a value which indicates that an item should never expire.
2323
/// </summary>
2424
public static readonly TimeSpan Infinite = TimeSpan.Zero;
25-
private ILogger<MemcachedClient> _logger;
26-
private bool _suppressException;
25+
private readonly ILogger<MemcachedClient> _logger;
26+
private readonly bool _suppressException;
27+
private readonly IMemcachedKeyTransformer _keyTransformer;
28+
private readonly ITranscoder _transcoder;
29+
private readonly bool _userIPv6;
2730

2831
private IServerPool _pool;
29-
private IMemcachedKeyTransformer _keyTransformer;
30-
private ITranscoder _transcoder;
3132

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

52+
_userIPv6 = configuration.UseIPv6;
5153
_suppressException = configuration.SuppressException;
5254
_keyTransformer = configuration.CreateKeyTransformer() ?? new DefaultKeyTransformer();
5355
_transcoder = configuration.CreateTranscoder() ?? new DefaultTranscoder();
@@ -1199,7 +1201,7 @@ public ServerStats Stats(string type)
11991201
Task.WaitAll(tasks.ToArray());
12001202
}
12011203

1202-
return new ServerStats(results);
1204+
return new ServerStats(results, _userIPv6);
12031205
}
12041206

12051207
/// <summary>

src/Enyim.Caching/NullMemcachedClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using Enyim.Caching.Memcached;
2+
using Enyim.Caching.Memcached.Results;
3+
using Enyim.Caching.Memcached.Results.Factories;
4+
using System;
25
using System.Collections.Generic;
36
using System.Net;
47
using System.Threading.Tasks;
5-
using Enyim.Caching.Memcached;
6-
using Enyim.Caching.Memcached.Results;
7-
using Enyim.Caching.Memcached.Results.Factories;
88

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

201201
public ServerStats Stats()
202202
{
203-
return new ServerStats(new Dictionary<EndPoint, Dictionary<string, string>>());
203+
return new ServerStats(new Dictionary<EndPoint, Dictionary<string, string>>(), false);
204204
}
205205

206206
public ServerStats Stats(string type)

0 commit comments

Comments
 (0)