Skip to content

Commit b5bfe4c

Browse files
authored
Decode the address type (#219)
Fixes #218 Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 7f2e9f7 commit b5bfe4c

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

RabbitMQ.Stream.Client/RoutingClient.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ private static async Task<IClient> LookupConnection(
5151
// We use the localhost ip as default
5252
// this is mostly to have a default value.
5353

54-
var endPointNoLb = new IPEndPoint(IPAddress.Loopback, (int)broker.Port);
54+
EndPoint endPointNoLb = new IPEndPoint(IPAddress.Loopback, (int)broker.Port);
5555

5656
// ValidateDns just validate the DNS
5757
// it the real world application is always TRUE
5858
// routing.ValidateDns == false is used just for test
5959
// it should not change.
6060
if (routing.ValidateDns)
6161
{
62-
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
63-
endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
62+
endPointNoLb = await GetEndPoint(broker);
6463
}
6564

6665
// In this case we just return the node (leader for producer, random for consumer)
@@ -100,6 +99,25 @@ private static async Task<IClient> LookupConnection(
10099
return client;
101100
}
102101

102+
internal static async Task<EndPoint> GetEndPoint(Broker broker)
103+
{
104+
switch (Uri.CheckHostName(broker.Host))
105+
{
106+
case UriHostNameType.Basic:
107+
case UriHostNameType.Dns:
108+
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
109+
var endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
110+
return endPointNoLb;
111+
case UriHostNameType.IPv4:
112+
case UriHostNameType.IPv6:
113+
return new IPEndPoint(IPAddress.Parse(broker.Host), (int)broker.Port);
114+
case UriHostNameType.Unknown:
115+
throw new RoutingClientException($"Unknown host name {broker.Host}");
116+
default:
117+
throw new RoutingClientException($"Unknown host name {broker.Host}");
118+
}
119+
}
120+
103121
private static int MaxAttempts(StreamInfo metaDataInfo)
104122
{
105123
// Here we have a reasonable number of retry.

0 commit comments

Comments
 (0)