Skip to content

Commit

Permalink
Decode the address type (#219)
Browse files Browse the repository at this point in the history
Fixes #218

Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
  • Loading branch information
Gsantomaggio authored Jan 31, 2023
1 parent 7f2e9f7 commit b5bfe4c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions RabbitMQ.Stream.Client/RoutingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ private static async Task<IClient> LookupConnection(
// We use the localhost ip as default
// this is mostly to have a default value.

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

// ValidateDns just validate the DNS
// it the real world application is always TRUE
// routing.ValidateDns == false is used just for test
// it should not change.
if (routing.ValidateDns)
{
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
endPointNoLb = await GetEndPoint(broker);
}

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

internal static async Task<EndPoint> GetEndPoint(Broker broker)
{
switch (Uri.CheckHostName(broker.Host))
{
case UriHostNameType.Basic:
case UriHostNameType.Dns:
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
var endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
return endPointNoLb;
case UriHostNameType.IPv4:
case UriHostNameType.IPv6:
return new IPEndPoint(IPAddress.Parse(broker.Host), (int)broker.Port);
case UriHostNameType.Unknown:
throw new RoutingClientException($"Unknown host name {broker.Host}");
default:
throw new RoutingClientException($"Unknown host name {broker.Host}");
}
}

private static int MaxAttempts(StreamInfo metaDataInfo)
{
// Here we have a reasonable number of retry.
Expand Down

0 comments on commit b5bfe4c

Please sign in to comment.