Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions RabbitMQ.Stream.Client/RoutingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,34 @@ int maxAttempts
{
var routing = new T();

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

if (routing.ValidateDns)
{
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
endPoint = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
}

if (clientParameters.AddressResolver == null
|| clientParameters.AddressResolver.Enabled == false)
{
// We use the localhost ip as default
// this is mostly to have a default value.

var 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);
}

// In this case we just return the node (leader for producer, random for consumer)
// since there is not load balancer configuration

return routing.CreateClient(clientParameters with { Endpoint = endPoint });
return routing.CreateClient(clientParameters with { Endpoint = endPointNoLb });
}

// here it means that there is a AddressResolver configuration
// so there is a load-balancer or proxy we need to get the right connection
// as first we try with the first node given from the LB
endPoint = clientParameters.AddressResolver.EndPoint;
var endPoint = clientParameters.AddressResolver.EndPoint;
var client = routing.CreateClient(clientParameters with { Endpoint = endPoint });

var advertisedHost = GetPropertyValue(client.ConnectionProperties, "advertised_host");
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void AddressResolverLoadBalancerSimulate()
AddressResolver = addressResolver,
};
var metaDataInfo = new StreamInfo("stream", ResponseCode.Ok, new Broker("node2", 5552),
new List<Broker>() { new Broker("replica", 5552) });
new List<Broker>() { new Broker("node1", 5552), new Broker("node3", 5552) });
// run more than one time just to be sure to use all the IP with random
for (var i = 0; i < 4; i++)
{
Expand Down