Skip to content

Commit d39f36b

Browse files
authored
Merge pull request #1416 from rabbitmq/rabbitmq-dotnet-client-1414-6.x
Removed ReceiveBufferSize and SendBufferSize to improve message rates
2 parents ac2c4de + b612715 commit d39f36b

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

projects/RabbitMQ.Client/client/api/ConnectionFactoryBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public static ITcpClient DefaultSocketFactory(AddressFamily addressFamily)
5151
{
5252
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
5353
{
54-
NoDelay = true,
55-
ReceiveBufferSize = 65536,
56-
SendBufferSize = 65536
54+
NoDelay = true
5755
};
5856
return new TcpClientAdapter(socket);
5957
}

projects/Unit/TestConnectionFactory.cs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
using System;
3333
using System.Collections.Generic;
34+
using System.Net.Sockets;
3435
using System.Text;
3536
using NUnit.Framework;
36-
3737
using RabbitMQ.Client.Exceptions;
38+
using RabbitMQ.Client.Impl;
3839

3940
namespace RabbitMQ.Client.Unit
4041
{
@@ -44,12 +45,12 @@ public class TestConnectionFactory
4445
[Test]
4546
public void TestProperties()
4647
{
47-
string u = "username";
48+
string u = "username";
4849
string pw = "password";
49-
string v = "vhost";
50-
string h = "192.168.0.1";
51-
int p = 5674;
52-
uint mms = 512 * 1024 * 1024;
50+
string v = "vhost";
51+
string h = "192.168.0.1";
52+
int p = 5674;
53+
uint mms = 512 * 1024 * 1024;
5354

5455
var cf = new ConnectionFactory
5556
{
@@ -73,6 +74,35 @@ public void TestProperties()
7374
Assert.AreEqual(cf.Endpoint.MaxMessageSize, mms);
7475
}
7576

77+
[Test]
78+
public void TestConnectionFactoryWithCustomSocketFactory()
79+
{
80+
const int defaultSocketBufsz = 8192; // From the docs
81+
const int bufsz = 1024;
82+
83+
var cf = new ConnectionFactory
84+
{
85+
SocketFactory = (AddressFamily af) =>
86+
{
87+
var socket = new Socket(af, SocketType.Stream, ProtocolType.Tcp)
88+
{
89+
SendBufferSize = bufsz,
90+
ReceiveBufferSize = bufsz,
91+
NoDelay = false
92+
};
93+
return new TcpClientAdapter(socket);
94+
}
95+
};
96+
97+
ITcpClient c = cf.SocketFactory(AddressFamily.InterNetwork);
98+
Assert.IsAssignableFrom(typeof(TcpClientAdapter), c);
99+
TcpClientAdapter tcpClientAdapter = (TcpClientAdapter)c;
100+
Socket s = tcpClientAdapter.Client;
101+
Assert.AreNotEqual(defaultSocketBufsz, s.ReceiveBufferSize);
102+
Assert.AreNotEqual(defaultSocketBufsz, s.SendBufferSize);
103+
Assert.False(s.NoDelay);
104+
}
105+
76106
[Test]
77107
public void TestCreateConnectionUsesSpecifiedPort()
78108
{
@@ -84,7 +114,7 @@ public void TestCreateConnectionUsesSpecifiedPort()
84114
};
85115
Assert.That(() =>
86116
{
87-
using(IConnection conn = cf.CreateConnection()) {}
117+
using (IConnection conn = cf.CreateConnection()) { }
88118
}, Throws.TypeOf<BrokerUnreachableException>());
89119
}
90120

@@ -99,7 +129,7 @@ public void TestCreateConnectionWithClientProvidedNameUsesSpecifiedPort()
99129
};
100130
Assert.That(() =>
101131
{
102-
using(IConnection conn = cf.CreateConnection("some_name")) {}
132+
using (IConnection conn = cf.CreateConnection("some_name")) { }
103133
}, Throws.TypeOf<BrokerUnreachableException>());
104134
}
105135

@@ -154,10 +184,10 @@ public void TestCreateConnectionAmqpTcpEndpointListAndClientProvidedName()
154184
AutomaticRecoveryEnabled = true
155185
};
156186
var xs = new System.Collections.Generic.List<AmqpTcpEndpoint> { new AmqpTcpEndpoint("localhost") };
157-
using(IConnection conn = cf.CreateConnection(xs, "some_name"))
187+
using (IConnection conn = cf.CreateConnection(xs, "some_name"))
158188
{
159-
Assert.AreEqual("some_name", conn.ClientProvidedName);
160-
Assert.AreEqual("some_name", conn.ClientProperties["connection_name"]);
189+
Assert.AreEqual("some_name", conn.ClientProvidedName);
190+
Assert.AreEqual("some_name", conn.ClientProperties["connection_name"]);
161191
}
162192
}
163193

@@ -169,7 +199,8 @@ public void TestCreateConnectionUsesDefaultPort()
169199
AutomaticRecoveryEnabled = true,
170200
HostName = "localhost"
171201
};
172-
using (IConnection conn = cf.CreateConnection()){
202+
using (IConnection conn = cf.CreateConnection())
203+
{
173204
Assert.AreEqual(5672, conn.Endpoint.Port);
174205
}
175206
}
@@ -217,7 +248,7 @@ public void TestCreateConnectionWithAutoRecoveryUsesAmqpTcpEndpoint()
217248
Port = 1234
218249
};
219250
var ep = new AmqpTcpEndpoint("localhost");
220-
using(IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) {}
251+
using (IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) { }
221252
}
222253

223254
[Test]
@@ -230,7 +261,7 @@ public void TestCreateConnectionWithAutoRecoveryUsesInvalidAmqpTcpEndpoint()
230261
var ep = new AmqpTcpEndpoint("localhost", 1234);
231262
Assert.That(() =>
232263
{
233-
using(IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })){}
264+
using (IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) { }
234265
}, Throws.TypeOf<BrokerUnreachableException>());
235266
}
236267

@@ -243,7 +274,7 @@ public void TestCreateConnectionUsesAmqpTcpEndpoint()
243274
Port = 1234
244275
};
245276
var ep = new AmqpTcpEndpoint("localhost");
246-
using(IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) {}
277+
using (IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) { }
247278
}
248279

249280
[Test]
@@ -258,7 +289,7 @@ public void TestCreateConnectionWithForcedAddressFamily()
258289
AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork
259290
};
260291
cf.Endpoint = ep;
261-
using(IConnection conn = cf.CreateConnection()){};
292+
using (IConnection conn = cf.CreateConnection()) { };
262293
}
263294

264295
[Test]
@@ -268,7 +299,7 @@ public void TestCreateConnectionUsesInvalidAmqpTcpEndpoint()
268299
var ep = new AmqpTcpEndpoint("localhost", 1234);
269300
Assert.That(() =>
270301
{
271-
using(IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) {}
302+
using (IConnection conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) { }
272303
}, Throws.TypeOf<BrokerUnreachableException>());
273304
}
274305

@@ -278,7 +309,7 @@ public void TestCreateConnectioUsesValidEndpointWhenMultipleSupplied()
278309
var cf = new ConnectionFactory();
279310
var invalidEp = new AmqpTcpEndpoint("not_localhost");
280311
var ep = new AmqpTcpEndpoint("localhost");
281-
using(IConnection conn = cf.CreateConnection(new List<AmqpTcpEndpoint> { invalidEp, ep })) {};
312+
using (IConnection conn = cf.CreateConnection(new List<AmqpTcpEndpoint> { invalidEp, ep })) { };
282313
}
283314

284315
[Test]

0 commit comments

Comments
 (0)