Skip to content

Minor improvements and optimizations #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2020
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
4 changes: 3 additions & 1 deletion projects/RabbitMQ.Client/client/api/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,16 @@ public Uri Uri
public IAuthMechanismFactory AuthMechanismFactory(IList<string> mechanismNames)
{
// Our list is in order of preference, the server one is not.
foreach (IAuthMechanismFactory factory in AuthMechanisms)
for (int index = 0; index < AuthMechanisms.Count; index++)
{
IAuthMechanismFactory factory = AuthMechanisms[index];
string factoryName = factory.Name;
if (mechanismNames.Any<string>(x => string.Equals(x, factoryName, StringComparison.OrdinalIgnoreCase)))
{
return factory;
}
}

return null;
}

Expand Down
48 changes: 25 additions & 23 deletions projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ internal sealed class AutorecoveringConnection : IAutorecoveringConnection

private readonly object _recordedEntitiesLock = new object();

private readonly IDictionary<string, RecordedExchange> _recordedExchanges = new Dictionary<string, RecordedExchange>();
private readonly Dictionary<string, RecordedExchange> _recordedExchanges = new Dictionary<string, RecordedExchange>();

private readonly IDictionary<string, RecordedQueue> _recordedQueues = new Dictionary<string, RecordedQueue>();
private readonly Dictionary<string, RecordedQueue> _recordedQueues = new Dictionary<string, RecordedQueue>();

private readonly IDictionary<RecordedBinding, byte> _recordedBindings = new Dictionary<RecordedBinding, byte>();
private readonly Dictionary<RecordedBinding, byte> _recordedBindings = new Dictionary<RecordedBinding, byte>();

private readonly IDictionary<string, RecordedConsumer> _recordedConsumers = new Dictionary<string, RecordedConsumer>();
private readonly Dictionary<string, RecordedConsumer> _recordedConsumers = new Dictionary<string, RecordedConsumer>();

private readonly ICollection<AutorecoveringModel> _models = new List<AutorecoveringModel>();
private readonly List<AutorecoveringModel> _models = new List<AutorecoveringModel>();

private EventHandler<ConnectionBlockedEventArgs> _recordedBlockedEventHandlers;
private EventHandler<ShutdownEventArgs> _recordedShutdownEventHandlers;
Expand Down Expand Up @@ -485,12 +485,11 @@ public void DeleteRecordedBinding(RecordedBinding rb)

public RecordedConsumer DeleteRecordedConsumer(string consumerTag)
{
RecordedConsumer rc = null;
RecordedConsumer rc;
lock (_recordedEntitiesLock)
{
if (_recordedConsumers.ContainsKey(consumerTag))
if (_recordedConsumers.TryGetValue(consumerTag, out rc))
{
rc = _recordedConsumers[consumerTag];
_recordedConsumers.Remove(consumerTag);
}
}
Expand Down Expand Up @@ -912,10 +911,12 @@ private void PropagateQueueNameChangeToBindings(string oldName, string newName)
{
lock (_recordedBindings)
{
IEnumerable<RecordedBinding> bs = _recordedBindings.Keys.Where(b => b.Destination.Equals(oldName));
foreach (RecordedBinding b in bs)
foreach (RecordedBinding b in _recordedBindings.Keys)
{
b.Destination = newName;
if (b.Destination.Equals(oldName))
{
b.Destination = newName;
}
}
}
}
Expand All @@ -924,21 +925,22 @@ private void PropagateQueueNameChangeToConsumers(string oldName, string newName)
{
lock (_recordedConsumers)
{
IEnumerable<KeyValuePair<string, RecordedConsumer>> cs = _recordedConsumers.
Where(pair => pair.Value.Queue.Equals(oldName));
foreach (KeyValuePair<string, RecordedConsumer> c in cs)
foreach (KeyValuePair<string, RecordedConsumer> c in _recordedConsumers)
{
c.Value.Queue = newName;
if (c.Value.Queue.Equals(oldName))
{
c.Value.Queue = newName;
}
}
}
}

private void RecoverBindings()
{
IDictionary<RecordedBinding, byte> recordedBindingsCopy = null;
Dictionary<RecordedBinding, byte> recordedBindingsCopy;
lock (_recordedBindings)
{
recordedBindingsCopy = _recordedBindings.ToDictionary(e => e.Key, e => e.Value);
recordedBindingsCopy = new Dictionary<RecordedBinding, byte>(_recordedBindings);
}

foreach (RecordedBinding b in recordedBindingsCopy.Keys)
Expand Down Expand Up @@ -1031,10 +1033,10 @@ private void RecoverConsumers()
throw new ObjectDisposedException(GetType().FullName);
}

IDictionary<string, RecordedConsumer> recordedConsumersCopy = null;
Dictionary<string, RecordedConsumer> recordedConsumersCopy;
lock (_recordedConsumers)
{
recordedConsumersCopy = _recordedConsumers.ToDictionary(e => e.Key, e => e.Value);
recordedConsumersCopy = new Dictionary<string, RecordedConsumer>(_recordedConsumers);
}

foreach (KeyValuePair<string, RecordedConsumer> pair in recordedConsumersCopy)
Expand Down Expand Up @@ -1091,10 +1093,10 @@ private void RecoverEntities()

private void RecoverExchanges()
{
IDictionary<string, RecordedExchange> recordedExchangesCopy = null;
Dictionary<string, RecordedExchange> recordedExchangesCopy;
lock (_recordedEntitiesLock)
{
recordedExchangesCopy = _recordedExchanges.ToDictionary(e => e.Key, e => e.Value);
recordedExchangesCopy = new Dictionary<string, RecordedExchange>(_recordedExchanges);
}

foreach (RecordedExchange rx in recordedExchangesCopy.Values)
Expand Down Expand Up @@ -1125,10 +1127,10 @@ private void RecoverModels()

private void RecoverQueues()
{
IDictionary<string, RecordedQueue> recordedQueuesCopy = null;
Dictionary<string, RecordedQueue> recordedQueuesCopy;
lock (_recordedEntitiesLock)
{
recordedQueuesCopy = _recordedQueues.ToDictionary(entry => entry.Key, entry => entry.Value);
recordedQueuesCopy = new Dictionary<string, RecordedQueue>(_recordedQueues);
}

foreach (KeyValuePair<string, RecordedQueue> pair in recordedQueuesCopy)
Expand Down
6 changes: 1 addition & 5 deletions projects/RabbitMQ.Client/client/impl/BasicProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ public override object Clone()
var clone = MemberwiseClone() as BasicProperties;
if (IsHeadersPresent())
{
clone.Headers = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> entry in Headers)
{
clone.Headers[entry.Key] = entry.Value;
}
clone.Headers = new Dictionary<string, object>(Headers);
}

return clone;
Expand Down
5 changes: 2 additions & 3 deletions projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,9 @@ public void PrettyPrintShutdownReport()
{
Console.Error.WriteLine(
"Log of errors while closing connection {0}:", this);
foreach (ShutdownReportEntry entry in ShutdownReport)
for (int index = 0; index < ShutdownReport.Count; index++)
{
Console.Error.WriteLine(
entry.ToString());
Console.Error.WriteLine(ShutdownReport[index].ToString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

namespace RabbitMQ.Client.Impl
{
struct ContentHeaderPropertyReader
internal struct ContentHeaderPropertyReader
{
private ushort m_bitCount;
private ushort m_flagWord;
Expand Down Expand Up @@ -143,9 +143,9 @@ public string ReadShortstr()
}

/// <returns>A type of <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
public IDictionary<string, object> ReadTable()
public Dictionary<string, object> ReadTable()
{
IDictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
Dictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
_memoryOffset += bytesRead;
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

namespace RabbitMQ.Client.Impl
{
struct MethodArgumentReader
internal struct MethodArgumentReader
{
private int? _bit;
private int _bits;
Expand Down Expand Up @@ -125,10 +125,10 @@ public string ReadShortstr()
return result;
}

public IDictionary<string, object> ReadTable()
public Dictionary<string, object> ReadTable()
{
ClearBits();
IDictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
Dictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
_memoryOffset += bytesRead;
return result;
}
Expand Down
36 changes: 18 additions & 18 deletions projects/RabbitMQ.Client/client/impl/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ namespace RabbitMQ.Client.Impl
{
abstract class ModelBase : IFullModel, IRecoverable
{
public readonly IDictionary<string, IBasicConsumer> m_consumers = new Dictionary<string, IBasicConsumer>();

///<summary>Only used to kick-start a connection open
///sequence. See <see cref="Connection.Open"/> </summary>
public BlockingCell<ConnectionStartDetails> m_connectionStartCell = null;

private readonly Dictionary<string, IBasicConsumer> _consumers = new Dictionary<string, IBasicConsumer>();

private TimeSpan _handshakeContinuationTimeout = TimeSpan.FromSeconds(10);
private TimeSpan _continuationTimeout = TimeSpan.FromSeconds(20);

Expand Down Expand Up @@ -500,11 +500,11 @@ public void OnSessionShutdown(object sender, ShutdownEventArgs reason)
ConsumerDispatcher.Quiesce();
SetCloseReason(reason);
OnModelShutdown(reason);
BroadcastShutdownToConsumers(m_consumers, reason);
BroadcastShutdownToConsumers(_consumers, reason);
ConsumerDispatcher.Shutdown(this).GetAwaiter().GetResult();;
}

protected void BroadcastShutdownToConsumers(IDictionary<string, IBasicConsumer> cs, ShutdownEventArgs reason)
protected void BroadcastShutdownToConsumers(Dictionary<string, IBasicConsumer> cs, ShutdownEventArgs reason)
{
foreach (KeyValuePair<string, IBasicConsumer> c in cs)
{
Expand Down Expand Up @@ -578,10 +578,10 @@ public void HandleBasicAck(ulong deliveryTag,
public void HandleBasicCancel(string consumerTag, bool nowait)
{
IBasicConsumer consumer;
lock (m_consumers)
lock (_consumers)
{
consumer = m_consumers[consumerTag];
m_consumers.Remove(consumerTag);
consumer = _consumers[consumerTag];
_consumers.Remove(consumerTag);
}
if (consumer == null)
{
Expand All @@ -601,10 +601,10 @@ public void HandleBasicCancelOk(string consumerTag)
consumerTag
));
*/
lock (m_consumers)
lock (_consumers)
{
k.m_consumer = m_consumers[consumerTag];
m_consumers.Remove(consumerTag);
k.m_consumer = _consumers[consumerTag];
_consumers.Remove(consumerTag);
}
ConsumerDispatcher.HandleBasicCancelOk(k.m_consumer, consumerTag);
k.HandleCommand(null); // release the continuation.
Expand All @@ -615,9 +615,9 @@ public void HandleBasicConsumeOk(string consumerTag)
var k =
(BasicConsumerRpcContinuation)_continuationQueue.Next();
k.m_consumerTag = consumerTag;
lock (m_consumers)
lock (_consumers)
{
m_consumers[consumerTag] = k.m_consumer;
_consumers[consumerTag] = k.m_consumer;
}
ConsumerDispatcher.HandleBasicConsumeOk(k.m_consumer, consumerTag);
k.HandleCommand(null); // release the continuation.
Expand All @@ -632,9 +632,9 @@ public virtual void HandleBasicDeliver(string consumerTag,
ReadOnlyMemory<byte> body)
{
IBasicConsumer consumer;
lock (m_consumers)
lock (_consumers)
{
consumer = m_consumers[consumerTag];
consumer = _consumers[consumerTag];
}
if (consumer == null)
{
Expand Down Expand Up @@ -1005,9 +1005,9 @@ public void BasicCancel(string consumerTag)
k.GetReply(ContinuationTimeout);
}

lock (m_consumers)
lock (_consumers)
{
m_consumers.Remove(consumerTag);
_consumers.Remove(consumerTag);
}

ModelShutdown -= k.m_consumer.HandleModelShutdown;
Expand All @@ -1017,9 +1017,9 @@ public void BasicCancelNoWait(string consumerTag)
{
_Private_BasicCancel(consumerTag, true);

lock (m_consumers)
lock (_consumers)
{
m_consumers.Remove(consumerTag);
_consumers.Remove(consumerTag);
}
}

Expand Down
17 changes: 10 additions & 7 deletions projects/RabbitMQ.Client/client/impl/ProtocolBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ namespace RabbitMQ.Client.Framing.Impl
{
abstract class ProtocolBase : IProtocol
{
public IDictionary<string, bool> Capabilities = new Dictionary<string, bool>();
public IDictionary<string, bool> Capabilities;

public ProtocolBase()
{
Capabilities["publisher_confirms"] = true;
Capabilities["exchange_exchange_bindings"] = true;
Capabilities["basic.nack"] = true;
Capabilities["consumer_cancel_notify"] = true;
Capabilities["connection.blocked"] = true;
Capabilities["authentication_failure_close"] = true;
Capabilities = new Dictionary<string, bool>
{
["publisher_confirms"] = true,
["exchange_exchange_bindings"] = true,
["basic.nack"] = true,
["consumer_cancel_notify"] = true,
["connection.blocked"] = true,
["authentication_failure_close"] = true
};
}

public abstract string ApiName { get; }
Expand Down
5 changes: 3 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public Session(Connection connection, int channelNumber)

public override void HandleFrame(in InboundFrame frame)
{
using (Command cmd = _assembler.HandleFrame(in frame))
Command cmd = _assembler.HandleFrame(in frame);
if (cmd != null)
{
if (cmd != null)
using (cmd)
{
OnCommandReceived(cmd);
}
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SessionManager
public readonly ushort ChannelMax;
private readonly IntAllocator _ints;
private readonly Connection _connection;
private readonly IDictionary<int, ISession> _sessionMap = new Dictionary<int, ISession>();
private readonly Dictionary<int, ISession> _sessionMap = new Dictionary<int, ISession>();
private bool _autoClose = false;

public SessionManager(Connection connection, ushort channelMax)
Expand Down
6 changes: 1 addition & 5 deletions projects/RabbitMQ.Client/client/impl/StreamProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public override object Clone()
var clone = MemberwiseClone() as StreamProperties;
if (IsHeadersPresent())
{
clone.Headers = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> entry in Headers)
{
clone.Headers[entry.Key] = entry.Value;
}
clone.Headers = new Dictionary<string, object>(Headers);
}

return clone;
Expand Down
Loading