Skip to content

Commit da5b913

Browse files
committed
Resolve hostname to IP address in MemcachedClientConfiguration
1 parent 19cafc7 commit da5b913

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.Extensions.Logging;
88
using Microsoft.Extensions.Options;
99
using Microsoft.Extensions.Configuration;
10+
using System.Linq;
11+
using System.Net.Sockets;
1012

1113
namespace Enyim.Caching.Configuration
1214
{
@@ -41,23 +43,19 @@ public MemcachedClientConfiguration(
4143
var options = optionsAccessor.Value;
4244
if ((options == null || options.Servers.Count == 0) && configuration != null)
4345
{
44-
var section = configuration.GetSection("enyimMemcached");
46+
var section = configuration.GetSection("enyimMemcached");
4547
if (section.Exists())
4648
{
47-
section.Bind(options);
49+
section.Bind(options);
4850
}
4951
else
5052
{
5153
_logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration");
5254
options.AddDefaultServer();
53-
}
55+
}
5456
}
5557

56-
Servers = new List<DnsEndPoint>();
57-
foreach (var server in options.Servers)
58-
{
59-
Servers.Add(new DnsEndPoint(server.Address, server.Port));
60-
}
58+
ConfigureServers(options);
6159

6260
SocketPool = new SocketPoolConfiguration();
6361
if (options.SocketPool != null)
@@ -172,6 +170,35 @@ public MemcachedClientConfiguration(
172170
}
173171
}
174172

173+
private void ConfigureServers(MemcachedClientOptions options)
174+
{
175+
Servers = new List<DnsEndPoint>();
176+
foreach (var server in options.Servers)
177+
{
178+
if (!IPAddress.TryParse(server.Address, out var address))
179+
{
180+
var ip = Dns.GetHostAddresses(server.Address)
181+
.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork)?.ToString();
182+
183+
if (ip == null)
184+
{
185+
_logger.LogError($"Could not resolve host '{server.Address}'.");
186+
}
187+
else
188+
{
189+
_logger.LogInformation($"Memcached server address - {server.Address }({ip}):{server.Port}");
190+
server.Address = ip;
191+
}
192+
}
193+
else
194+
{
195+
_logger.LogInformation($"Memcached server address - {server.Address }:{server.Port}");
196+
}
197+
198+
Servers.Add(new DnsEndPoint(server.Address, server.Port));
199+
}
200+
}
201+
175202
/// <summary>
176203
/// Adds a new server to the pool.
177204
/// </summary>

0 commit comments

Comments
 (0)