Skip to content

Commit 4dc56c4

Browse files
committed
Enable IP address connection string in Basket.API
Dns.GetHostAddressesAsync can return problematic results when passed an IP address, and if the connection string is already an IP address, we needn't call it anyway.
1 parent e5cc7cd commit 4dc56c4

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ private async Task<IDatabase> GetDatabase()
6363
{
6464
if (_redis == null)
6565
{
66-
//TODO: Need to make this more robust. Also want to understand why the static connection method cannot accept dns names.
67-
var ips = await Dns.GetHostAddressesAsync(_settings.ConnectionString);
68-
_logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}");
69-
_redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString());
66+
if (IPAddress.TryParse(_settings.ConnectionString, out var ip))
67+
{
68+
_redis = await ConnectionMultiplexer.ConnectAsync(ip.ToString());
69+
_logger.LogInformation($"Connecting to database at {_settings.ConnectionString}");
70+
}
71+
else
72+
{
73+
// workaround for https://github.com/StackExchange/StackExchange.Redis/issues/410
74+
var ips = await Dns.GetHostAddressesAsync(_settings.ConnectionString);
75+
_logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}");
76+
_redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString());
77+
}
7078
}
7179

7280
return _redis.GetDatabase();

0 commit comments

Comments
 (0)