Skip to content

File tree

src/Transport/Failover/FailoverTransport.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ public class FailoverTransport : ICompositeTransport, IComparable
9494
private List<Uri> priorityList = new List<Uri>();
9595
private bool priorityBackupAvailable = false;
9696
private String sslProtocol = null;
97+
private string nestedExtraQueryOptions;
9798

98-
// Not Sure how to work these back in with all the changes.
99-
//private int asyncTimeout = 45000;
99+
// Not Sure how to work these back in with all the changes.
100+
//private int asyncTimeout = 45000;
100101
//private bool asyncConnect = false;
101102

102103
public FailoverTransport()
@@ -1177,7 +1178,7 @@ private bool DoConnect()
11771178
// URI from the pool until next time around.
11781179
if (transport == null)
11791180
{
1180-
uri = iter.Current;
1181+
uri = AddExtraQueryOptions(iter.Current);
11811182
transport = TransportFactory.CompositeConnect(uri);
11821183
}
11831184

@@ -1310,14 +1311,14 @@ private bool BuildBackups()
13101311
}
13111312
}
13121313

1313-
foreach(Uri uri in connectList)
1314+
foreach(Uri u in connectList)
13141315
{
13151316
if (disposed)
13161317
{
13171318
break;
13181319
}
1319-
1320-
if(ConnectedTransportURI != null && !ConnectedTransportURI.Equals(uri))
1320+
Uri uri = AddExtraQueryOptions(u);
1321+
if (ConnectedTransportURI != null && !ConnectedTransportURI.Equals(uri))
13211322
{
13221323
try
13231324
{
@@ -1712,5 +1713,35 @@ private bool Contains(Uri newURI)
17121713

17131714
return result;
17141715
}
1716+
1717+
public void SetNestedExtraQueryOptions(String nestedExtraQueryOptions)
1718+
{
1719+
this.nestedExtraQueryOptions = nestedExtraQueryOptions;
1720+
}
1721+
1722+
private Uri AddExtraQueryOptions(Uri uri)
1723+
{
1724+
try
1725+
{
1726+
if (!string.IsNullOrEmpty(nestedExtraQueryOptions))
1727+
{
1728+
if (uri.Query == null)
1729+
{
1730+
uri = URISupport.CreateUriWithQuery(uri, nestedExtraQueryOptions);
1731+
}
1732+
else
1733+
{
1734+
uri = URISupport.CreateUriWithQuery(uri, uri.Query + "&" + nestedExtraQueryOptions);
1735+
}
1736+
Tracer.Info($"URI with nested parameter is {uri.ToString()}");
1737+
}
1738+
}
1739+
catch (UriFormatException e)
1740+
{
1741+
Tracer.Error(e.Message);
1742+
throw;
1743+
}
1744+
return uri;
1745+
}
17151746
}
17161747
}

src/Transport/Failover/FailoverTransportFactory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ public virtual ITransport CreateTransport(URISupport.CompositeData compositData)
5555
StringDictionary options = compositData.Parameters;
5656
FailoverTransport transport = CreateTransport(options);
5757
transport.Add(false, compositData.Components);
58-
return transport;
58+
try
59+
{
60+
transport.SetNestedExtraQueryOptions(URISupport.CreateQueryString(URISupport.GetProperties(options, "nested.")));
61+
}
62+
catch (Exception e)
63+
{
64+
Tracer.Error($"Error in setting nested parameters {e.Message}");
65+
}
66+
return transport;
5967
}
6068

6169
protected FailoverTransport CreateTransport(StringDictionary parameters)

test/Transport/failover/FailoverTransportTest.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,36 @@ public void FailoverTransportWithBackupsTest()
162162
Assert.IsTrue(failover.IsConnected);
163163
}
164164
}
165+
[Test]
166+
public void FailoverTransportWithNestedParametersTest()
167+
{
168+
Uri uri = new Uri("failover:(mock://localhost:61616)?transport.randomize=false&transport.backup=true&nested.transport.failOnSendMessage=true&nested.transport.numSentMessagesBeforeFail=20");
169+
FailoverTransportFactory factory = new FailoverTransportFactory();
165170

166-
[Test]
171+
using (ITransport transport = factory.CreateTransport(uri))
172+
{
173+
Assert.IsNotNull(transport);
174+
transport.CommandAsync = OnCommand;
175+
transport.Exception = OnException;
176+
transport.Resumed = OnResumed;
177+
transport.Interrupted = OnInterrupted;
178+
179+
FailoverTransport failover = transport.Narrow(typeof(FailoverTransport)) as FailoverTransport;
180+
Assert.IsNotNull(failover);
181+
Assert.IsFalse(failover.Randomize);
182+
Assert.IsTrue(failover.Backup);
183+
184+
transport.Start();
185+
Thread.Sleep(1000);
186+
Assert.IsTrue(failover.IsConnected);
187+
188+
MockTransport mock = transport.Narrow(typeof(MockTransport)) as MockTransport;
189+
Assert.IsNotNull(mock);
190+
Assert.IsTrue(mock.FailOnSendMessage);
191+
Assert.AreEqual(20,mock.NumSentMessagesBeforeFail);
192+
}
193+
}
194+
[Test]
167195
public void FailoverTransportCreateFailOnCreateTest()
168196
{
169197
Uri uri = new Uri("failover:(mock://localhost:61616?transport.failOnCreate=true)?" +

0 commit comments

Comments
 (0)