public void RecordFailureInfoForMessage(string messageId, Exception exception) { lock (lockObject) { if (failureInfoPerMessage.TryGetValue(messageId, out var node)) { // We have seen this message before, just update the counter and store exception. node.FailureInfo = new ProcessingFailureInfo(node.FailureInfo.NumberOfProcessingAttempts + 1, ExceptionDispatchInfo.Capture(exception)); // Maintain invariant: leastRecentlyUsedMessages.First contains the LRU item. leastRecentlyUsedMessages.Remove(node.LeastRecentlyUsedEntry); leastRecentlyUsedMessages.AddLast(node.LeastRecentlyUsedEntry); } else { if (failureInfoPerMessage.Count == maxElements) { // We have reached the maximum allowed capacity. Remove the LRU item. var leastRecentlyUsedEntry = leastRecentlyUsedMessages.First; failureInfoPerMessage.Remove(leastRecentlyUsedEntry.Value); leastRecentlyUsedMessages.RemoveFirst(); } var newNode = new FailureInfoNode( messageId, new ProcessingFailureInfo(1, ExceptionDispatchInfo.Capture(exception))); failureInfoPerMessage[messageId] = newNode; // Maintain invariant: leastRecentlyUsedMessages.First contains the LRU item. leastRecentlyUsedMessages.AddLast(newNode.LeastRecentlyUsedEntry); } } }
public void RecordFailureInfoForMessage(string messageId, Exception exception) { lock (lockObject) { FailureInfoNode node; if (failureInfoPerMessage.TryGetValue(messageId, out node)) { // We have seen this message before, just update the counter and store exception. node.FailureInfo = new ProcessingFailureInfo(node.FailureInfo.NumberOfProcessingAttempts + 1, ExceptionDispatchInfo.Capture(exception)); // Maintain invariant: leastRecentlyUsedMessages.First contains the LRU item. leastRecentlyUsedMessages.Remove(node.LeastRecentlyUsedEntry); leastRecentlyUsedMessages.AddLast(node.LeastRecentlyUsedEntry); } else { if (failureInfoPerMessage.Count == maxElements) { // We have reached the maximum allowed capacity. Remove the LRU item. var leastRecentlyUsedEntry = leastRecentlyUsedMessages.First; failureInfoPerMessage.Remove(leastRecentlyUsedEntry.Value); leastRecentlyUsedMessages.RemoveFirst(); } var newNode = new FailureInfoNode( messageId, new ProcessingFailureInfo(1, ExceptionDispatchInfo.Capture(exception))); failureInfoPerMessage[messageId] = newNode; // Maintain invariant: leastRecentlyUsedMessages.First contains the LRU item. leastRecentlyUsedMessages.AddLast(newNode.LeastRecentlyUsedEntry); } } }