Skip to content

Commit e4959cf

Browse files
authored
Change the DNS validation (#126)
Fixes #125 Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 62217de commit e4959cf

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

RabbitMQ.Stream.Client/RoutingClient.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,34 @@ int maxAttempts
4646
{
4747
var routing = new T();
4848

49-
var endPoint = new IPEndPoint(IPAddress.Loopback, (int)broker.Port);
50-
51-
if (routing.ValidateDns)
52-
{
53-
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
54-
endPoint = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
55-
}
56-
5749
if (clientParameters.AddressResolver == null
5850
|| clientParameters.AddressResolver.Enabled == false)
5951
{
52+
// We use the localhost ip as default
53+
// this is mostly to have a default value.
54+
55+
var endPointNoLb = new IPEndPoint(IPAddress.Loopback, (int)broker.Port);
56+
57+
// ValidateDns just validate the DNS
58+
// it the real world application is always TRUE
59+
// routing.ValidateDns == false is used just for test
60+
// it should not change.
61+
if (routing.ValidateDns)
62+
{
63+
var hostEntry = await Dns.GetHostEntryAsync(broker.Host);
64+
endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port);
65+
}
66+
6067
// In this case we just return the node (leader for producer, random for consumer)
6168
// since there is not load balancer configuration
6269

63-
return routing.CreateClient(clientParameters with { Endpoint = endPoint });
70+
return routing.CreateClient(clientParameters with { Endpoint = endPointNoLb });
6471
}
6572

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

7279
var advertisedHost = GetPropertyValue(client.ConnectionProperties, "advertised_host");

Tests/UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void AddressResolverLoadBalancerSimulate()
155155
AddressResolver = addressResolver,
156156
};
157157
var metaDataInfo = new StreamInfo("stream", ResponseCode.Ok, new Broker("node2", 5552),
158-
new List<Broker>() { new Broker("replica", 5552) });
158+
new List<Broker>() { new Broker("node1", 5552), new Broker("node3", 5552) });
159159
// run more than one time just to be sure to use all the IP with random
160160
for (var i = 0; i < 4; i++)
161161
{

0 commit comments

Comments
 (0)